【发布时间】:2011-02-25 17:09:36
【问题描述】:
我有一个使用 boost v1.45.0 程序选项的 Visual Studio 2008 C++ 应用程序。
我希望能够解析如下所示的命令行选项:foo.exe -x 1,2, 4-7,这样它会生成具有值 [1、2、4、5、6、7] 的 std::vector< int >。所以,我写了一个自定义验证器:
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。
我需要做些什么才能使这项工作如愿以偿?
谢谢, 保罗H
【问题讨论】:
-
运行时给它的命令行是什么?
-
BTW -- 你是否使用 UNICODE 编译...在这种情况下你需要使用库操作的 wstring 版本。
-
@Dennis - 命令行是
foo.exe -x 1,2, 4-7。是的,我正在使用 UNICODE 标志进行编译。更改为std::wstring不会更改结果。我得到了同样的例外。
标签: c++ boost boost-program-options