0

I'm attempting to get all of the input given in the commandline after a certain flag, it seems that boost delimits after a space, and I'm trying to get more than one piece of data for the given command.

For example given the command ./Program -n Name -t Type -a key=value key=value key=value

Name, and Type will be stored correctly, but the series of key=values will not, only the first one will be stored, while I want to store all of them. Instead of having a string result of just key=value I want key=value key=value key=value stored in that string.

Is there a known way of allowing boost to store a string that is not delimited by a space. I cannot post my exact code, but it looks very similar to this example:

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

using namespace boost::program_options;

void on_age(int age)
{
  std::cout << "On age: " << age << '\n';
}

int main(int argc, const char *argv[])
{
  try
  {
    options_description desc{"Options"};
    desc.add_options()
      ("help,h", "Help screen")
      ("name,n", value<string>(), "name")
      ("type,t", value<string>(), "type")
      ("args,a", value<string>(), "args");

    variables_map vm;
    store(parse_command_line(argc, argv, desc), vm);
    notify(vm);

    if (vm.count("help"))
      std::cout << desc << '\n';
    if (vm.count("name"))
      std::cout << "Name: " << vm["name"].as<string>() << '\n';
    if (vm.count("type"))
      std::cout << "Type: " << vm["type"].as<string>() << '\n';
    if (vm.count("args"))
      std::cout << "Args: " << vm["args"].as<string>() << '\n';

  }
  catch (const error &ex)
  {
    std::cerr << ex.what() << '\n';
  }
}

What modifications can I make to my code that will allow the program options to store until either the next flag or the end of the input. This will generally be the last flag called as well.

clbx
  • 154
  • 2
  • 15
  • Shouldn't `("args,a", value(), "args");` rather be `("args,a", value>(), "args");` or something? – πάντα ῥεῖ Jul 13 '18 at 15:39
  • I can give that a try, the way I currently have it set up to process that relies on a single string, but I'll see what happens – clbx Jul 13 '18 at 15:43
  • Subtle point, but the arguments were already parsed by the shell before the application sees them. Boost Program Options does additional parsing, but I'm not sure if it will join arguments back together. – Eljay Jul 13 '18 at 15:46
  • Another option might be to use unnamed options like with list of input files example. – πάντα ῥεῖ Jul 13 '18 at 15:47
  • @πάνταῥεῖ I like where you are going with using the vector, it seems much simpler and a much better solution than the other post that you added as a duplicate, I can certaintly try that out, but I would prefer to try another route to have another option to solve this problem. with that in mind, using the vector in the collection of the command seems to work, but I am at a problem with then how to display it as `vm["args"].as()` is no longer valid and it does not seem that boost will take `vm["args"].as>()` do you know of any other way to express this? – clbx Jul 13 '18 at 18:10
  • @clbx I believe that this duplicate link points you to everything you need to realize that _`("args,a", value>(), "args");`_ proposal I made. – πάντα ῥεῖ Jul 13 '18 at 18:13
  • @πάνταῥεῖ I see, it doesn't seem this will be possible without a drastic change of a large portion of my code, thank you for your help. – clbx Jul 13 '18 at 18:18

0 Answers0