classification
Title: argparse: options starting with -- match substrings
Type: Stage:
Components: Extension Modules Versions: Python 3.2, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: david.caro, eric.araujo
Priority: normal Keywords:

Created on 2011-01-22 12:49 by david.caro, last changed 2011-01-26 23:22 by david.caro. This issue is now closed.

Messages (4)
msg126833 - (view) Author: David Caro (david.caro) Date: 2011-01-22 12:49
When parsing option like --optionname, --option will match it too

example:

>>> import argparse
>>>  parser = argparse.ArgumentParser()
>>> 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')

I'm using argparse 1.1 with python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39), on ubuntu 10.10 32bit
msg126841 - (view) Author: David Caro (david.caro) Date: 2011-01-22 15:02
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
msg127151 - (view) Author: √Čric Araujo (eric.araujo) * (Python committer) Date: 2011-01-26 22:43
Is this behavior documented?
msg127155 - (view) Author: David Caro (david.caro) Date: 2011-01-26 23:22
Yes it is, here http://docs.python.org/dev/library/argparse.html#argument-abbreviations.
History
Date User Action Args
2011-01-26 23:22:52david.carosetmessages: + msg127155
2011-01-26 22:43:14eric.araujosetnosy: + eric.araujo

messages: + msg127151
versions: + Python 2.7, Python 3.2, - Python 2.6
2011-01-22 15:02:47david.carosetstatus: open -> closed

messages: + msg126841
resolution: not a bug
2011-01-22 12:53:12david.carosettitle: options starting with -- match substrings -> argparse: options starting with -- match substrings
2011-01-22 12:51:01david.carosetcomponents: + Extension Modules
versions: + Python 2.6
2011-01-22 12:49:08david.carocreate