[program_options] Short program options
Hi there, a while ago I was asking some questions about the short option feature. If I'm not mistaken a short option can only be one letter long. Is that correct to say? In my project I need more than this. So I started to just add the short names to the options list like I would do for all options. But in this case they would share the same variable. Like this: po::options_description settings( "" ); settings.add_options() ( "input_1" , po::value< string >( &_input ) ->default_value( "" ) , "" ) ( "i1" , po::value< string >( &_input ) ->default_value( "" ) , "" ); Both options are pointing to the same variable. For some reasons this code works only for "input_1" and not for "i1". Why is that? If I use --i1="Hallo" the _input is empty. Regards, Christian
"Christian Henning" writes:
Hi there, a while ago I was asking some questions about the short option feature. If I'm not mistaken a short option can only be one letter long. Is that correct to say?
In my project I need more than this. So I started to just add the short names to the options list like I would do for all options. But in this case they would share the same variable. Like this:
po::options_description settings( "" ); settings.add_options() ( "input_1" , po::value< string >( &_input ) ->default_value( "" ) , "" ) ( "i1" , po::value< string >( &_input ) ->default_value( "" ) , "" );
Both options are pointing to the same variable. For some reasons this code works only for "input_1" and not for "i1". Why is that? If I use --i1="Hallo" the _input is empty.
That makes sense. The default value for 'input_1' is getting applied *after* the provided 'i1' value is applied, so it overwrites the 'i1' value. I'm not %100 sure, but you might get the right effect by initializing '_input' yourself (or leaving it uninitialized), and remove the 'default_value' settings. Then you can test the value of '_input' to see if it got set. The only problem is the usage statement won't print what the default value is, but is that something you want in this case? -Bryan
Hi Bryan and Vladimir,
I'm not %100 sure, but you might get the right effect by initializing '_input' yourself (or leaving it uninitialized), and remove the 'default_value' settings. Then you can test the value of '_input' to see if it got set. The only problem is the usage statement won't print what the default value is, but is that something you want in this case?
Thanks for the hint. I left out the default_value call and I got what I wanted. Christian
Christian Henning wrote:
Hi there, a while ago I was asking some questions about the short option feature. If I'm not mistaken a short option can only be one letter long. Is that correct to say?
Yes.
In my project I need more than this. So I started to just add the short names to the options list like I would do for all options. But in this case they would share the same variable. Like this:
po::options_description settings( "" ); settings.add_options() ( "input_1" , po::value< string >( &_input ) ->default_value( "" ) , "" ) ( "i1" , po::value< string >( &_input ) ->default_value( "" ) , "" );
Both options are pointing to the same variable. For some reasons this code works only for "input_1" and not for "i1". Why is that? If I use --i1="Hallo" the _input is empty.
Well, you specify default value for both options. So, at the end of processing, both options have values, and both values are assigned to 'input', in no particular order. Does this answer your question? - Volodya
participants (3)
-
Bryan Green
-
Christian Henning
-
Vladimir Prus