argparse uses per default abbreviation in unambiguous cases.
I don't want abbreviation and I'd like to disable it. But didn't find it in the documentation.
Is it possible?
Example:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--send', action='store_true')
parser.parse_args(['--se']) # returns Namespace(send=True)
But I want it only to be true when the full parameter is supplied. To prevent user errors.
UPDATE:
I created a ticket at python bugtracker after Vikas answer. And it already has been processed.
No, apparently this is not possible. At least in Python 2.7.2.
First, I took a look into the documentation - to no avail.
Then I opened the Lib\argparse.py and looked through the source code. Omitting a lot of details, it seems that each argument is parsed by a regular expression like this (argparse:2152):
This regex will successfully parse both '-' and '--', so we have no control over the short and long arguments. Other regexes use the -* construct too, so it does not depend on the type of the parameter (no sub-arguments, 1 sub-argument etc).
Later in the code double dashes are converted to one dash (only for non-optional args), again, without any flags to control by user:
No, well not without ugly hacks.
The code snippet @Vladimir posted, i suppose that is not what you are looking for. The actual code that is doing this is:
See the check is
startswith
not==
.And you can always extend
argparse.ArgumentParser
to provide your own_get_option_tuples(self, option_string)
to change this behavior. I just did by replacing two occurrence ofoption_string.startswith(option_prefix)
tooption_string == option_prefix
and:A word of caution
The method
_get_option_tuples
is prefixed with_
, which typically means a private method in python. And it is not a good idea to override a private.Another way for Python 2.7. Let's get clunky! Say you want to recognize
--dog
without abbreviation.By adding a second argument
--dox
that differs from the argument you want only in the third letter,--d
and--do
become ambiguous. Therefore, the parser will refuse to recognize them. You would need to add code to catch the resulting exception and process it according to the context in which you are callingparse_args
. You might also need to suppress/tweak the help text.The
help=...
keeps the argument out of the option list on the default help message (per this), andmetavar='IGNORE'
is just to make it clear you really aren't doing anything with this option :) .As of Python 3.5.0 you can disable abbreviations by initiating the ArgumentParser with the following:
Also see the documentation.