windows boost::program_options - 解析多个命令行参数,其中一些是包括空格和字符的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4092405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
boost::program_options - parsing multiple command line arguments where some are strings including spaces and characters
提问by Onkar Deshpande
I want to parse multiple command line arguments using boost::program_options. However, some arguments are strings enclosed in double quotes. This is what I have -
我想使用 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.
}
}
this works fine when I do
当我这样做时,这很好用
cmdparsing.exe --create arg1 arg2
But does notwork when I do
但是当我这样做时不起作用
cmdparsing.exe --create "this is arg1" "this is arg2"
from windows command line. For second option, it gets converted to ["this" "is" "arg1" "this" "is" "arg2"]
in createOptions vector. Thus, val1
gets "this"
and val2
gets
"is"
instead of "this is arg1"
and "this is arg2"
respectively.
从 Windows 命令行。对于第二个选项,它被转换为["this" "is" "arg1" "this" "is" "arg2"]
createOptions 向量。因此,val1
获得"this"
和val2
得到
"is"
,而不是"this is arg1"
和"this is arg2"
分别。
How can I use boost::program_option to make this work ?
我如何使用 boost::program_option 来完成这项工作?
回答by Onkar Deshpande
I fixed it using a native Windows function which handles command line arguments differently. See CommandLineToArgvWfor details. Before passing it to processCommands(), I am modifying my argv[] and argc using the method mentioned above. Thank you Bart van Ingen Schenau for your comment.
我使用本机 Windows 函数修复了它,该函数以不同方式处理命令行参数。有关详细信息,请参阅CommandLineToArgvW。在将它传递给 processCommands() 之前,我正在使用上述方法修改我的 argv[] 和 argc。感谢 Bart van Ingen Schenau 的评论。
#ifdef _WIN32
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (NULL == argv)
{
std::wcout << L"CommandLineToArgvw failed" << std::endl;
return -1;
}
#endif
回答by Fantastic Mr Fox
You should be able to achieve this with positional options
:
您应该能够通过以下方式实现这一目标positional options
:
positional_options_description pos_desc;
pos_desc.add("create", 10); // Force a max of 10.
Then when you parse the command line add this pos_desc
:
然后当你解析命令行时添加pos_desc
:
using namespace boost::program_options;
command_line_parser parser{argc, argv};
parser.options(desc).positional(pos_desc);
store(parser.run(), vm);