boost::program_options - 解析多个命令行参数,其中一些是包括空格和字符的字符串

发布于 2024-09-30 09:24:36 字数 1306 浏览 5 评论 0原文

我想使用 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

这样做时,它不起作用

cmdparsing.exe --create "this is arg1" "this is arg2"

但是当我从 Windows 命令行中 。对于第二个选项,它在 createOptions 向量中转换为 ["this" "is" "arg1" "this" "is" "arg2"] 。因此,val1 获取 "this" 并且 val2 获取 “is” 分别代替 “这是 arg1”“这是 arg2”

我如何使用 boost::program_option 来完成这项工作?

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 -

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 not work 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.

How can I use boost::program_option to make this work ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青朷 2024-10-07 09:24:36

我使用本机 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

I fixed it using a native Windows function which handles command line arguments differently. See CommandLineToArgvW for 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.

#ifdef _WIN32
    argv = CommandLineToArgvW(GetCommandLineW(), &argc);
    if (NULL == argv)
    {
        std::wcout << L"CommandLineToArgvw failed" << std::endl;
        return -1;
    }
#endif
故人爱我别走 2024-10-07 09:24:36

您应该能够通过位置选项来实现此目的:

positional_options_description pos_desc;
pos_desc.add("create", 10); // Force a max of 10.

然后在解析命令行时添加以下 pos_desc:

using namespace boost::program_options;
command_line_parser parser{argc, argv};
parser.options(desc).positional(pos_desc);
store(parser.run(), vm);

You should be able to achieve this with 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:

using namespace boost::program_options;
command_line_parser parser{argc, argv};
parser.options(desc).positional(pos_desc);
store(parser.run(), vm);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文