[issue10981] argparse: options starting with -- match substrings

David Caro report at bugs.python.org
Sat Jan 22 16:02:53 CET 2011


David Caro <david.caro.estevez at gmail.com> added the comment:

It is not an issue, it will try to match all the optional parameters, and if only one matches, then it will use it:

2110                 elif option_string.startswith(option_prefix):
2111                     action = self._option_string_actions[option_string]
2112                     tup = action, option_string, explicit_arg
2113                     result.append(tup)

and

2057         # if exactly one action matched, this segmentation is good,
2058         # so return the parsed action
2059         elif len(option_tuples) == 1:
2060             option_tuple, = option_tuples
2061             return option_tuple


if you try to add more than one optional parameter that matches the substring, it will complain:

>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--superstring')
_StoreAction(option_strings=['--superstring'], dest='superstring', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args(['--super','value'])
Namespace(superstring='value')
>>> parser.add_argument('--superstring2')
_StoreAction(option_strings=['--superstring'], dest='superstring', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> parser.parse_args(['--super','value'])
usage: [-h] [--superstring SUPERSTRING] [--superstring2 SUPERSTRING2]
: error: ambiguous option: --super could match --superstring, --superstring2

----------
resolution:  -> invalid
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10981>
_______________________________________


More information about the Python-bugs-list mailing list