Hello,
The same apply with optional placeholders.
I wanted to have an option with an optional argument:
- if the option is not specified, do not use it,
- if the option is specified without the optional argument, then use a
default value define somewhere[1],
- if the option is specified with the optional argument, use that
optional argument.
I think it should looks like this:
#v+ imaginary exemples
./test-optional-placeholders.pl
option2 is 'value2'
./test-optional-placeholders.pl --enable-opt2
option2 is 'value2'
./test-optional-placeholders.pl --enable-opt2 'different value 2'
option2 is 'different value 2'
./test-optional-placeholders.pl --enable-opt1
option1 is 'default value 1'
option2 is 'value2'
./test-optional-placeholders.pl --enable-opt1 'different value 1'
option1 is 'different value 1'
option2 is 'value2'
./test-optional-placeholders.pl --enable-opt1 'different value 1' --enable-opt2
option1 is 'different value 1'
option2 is 'value2'
./test-optional-placeholders.pl --enable-opt1 'different value 1' --enable-opt2 'different value 2'
option1 is 'different value 1'
option2 is 'different value 2'
./test-optional-placeholders.pl --enable-opt1 --enable-opt2
option1 is 'default value 1'
option2 is 'value2'
./test-optional-placeholders.pl --enable-opt1 --enable-opt2 'different value 2'
option1 is 'default value 1'
option2 is 'different value 2'
#v-
In practice, a problem arise when the optional argument to --enable-opt1
is omitted:
#v+
./test-optional-placeholders.pl --enable-opt1 --enable-opt2
option1 is '--enable-opt2'
option2 is 'value2'
./test-optional-placeholders.pl --enable-opt1 --enable-opt2 'different value 2'
option1 is '--enable-opt2''
option2 is 'different value 2'
#v-
The optional placeholder eat the next option, like with multiple
placeholders, options should be excluded.
1. An optional placeholder should not be fed by an option.
2. A multiple placeholder should stopped to be fed when an option is
encountered.
With those points, a policy on what to do with non-option single/double-dash
starting strings are encounter in that case:
#v+
./test-optional-placeholders.pl --enable-opt1 --not-an-option-defined-in-getopt-euclid --enable-opt2
#v-
- An unknown option error? I think it's the saner one, it permit to
detect typos on command lines
- option1 is '--not-an-option-defined-in-getopt-euclid' ?
Regards.
Footnotes:
[1] like at the top of the script
--
Daniel Dehennin
Récupérer ma clef GPG:
gpg --keyserver pgp.mit.edu --recv-keys 0x6A2540D1
Message body not shown because it is not plain text.