Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost program options - how to conditionally type positional args?

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);
like image 931
Andrew B. Avatar asked Dec 06 '25 03:12

Andrew B.


1 Answers

Always read them as strings and do some post-processing depending on the other options.

like image 90
wilx Avatar answered Dec 09 '25 19:12

wilx