Search code examples
c++boostcommand-line-argumentsboost-program-options

How to use boost program_options to consume/remove an option in ARGV after usage


Currently, our codebase is using boost program_options as command-line parser in our testing framework. I want to implement a "consume-after-use" kind of rule in our command-line parsing.

What I mean by "consume-after-use" is, for example. After --option1 is used, I can then remove this --option1 in ARGC, ARGV. The main reason for this is I want to pass ARGC, and ARGV in another parser after I use the options I'm only concerned with.

Is this possible in boost program_option?


Solution

  • As you can read here the boost::program_options::variables_map inherites from std::map, so you can call std::map:erase on it:

    namespace po = boost::program_options;
    
    po::variables_map vm;
    po::store(po::parse_command_line(ac, av, desc), vm);
    po::notify(vm);
    
    if (vm.count("option1")) {
        // handle option1
        ...
        // erase option1
        vm.erase("option1");
        if (vm.count("option1")) {
          // never gonna happen
        }
    }