1

Please excuse any ignorance, I'm new to c++.

I'm trying to use boost program_options and commandline parser for my command line program. The program uses getline to get some user inputted command. I want to parse that so it can be passed to boost but I can't seem to get the types right. I'm still learning pointers and all that jazz properly but I'm having a tough time.

Here's the code so you can get a idea of what I'm trying to do:

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); the 5 is arbitrary just while I'm trying to make this work.

With the code I'm not entirely sure what my issue is but I think it actually has something to do with splitting the input, this is the current error message error C2078: too many initializers.

I originally split the input into a vector of strings, which got me past that error but then I had problems with po::parse_command_line(5, parsedInput, desc) taking the wrong type, I assume it can't take a vector of strings. After looking at the documentation that does seem to be the case.

If anyone could give me a hand or point me in the right direction I would be grateful. Thank you.

user3238415
  • 188
  • 1
  • 11
  • I have no idea what you were tripping over, but `parse_command_line` does not take a `vector`. You may be better off posting what you originally attempted because this is surely a bad path. – user4581301 Jun 06 '17 at 01:13
  • Typical usage would be `parse_command_line(argc, argv, desc)`, assuming `_TCHAR` is defined to be something friendly. More reading here: [Converting _TCHAR* to char*](https://stackoverflow.com/questions/19301538/converting-tchar-to-char) – user4581301 Jun 06 '17 at 01:15

1 Answers1

1

You can't split into a char const*[].

Instead, split into a vector of std::string and transform to the required 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";
    }
}

Note that first argument (arg[0]) should be the program name

Live On Coliru

allowed options:
  --help                produce help message
  --compression arg     set compression level
sehe
  • 374,641
  • 47
  • 450
  • 633
  • @user3238415 You're welcome. Keep in mind your split doesn't deal with escapes, so you might prefer to just use constant test cases: http://coliru.stacked-crooked.com/a/61293b6a2d860702 – sehe Jun 06 '17 at 17:33