Ppton optparse usually allows the user to specify a parameter more than once and silently ignores all occurrences of the option except the last one. For example, if the action of the --foo option is store and the action of the --flag option is store_const , store_true or store_false , the following commands will be equivalent:
my-command --foo=bar --foo=another --flag --foo=last --flag my-command --flag --foo=last
(Update: argparse does the same thing by default.)
Now I have many options, and defining any of them more than once does not make sense. If the user specifies the same option more than once, I would like to warn them of a possible error.
What is the most elegant way of defining options that have been specified multiple times? Note that the same option can have a short form, a long form, and shortened long forms (so -f , --foobar , --foob and --foo are all the same option). It would be even better if it were possible to detect the case when several parameters having the same destination were specified at the same time, so that a warning could be given if the user indicates both --quiet and --verbose , while while both parameters store the value at the same destination and effectively overlap each other.
Update. To be more convenient, the warning should refer to the exact names of the options that are used on the command line. Using append actions instead of store possible, but when we detect a conflict, we cannot say which parameters caused it (were they -q and --verbose or --quiet --quiet ?).
Unfortunately, I am stuck with optparse and cannot use argparse because I need to support Python 2.6.
P. S. If you know a solution that only works with argparse, submit it too. Although I'm trying to minimize the number of external dependencies, using argparse under Python 2.6 is still an option.