我想使用boost :: program_options解析多個命令行參數。但是,一些參數是用雙引號括起來的字符串。這就是我 -boost :: program_options - 解析多個命令行參數,其中一些是包括空格和字符的字符串
void processCommands(int argc, char *argv[]) {
std::vector<std::string> createOptions;
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("create", boost::program_options::value<std::vector<std::string> >(&createOptions)->multitoken(), "create command")
;
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
boost::program_options::notify(vm);
if(vm.count("create") >= 1) {
std::string val1 = createOptions[0];
std::string val2 = createOptions[1];
...
// call some function passing val1, val2.
}
}
當我做
cmdparsing.exe --create arg1 arg2
這工作正常但是當我從Windows命令行做
cmdparsing.exe --create "this is arg1" "this is arg2"
不工作。對於第二個選項,它將在createOptions向量中轉換爲["this" "is" "arg1" "this" "is" "arg2"]
。因此,val1
得到"this"
和val2
得到 "is"
而不是"this is arg1"
和"this is arg2"
。
如何使用boost :: program_option來完成這項工作?
這在Linux上工作。 – 2010-11-04 08:50:13
首先要檢查的是操作系統如何爲您的程序提供這些選項。如果'cmdparsing.exe --create this是arg1'和'cmdparsing.exe --create「這是arg1」'結果與'argv'數組的內容相同,那麼您必須找到一些其他方式來說服您的操作系統引號中的部分需要保持在一起。 – 2010-11-04 13:43:06