[program_options] allowing unknown options (from third party libraries)

Dear Boosters, 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 Best regards C. -- MIT Affiliate Debian/GNU/Linux developer for scientific computing packages fingerprint = 3703 50DE 7A9F 024E 0F26 0D07 A18F B40B D4BE 1450

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. This 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. 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). allow_unregistered().run(); 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, include_positional); - Volodya

Hi Vladimir, [ 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. super ! | 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) | This | 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). | allow_unregistered().run(); 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, | include_positional); 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 unreg_argc= to_pass_further.size(); char** unreg_argv = transform_to_charstarstar( to_pass_further ); MPI_Init(unreg_argc,unreg_argv); PETSCInitialize(unreg_argc,unreg_argv); } Thank you for this very convenient addition, that adds good flexibility to program_options in the presence of alien systems :) Best regards C. -- MIT Affiliate Debian/GNU/Linux developer for scientific computing packages fingerprint = 3703 50DE 7A9F 024E 0F26 0D07 A18F B40B D4BE 1450

Hi Christophe,
| vector<string> to_pass_further = collect_arguments(parsed.option, | include_positional);
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 unreg_argc= to_pass_further.size(); char** unreg_argv = transform_to_charstarstar( to_pass_further );
One should take care to pass argv[0] as well, as program_options just ignores it and it's not included in the result of "collect_arguments". Otherwise, yes, this should work. - Volodya
participants (2)
-
Christophe Prud'homme
-
Vladimir Prus