提升程序选项语法 [英] Boost Program Options Syntax

查看:131
本文介绍了提升程序选项语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用提振:: program_options 来读取用户的通过命令行参数输入。它的工作原理非常精美,让我可以输出有益的使用信息和正确的验证输入。然而,在默认情况下长选项名称必须拿出一个双横线例如在 - my_long_option 和短选项来一个破折号后的的必须是单个字符,例如; -m

有没有办法要么...


  • 允许很久之后选择一个 -

  • 允许短选项有多个字符?

因此​​,让我有命令行,看起来像

  ./ a.out的-myopt1富-myopt2酒吧

这两个可能性应该有同样的效果,虽然从编程的角度来看第一个会更好。我一看<一个href=\"http://www.boost.org/doc/libs/1_38_0/doc/html/boost/program_options/command_line_style/style_t.html\">boost::program_options::command_line_style但它并不像它可以做什么,我需要。

感谢

编辑:继低于公认的答案得到它使用的样式必须添加以下code(以下升压文档的命名约定)

  PO ::店(
    PO :: command_line_parser(AC,AV)
        可供选项(DESC)
        。样式(
            PO :: command_line_style :: unix_style
          | PO :: command_line_style :: allow_long_disguise)
        。跑(),
    VM);


解决方案

短选项的定义只有一个字符。如果他们有更多的,他们会长期选择。

要允许长选项来启动一个单一的破折号,包括 allow_long_disguise 命令行风格,<一个href=\"http://www.boost.org/doc/libs/1_38_0/doc/html/boost/program_options/command_line_style/style_t.html#boost.program_options.command_line_style.style_t.allow_long_disguise\">as

:您链接到的文档页面上描述

  

这是可能的引进由相同的字符长选项短选项,请参见allow_long_disguise。


I'm using boost::program_options to read the users' input from the command line argument. It works very nicely and allows me to output helpful usage messages and validate input properly. However, by default long option names must come after a double-dash for example --my_long_option and short options come after a single dash and must be a single character, example; -m.

Is there a way to either...

  • Allow long options after a single -?
  • Allow short options to have more than one character?

Thus allowing me to have command lines which looks like

./a.out -myopt1 foo -myopt2 bar

The two possibilities should have the same effect though from a programming point of view the first would be better. I had a look at boost::program_options::command_line_style but it doesn't look like it can do what I need.

Thanks

Edit: Further to accepted answer below to get it to use that style one must add the following code (following the naming convention of the boost docs)

po::store(
    po::command_line_parser(ac,av)
        .options(desc)
        .style(
            po::command_line_style::unix_style
          | po::command_line_style::allow_long_disguise)
        .run(),
    vm);

解决方案

Short options by definition have just one character. If they had more, they'd be long options.

To allow long options to start with a single dash, include the allow_long_disguise command-line style, as described on the documentation page you linked to:

It's possible to introduce long options by the same character as short options, see allow_long_disguise.

这篇关于提升程序选项语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆