【问题标题】:Boost Program Options Add Options SyntaxBoost 程序选项添加选项语法
【发布时间】:2012-05-07 17:43:35
【问题描述】:

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

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

我看到在标题中,operator() 被覆盖,但我不确定这如何使它在语法上是正确的。

其次,与只调用 add_options() 多次相比,这种语法有什么优势(除了炫耀您可以像这样操作语法之外)?

【问题讨论】:

  • Boost 作者喜欢炫耀...
  • “闹鬼”是一个很好的描述......我正式理解它,但“感觉”很奇怪......

标签: c++ boost boost-program-options


【解决方案1】:

add_options 成员函数返回options_description_easy_init 类型的对象。后者重载了operator() 以返回对自身的引用。这允许您像在 sn-p 中显示的那样链接调用。

链接调用和多次调用add_options 之间的区别在于,在前一种情况下,会创建一个options_description_easy_init 的单个实例,并且每次调用operator() 时,它都会将选项添加到所有者( options_description)。如果您要多次调用add_options,则每次调用都会创建一个options_description_easy_init 的新实例。

【讨论】:

  • @paulrehkugler Nicol 的 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(),这样更容易阅读。

【讨论】:

  • 我怀疑这是唯一的优势。在我看来,制作更漂亮的代码需要做更多的工作。
  • 这两个例子都非常可怕。您能否定义options_description_easy_init o = visible.add_options() 并简单地调用o("help", "Generate this help message");
  • @Vortico:我没有看到第一个案例有什么特别令人不快的地方。确实,我看不出您的建议比第一个建议有什么好处,即使它确实有效。我不知道会不会;您必须查看 Boost 的文档才能看到。但一般来说,让中间体远离这样的构造并不是一个好主意。
  • 不是v.ao(...);?
  • @Aconcagua:vao 是什么?至少对于当前的语法,很明显每组括号都在做与add_options 相关的事情。对于你的,它是关于具有神秘名称的符号。
猜你喜欢
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多