简体   繁体   中英

boost program options - append multitoken options from config file to command-line

Using boost program options I am trying to allow the user to set default values for a multitoken argument in a config file (.ini) that will append to their command-line selections.

Example:

Program Options:

m_Desc.add_options()
    ("settings,s", po::value<string>("FILE")->default_value("~/.config.ini")->multitoken(), "Settings")
    ("tax,t", po::value<vector<string>>("name|rate")->multitoken(), "Tax")
;

try {

    po::store(
        po::command_line_parser(argc, argv).
        options(m_Desc).
        positional(m_Pos).
        run(),
        m_Vm);

    ifstream config(m_Vm["settings"].as<string>(), ifstream::in);

    if(config) {
        po::store(
            po::parse_config_file(config, m_Desc),
            m_Vm);

    }

    if (m_Vm.count("help")) {
        Usage();
        return;
    }

    po::notify(m_Vm);

} catch(const po::error &e) {
    throw MyCustomException(e.what());
}

User Config :

// config.ini
tax = gst|7
tax = vat|5

// What happens:
$ ./a.out --tax another|3
Tax:
another|3

$ ./a.out
Tax:
gst|7
vat|5

// What I'd like:
$ ./a.out --tax another|3
Tax:
gst|7
another|3
vat|5

$ ./a.out
Tax:
gst|7
vat|5

How can I customize boost PO to merge multitoken options rather than overwrite?

I've tried storing the options from the command-line and config file in separate variable maps, and then merging, but that became an issue with my other command-line options.

the value function you're looking for is ->composing() :

("settings,s", 
    po::value<string>("FILE")->default_value("~/.config.ini")->multitoken()->composing(), 
    "Settings")

("tax,t", 
    po::value<vector<string>>("name|rate")->multitoken()->composing(), 
    "Tax")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM