升压型程序选项添加选项语法(Boost Program Options Add Options Sy

2019-06-24 05:26发布

我写一个使用Boost的程序选项库的程序,我注意到下面的语法,因为我看到它已经困扰我:

desc.add_options()
        ("help","produce help message")
        ( /* other flag, value, description pairs here */)
;

我看到,在标题中,运营商()被覆盖,但我不知道如何使这是语法正确。

其次,它有什么优势这句法,只调用add_options()多次(除了显示的事实,你可以操纵的语法像这样关)相比?

Answer 1:

所述add_options成员函数返回类型的对象options_description_easy_init 。 后者具有operator()重载到参考返回到自身。 这允许您链的调用如您在片段所示。

链接呼叫和通话之间的差异add_options几次是在前者的情况下的一个实例options_description_easy_init创建和每次调用operator()就可以了,它添加的选项所有者( options_description )。 如果你打电话给add_options多次每次通话将创建的新实例options_description_easy_init



Answer 2:

其优点的问题是主观的,但在这种情况下,它的简洁。

从我家的一个项目比较:

("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.")

为此:

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.");

线路长度问题。 而且不必有visible.add_options()眼前的一切使得它更容易阅读。



文章来源: Boost Program Options Add Options Syntax