Boost 程序选项语法

发布于 2024-12-02 11:16:38 字数 997 浏览 2 评论 0原文

我正在使用 boost::program_options 读取用户的输入命令行参数。它工作得非常好,允许我输出有用的使用消息并正确验证输入。但是,默认情况下,长选项名称必须位于双破折号之后,例如 --my_long_option ,短选项名称位于单破折号之后并且必须是单个字符,例如; -m

有没有办法...

  • 在单个 - 之后允许长选项?
  • 允许短选项有多个字符吗?

因此,允许我拥有看起来像这样的命令行。

./a.out -myopt1 foo -myopt2 bar

两种可能性应该具有相同的效果,尽管从编程的角度来看,第一种会更好。我查看了 boost::program_options::command_line_style 但看起来它不能满足我的需要。

谢谢

编辑:除了下面接受的答案之外,要使其使用该样式,必须添加以下代码(遵循 boost 文档的命名约定)

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不如归去 2024-12-09 11:16:38

根据定义,短选项只有一个字符。如果他们有更多,他们就会有很多选择。

要允许长选项以单个破折号开头,请包含 allow_long_disguise 命令行样式,如您链接到的文档页面上所述

可以通过与短选项相同的字符引入长选项,请参阅allow_long_disguise。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文