我有一個使用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