我有一个使用 boost v1.45.0 程序选项的 Visual Studio 2008 C++ 应用程序。
我希望能够解析一个看起来像这样的命令行选项:foo.exe -x 1,2, 4-7
这样它会产生一个std::vector< int >
值为 [1, 2, 4, 5, 6, 7] 的值。所以,我写了一个自定义验证器:
typedef std::vector< int > IDList;
void validate( boost::any& v, const std::vector< std::string >& tokens, IDList*, int )
{
// Never gets here
}
int _tmain( int argc, _TCHAR* argv[] )
{
IDList test_case_ids;
po::options_description desc( "Foo options" );
desc.add_options()
("id,x", po::value< IDList >(), "Specify a single ID or a range of IDs as shown in the following command line: foo.exe -x10,12, 15-20")
;
po::variables_map vm;
try
{
po::store( po::parse_command_line( argc, argv, desc ), vm );
po::notify( vm );
}
catch( const std::exception& e)
{
std::cerr << e.what() << std::endl;
std::cout << desc << std::endl;
return 1;
}
return 0;
}
但是,我从来没有得到我的自定义验证器代码。我总是收到一条异常parse_command_line
消息:in option 'id': invalid option value
.
我需要做什么才能使这项工作如愿以偿?
谢谢,保罗