From: Christophe Prud'homme (prudhomm_at_[hidden])
Date: 2005-10-27 05:15:14
[ Jeudi 27 Octobre 2005 11:30 ]
| Hi Christophe,
| > I could not find in the doc whether it is possible to have unknown
| > options in the command line and tell the parsers to not worry about them.
| > If it is already a feature, then perhaps it should be documented.
| > Otherwise I think it could be a nice addition.
| > the rationale is that there are framework/libraries out there with there
| > own command line options : for example MPI and PETSC
| Hmm.. it looks like there's some bad fate with this feature. It was already
| present in 1.32, but it was hidden deep inside private interfaces, and so
| not usable. For 1.33, I did some refactoring to fix this but looking again,
| it looks like it's still not usable, because I privately derive from the
| class implementing the relevant method :-(
| I've now fixes that, added a test, and even a documentation. It's in CVS
| HEAD, and I'll merge it to 1.33.1 branch soon.
| Here's the extract from the documentation:
| Allowing unknown options
| Usually, the library throws an exception on unknown option names.
Yeah what I do now is catch the exception to be able to pass options to
MPI(message passing interface)
| behaviour can be changed. For example, only some part of your
| application uses Program_options, and you wish to pass unrecognized options
| to another part of your program, or even another application.
Yup exactly (third party libraries also: mpi, petsc,...)
| To allows unregistered options on the command line, you need to use the
| basic_command_line_parser class for parsing (not parse_command_line)
| and call the allow_unregistered method of that class:
| parsed_options parsed =
| command_line_parser(argv, argc).options(desc).
Good that it is not the default behaviour !
| For each token that looks like an option, but does not have a known
| name, an instance of basic_option will be added to the result. The
| string_key and value fields of the instance will contain results of
| syntactic parsing of the token, the unregistered field will be set to true,
| and the original_tokens field will contain the token as it appeared on the
| command line.
| If you want to pass the unrecognized options further, the
| collect_unrecognized function can be used. The function will collect
| original tokens for all unrecognized values, and optionally, all found
| positional options. Say, if your code handles a few options, but does
| not handles positional options at all, you can use the function like this:
| vector<string> to_pass_further = collect_arguments(parsed.option,
I think that's very convenient. A lot of libraries which get initialized with
command line options take the usual (int argc, char** argv)
for example., if I understand correctly, with your changes we have
int main(int argc,char** argv)
// do the usual program_options stuff
// get further(unregistered) options to pass
char** unreg_argv = transform_to_charstarstar( to_pass_further );
Thank you for this very convenient addition, that adds good flexibility to
program_options in the presence of alien systems :)
-- MIT Affiliate Debian/GNU/Linux developer for scientific computing packages fingerprint = 3703 50DE 7A9F 024E 0F26 0D07 A18F B40B D4BE 1450
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk