c++ - Boost 程序选项语法

标签 c++ boost command-line boost-program-options

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

最佳答案

根据定义,短选项只有一个字符。如果他们有更多,他们将是很长的选择。

要允许长选项以单个破折号开头,请包含 allow_long_disguise 命令行样式 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.

关于c++ - Boost 程序选项语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7233325/

相关文章:

c++ - 将函数从一个类发送到另一个类并在 C++ 中执行

c++ - 我怎样才能 boost::bind 到传递并返回 std::string 的托管类的成员?

ios - 从越狱应用程序 ios 7 启动命令行

C++ 如何通过名称查找类?

C++:如何构建我自己的实用程序库?

c++ - boost::spirit::qi::parse --> 无结果

c++ - 振奋 spirit : What type names should be used for the built in terminals?

amazon-web-services - AWS S3 CLI : Anonymous users cannot initiate multipart uploads

command-line - 如何识别 CVS 中存在哪些分支?

c++ - 如何从 flatbuffers 中获取 union 结构的 void* 值反序列化的大小