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")