[英]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);
}
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()
启用对位置参数的处理。
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);
}
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.