Boost程序选项添加选项语法

pau*_*ler 21 c++ boost boost-program-options

我正在编写一个使用Boost程序选项库的程序,我注意到以下语法在我看到之后一直困扰着我:

desc.add_options()
        ("help","produce help message")
        ( /* other flag, value, description pairs here */)
;
Run Code Online (Sandbox Code Playgroud)

我在标题中看到,operator()被覆盖了,但是我不确定它是如何允许它在语法上正确的.

其次,与多次调用add_options()相比,这种语法是否有任何优势(除了展示你可以像这样操作语法这一事实)?

Pra*_*ian 18

所述add_options成员函数返回类型的对象options_description_easy_init.后者已经operator()重载以返回对自身的引用.这允许您按照片段中显示的方式链接呼叫.

链接调用和add_options多次调用之间的区别在于,在前一种情况下,options_description_easy_init创建了一个实例,每次调用operator()它时,它都会将选项添加到owner(options_description).如果您add_options多次调用,每次调用都会创建一个新实例options_description_easy_init.


Nic*_*las 14

优势问题是主观的,但在这种情况下,它是简洁的.

比较我的一个家庭项目:

("help,h", "Generate this help message")
("output-file,o", po::value<std::string>(), "Output filename. Required.")
("tangent,t", "Generate/load tangent-space basis.")
("collada-output,c", "Write a Collada file, rather than our mesh XML format.")
("arrays,a", "Write arrays instead of indexed verts. Cannot combine with Collada writing.")
("flip-tangent,f", "Change the tangent-space basis matrix's handedness. Negates bitangent.")
("map", po::value<std::string>(), "Map filename. Defaults to the ColladaConv directory's 'stdmap.txt' file.")
("vao", po::value<std::vector<std::string> >(), "Sequence of mappings, of the form:\n"
        "Name # # # #\n"
        "\n"
        "Each # is an attribute index to use for this VAO.\n"
        "Each VAO name must be unique; you cannot use the same VAO in the same place.")
Run Code Online (Sandbox Code Playgroud)

对此:

visible.add_options()("help,h", "Generate this help message")
visible.add_options()("output-file,o", po::value<std::string>(), "Output filename. Required.")
visible.add_options()("tangent,t", "Generate/load tangent-space basis.");
visible.add_options()("collada-output,c", "Write a Collada file, rather than our mesh XML format.");
visible.add_options()("arrays,a", "Write arrays instead of indexed verts. Cannot combine with Collada writing.");
visible.add_options()("flip-tangent,f", "Change the tangent-space basis matrix's handedness. Negates bitangent.");
visible.add_options()("map", po::value<std::string>(), "Map filename. Defaults to the ColladaConv directory's 'stdmap.txt' file.");
visible.add_options()("vao", po::value<std::vector<std::string> >(), "Sequence of mappings, of the form:\n"
        "Name # # # #\n"
        "\n"
        "Each # is an attribute index to use for this VAO.\n"
        "Each VAO name must be unique; you cannot use the same VAO in the same place.");
Run Code Online (Sandbox Code Playgroud)

线长很重要.并且不必拥有visible.add_options()一切,使其更容易阅读.