views:

28

answers:

1

Is there a way to collect all of the values after a specified argument with boost::program_options? There are two caveats that I need to take care of though, I need to accept unrecognized arguments, and I need to accept values that could contain dashes. I've tried playing around with command_line_parser vs parse_command_line and I can get either unrecognized or values that contain dashes, but not both.

Example: ./myprog Ignore1 Ignore2 --Accept 1 --AlsoAccept 2 --AcceptAll 1 2 -3 4

I'm not really concerned with verifying that --AcceptAll is the last flag passed; I'm just looking for logic that returns a vector of strings for everything after that flag.

A: 

have you tried positional options?

#include <boost/program_options.hpp>

#include <boost/foreach.hpp>

#include <iostream>
#include <string>

namespace po = boost::program_options;

int
main( unsigned int argc, char** argv )
{
    std::string foo;
    std::vector<std::string> extra;
    po::options_description desc;
    desc.add_options()
        ("foo", po::value<std::string>(&foo), "some string")
        ("extra-options", po::value(&extra), "extra args" )
        ;

    po::positional_options_description p;
    p.add("extra-options", -1);

    po::variables_map vm;
    po::store(
            po::command_line_parser(argc, argv).
            options(desc).
            positional(p).
            run(),
            vm);
    po::notify(vm);

    BOOST_FOREACH( const std::string& i, extra ) {
        std::cout << i << std::endl;
    }

    return 0;
}

sample session

samm@macmini ~> ./a.out --foo bar far hello world how are you
far
hello
world
how
are
you
samm@macmini ~>
Sam Miller
That solution doesn't allow me to ignore arguments nor does it allow arguments with a '-'.
sfpiano

related questions