c++ - 获取 boost 命令行来解析我提供的数组而不是来自 main 的 argv - 模拟 argv

标签 c++ parsing boost command-line-interface argv

请原谅我的无知,我是 c++ 的新手。

我正在尝试为我的命令行程序使用 boost program_options 和命令行解析器。该程序使用 getline 获取一些用户输入的命令。我想解析它以便将其传递给 boost 但我似乎无法正确设置类型。我仍在正确地学习指针和所有爵士乐,但我过得很艰难。

这是代码,因此您可以了解我正在尝试做什么:

namespace po = boost::program_options;

int _tmain(int argc, _TCHAR* argv[])
{
    std::string input;
    _TCHAR* parsedInput[20];

    while (std::cin) {
        std::getline(std::cin, input);
        boost::split(parsedInput, input, boost::is_any_of(" "), boost::token_compress_on);

        po::options_description desc("allowed options");
        desc.add_options()
            ("help", "produce help message")
            ("compression", po::value<int>(), "set compression level")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(5, parsedInput, desc), vm);
        po::notify(vm);

        if (vm.count("help"))
            std::cout << desc << "\n";
    }

    return 0;
}

po::store(po::parse_command_line(5, parsedInput, desc), vm); 5 是任意的,而我正试图让它工作。

对于代码,我不完全确定我的问题是什么,但我认为它实际上与拆分输入有关,这是当前的错误消息 error C2078: too many initializers

我最初将输入拆分为一个字符串 vector ,这让我克服了那个错误,但后来我遇到了 po::parse_command_line(5, parsedInput, desc) 采取错误类型的问题,我假设它不能采用字符串 vector 。查看documentation后似乎确实如此。

如果有人能帮助我或指出正确的方向,我将不胜感激。谢谢。

最佳答案

你不能拆分成一个char const*[]

相反,拆分为 std::string 的 vector 并转换为所需的 vector :

#include <boost/program_options.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>

namespace po = boost::program_options;

int main()
{
    std::string input;

    while (std::cin) {
        std::getline(std::cin, input);

        std::vector<std::string> parsedInput;
        boost::split(parsedInput, input, boost::is_any_of(" "), boost::token_compress_on);

        std::vector<char const*> args { "command" };
        for (auto& arg : parsedInput)
            args.push_back(arg.c_str());

        po::options_description desc("allowed options");
        desc.add_options()
            ("help", "produce help message")
            ("compression", po::value<int>(), "set compression level")
        ;

        po::variables_map vm;
        po::store(po::parse_command_line(args.size(), args.data(), desc), vm);
        po::notify(vm);

        if (vm.count("help"))
            std::cout << desc << "\n";
    }
}

注意第一个参数(arg[0])应该是程序名

Live On Coliru

allowed options:
  --help                produce help message
  --compression arg     set compression level

关于c++ - 获取 boost 命令行来解析我提供的数组而不是来自 main 的 argv - 模拟 argv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44379711/

相关文章:

c++ - 如何向我的 openGL 程序添加多个纹理?

c++ - union 'punning' 结构 w/ "common initial sequence": Why does C (99+), 但不是 C++,规定 'visible declaration of the union type' ?

c++ - 将 C++ 字符串解析为元组

c++ - 由于 operator= 不明确,multi_index_container 不适用于递归变体

c++ - boost::hold_any 构造函数是否有未定义的行为?

C++ - Boost : Getting same random number . 播种似乎不起作用

c++ - 促进 ASIO TCP 消息分离

c++ - Bison 中的三元运算符,避免双方计算

Java:如何严格解析日期?

c++ - 回复:使用 boost::make_transform_iterator 进行引用访问