Boost:位置参数无法识别的选项
Boost: unrecognised option for positional argument
我正在尝试使用 boost 1.58.0 解析命令行。我的代码非常简单,来自教程 copy\pasted。看起来像这样:
try {
po::options_description desc;
desc.add_options()
("version,v", "Display version of application.");
po::positional_options_description p;
p.add("input-file", -1);
try
{
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
if ( vm.count("version") )
{
std::cout << "Program version: " << SHUF_T_VERSION << std::endl << "Boost library version: " << BOOST_LIB_VERSION << std::endl;
return false;
}
po::notify(vm); // throws on error, so do after help in case
// there are any problems
}
catch(po::error& e)
{
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return false;
}
}
catch(std::exception& e)
{
std::cerr << "Unhandled Exception: "
<< e.what() << ", application will now exit" << std::endl;
return false;
}
return true;
整个代码是here。
代码似乎是正确的。 app -v
处理正确。但是如果我包含任何位置争论,比如 app myfile
,po::store()
就会抛出 unrecognised option 'myfile'
。关于为什么会发生这种情况有什么想法吗?
您需要添加 "input-file" 作为选项:
po::options_description desc;
desc.add_options()
("version,v", "Display version of application.")
("input-file", po::value<std::vector<std::string> >(), "input file");
来自the tutorial:
The "input-file" option specifies the list of files to process. That's okay for a start, but, of course, writing something like:
compiler --input-file=a.cpp
is a little non-standard, compared with
compiler a.cpp
We'll address this in a moment.
The command line tokens which have no option name, as above, are called "positional options" by this library. They can be handled too. With a little help from the user, the library can decide that "a.cpp" really means the same as "--input-file=a.cpp". Here's the additional code we need:
po::positional_options_description p;
p.add("input-file", -1);
po::variables_map vm;
po::store(po::command_line_parser(ac, av).
options(desc).positional(p).run(), vm);
po::notify(vm);
我正在尝试使用 boost 1.58.0 解析命令行。我的代码非常简单,来自教程 copy\pasted。看起来像这样:
try {
po::options_description desc;
desc.add_options()
("version,v", "Display version of application.");
po::positional_options_description p;
p.add("input-file", -1);
try
{
po::store(po::command_line_parser(argc, argv).
options(desc).positional(p).run(), vm);
if ( vm.count("version") )
{
std::cout << "Program version: " << SHUF_T_VERSION << std::endl << "Boost library version: " << BOOST_LIB_VERSION << std::endl;
return false;
}
po::notify(vm); // throws on error, so do after help in case
// there are any problems
}
catch(po::error& e)
{
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
std::cerr << desc << std::endl;
return false;
}
}
catch(std::exception& e)
{
std::cerr << "Unhandled Exception: "
<< e.what() << ", application will now exit" << std::endl;
return false;
}
return true;
整个代码是here。
代码似乎是正确的。 app -v
处理正确。但是如果我包含任何位置争论,比如 app myfile
,po::store()
就会抛出 unrecognised option 'myfile'
。关于为什么会发生这种情况有什么想法吗?
您需要添加 "input-file" 作为选项:
po::options_description desc;
desc.add_options()
("version,v", "Display version of application.")
("input-file", po::value<std::vector<std::string> >(), "input file");
来自the tutorial:
The "input-file" option specifies the list of files to process. That's okay for a start, but, of course, writing something like:
compiler --input-file=a.cppis a little non-standard, compared with
compiler a.cppWe'll address this in a moment.
The command line tokens which have no option name, as above, are called "positional options" by this library. They can be handled too. With a little help from the user, the library can decide that "a.cpp" really means the same as "--input-file=a.cpp". Here's the additional code we need:
po::positional_options_description p; p.add("input-file", -1); po::variables_map vm; po::store(po::command_line_parser(ac, av). options(desc).positional(p).run(), vm); po::notify(vm);