简体   繁体   English

Boost :: Program_options,传递未知的命令行参数

[英]Boost::Program_options, passing an unknown command line argument

I am using boost::program_options to pass configuration files for my program. 我正在使用boost :: program_options传递程序的配置文件。 In particular I use often command line overriding of some of the options. 特别是,我经常使用命令行覆盖某些选项。 For example if I register two options "opt1" and "opt2" I can successfully override the default values by running my program with 例如,如果我注册了两个选项“ opt1”和“ opt2”,则可以通过运行程序来成功覆盖默认值

 myProgram.exe --opt1=option_value_1 --opt2=option_value_2

All good, but it happened already few times that I run my program mistakenly as 一切都很好,但是已经发生了几次,我误将程序运行为

 myProgram.exe --opt1=option_value_1 opt2=option_value_2

In such a case (missing double hyphen) no error is thrown. 在这种情况下(缺少双连字符),不会引发任何错误。 In fact I can apparently run myProgram as 实际上,我显然可以将myProgram运行为

 myProgram.exe list of any unregistered and unknown values

and it still runs correctly. 并且仍然可以正常运行。 I would expect to at least get informed that something unexpected happened. 我希望至少能得到一些意外情况的通知。 Is there a solution to my problem? 我的问题有解决方案吗?

You should remove allow_unregistered() from your parse command. 您应该从解析命令中删除allow_unregistered() You command should simply be 您的命令应该只是

po::store(parse_command_line(argc, argv, desc), vm);

then exception will be thrown on unknown options. 那么异常将抛出未知选项。

http://www.boost.org/doc/libs/1_54_0/doc/html/program_options/howto.html#idp123440592 http://www.boost.org/doc/libs/1_54_0/doc/html/program_options/howto.html#idp123440592

If you want exception/error, if option has no "--" you should write extra parser, something like this, can help you 如果您想要异常/错误,如果选项没有“-”,则应该编写额外的解析器,这样可以帮助您

std::pair<std::string, std::string> fix_option(const std::string& value)
{
   std::string name = value;
   std::string val;
   std::string::size_type pos = name.find("=");
   if (pos != std::string::npos)
   {
      val = name.substr(pos + 1);
      name = name.substr(0, pos);
   }
   if (name.substr(0, 2) != "--")
   {
      throw std::logic_error(std::string("invalid command, no -- in command: ") + name);
   }
   return std::make_pair(name.substr(2), val);
}

code example 代码示例

results: 结果:

./new --help=j

output: j

./new help=j

output: 
terminate called after throwing an instance of 'std::logic_error'
  what():  invalid command, no -- in command: help

It seems that boost::program_options does not recognize positional arguments per default. 似乎boost :: program_options不能默认识别位置参数。 This means that non-option arguments like opt2=option_value_2 are ignored. 这意味着诸如opt2=option_value_2类的非选项参数将被忽略。 However, the documentation is not clear about it. 但是,文档尚不清楚。 You can enable handling of positional arguments with basic_command_line_parser::positional() . 您可以使用basic_command_line_parser::positional()启用对位置参数的处理。

By Example 举个例子

try {
    po::variables_map vm;
    po::store(po::command_line_parser(argc, argv).
                options(desc).positional({}).run(),
              vm);
    po::notify(vm);
} catch (po::too_many_positional_options_error &e) {
    // A positional argument like `opt2=option_value_2` was given
    cerr << e.what() << endl;
    exit(1);
} catch (po::error_with_option_name &e) {
    // Another usage error occurred
    cerr << e.what() << endl;
    exit(1);
}

Explanation 说明

Basically, 基本上,

po::store(po::parse_command_line(argc, argv, desc), vm);

has been replaced with 已被替换为

po::store(po::command_line_parser(argc, argv)
            .options(desc).positional({}).run(),
          vm);

As I understand the documentation, parse_command_line(argc, argv, desc) is a shorthand for command_line_parser(argc, argv).options(desc).run() . 据我了解的文档, parse_command_line(argc, argv, desc)command_line_parser(argc, argv).options(desc).run()的简写。 Through adding a call to positional() , we are enabling handling of positional arguments. 通过添加对positional()的调用,我们可以处理位置参数。 Through specifying {} , no positional arguments are allowed. 通过指定{} ,不允许使用位置参数。 An instance of too_many_positional_options_error is thrown when too many positional arguments are given. 给定太多位置参数时,将抛出too_many_positional_options_error实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 boost :: program_options始终以空值解析命令行 - boost::program_options parse command line always with empty value 使用boost :: program_options解析LPTSTR *命令行参数 - Parsing LPTSTR* command line args with boost::program_options 在 INI 和命令行中使用 boost::program_options 的正确方法? - Sane way to use boost::program_options with INI and command line? boost :: program_options“多态”参数 - boost::program_options “polymorphic” argument boost :: program_options - 它是否为命令行选项进行了精确的字符串匹配? - boost::program_options - does it do an exact string matching for command line options? 如何通过 Boost Program_Options 使用命令行和分层配置文件的选项 - How to use options for both command line and hierarchical config file with Boost Program_Options boost :: program_options:如何忽略未知参数? - boost::program_options: how to ignore unknown parameters? boost program_options:读入3d向量作为命令行参数 - boost program_options: Read in 3d Vector as Command Line Parameter boost :: Program_options一种从命令行或ini文件判断值的方法? - boost::Program_options A way to tell if the value from command line or ini file? boost :: program_options - 解析多个命令行参数,其中一些是包含空格和字符的字符串 - boost::program_options - parsing multiple command line arguments where some are strings including spaces and characters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM