I'm using boost program options, and I want to interpret some positional arguments as either strings, or ints, based on a user specified command line switch. For example:
foo -asint outputfile 10 11 12
foo -asstr outputfile 10 11 12
would list (10,11,12) as ints in the first example and strings in the second.
I can't figure out how to do this using boost po. Here is a snippet of my command line parsing:
// basic options group
po::options_description genericOpts("allowed options");
genericOpts.add_options()
("help,h", "display help message / usage")
("asint,i", "interpret arguments ints instead of strings")
;
// hidden options group - don't show in help
po::options_description hiddenOpts("hidden options");
hiddenOpts.add_options()
("filename", po::value<string>()->required(),"output file")
("inputs", po::value<vector<string>>(), "inputs, either strings or ints")
;
po::options_description cmdline_options;
cmdline_options.add(genericOpts).add(hiddenOpts);
po::positional_options_description p;
p.add("filename",1).add("inputs", -1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).
options(cmdline_options).positional(p).run(), vm);
Always read them as strings and do some post-processing depending on the other options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With