我有一个使用boost v1.45.0程序选项的Visual Studio 2008 C++应用程序。使用boost程序选项无效的选项值异常
我想能够解析一个命令行选项,看起来像这样: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
。
我需要做什么才能使这项工作按需要进行?
感谢, PaulH
当您运行时,您提供的命令行是什么? – Dennis 2011-02-25 17:42:14
顺便说一句 - 你在编译使用UNICODE ...在这种情况下,你需要使用库操作的wstring版本。 – Dennis 2011-02-25 17:51:02
@丹尼斯 - 命令行是'foo.exe -x 1,2,4-7'。是的,我正在编译UNICODE标志。更改为'std :: wstring'不会改变结果。我得到同样的例外。 – PaulH 2011-02-25 18:06:19