mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Issue #14910: Add allow_abbrev parameter to argparse.ArgumentParser.
Patch by Jonathan Paugh, Steven Bethard, paul j3 and Daniel Eriksson.
This commit is contained in:
parent
0fe6325acf
commit
8089cd642f
5 changed files with 95 additions and 20 deletions
|
@ -135,7 +135,7 @@ ArgumentParser objects
|
|||
formatter_class=argparse.HelpFormatter, \
|
||||
prefix_chars='-', fromfile_prefix_chars=None, \
|
||||
argument_default=None, conflict_handler='error', \
|
||||
add_help=True)
|
||||
add_help=True, allow_abbrev=True)
|
||||
|
||||
Create a new :class:`ArgumentParser` object. All parameters should be passed
|
||||
as keyword arguments. Each parameter has its own more detailed description
|
||||
|
@ -169,6 +169,12 @@ ArgumentParser objects
|
|||
|
||||
* add_help_ - Add a -h/--help option to the parser (default: ``True``)
|
||||
|
||||
* allow_abbrev_ - Allows long options to be abbreviated if the
|
||||
abbreviation is unambiguous. (default: ``True``)
|
||||
|
||||
.. versionchanged:: 3.5
|
||||
*allow_abbrev* parameter was added.
|
||||
|
||||
The following sections describe how each of these are used.
|
||||
|
||||
|
||||
|
@ -518,6 +524,26 @@ calls, we supply ``argument_default=SUPPRESS``::
|
|||
>>> parser.parse_args([])
|
||||
Namespace()
|
||||
|
||||
.. _allow_abbrev:
|
||||
|
||||
allow_abbrev
|
||||
^^^^^^^^^^^^
|
||||
|
||||
Normally, when you pass an argument list to the
|
||||
:meth:`~ArgumentParser.parse_args` method of a :class:`ArgumentParser`,
|
||||
it :ref:`recognizes abbreviations <prefix-matching>` of long options.
|
||||
|
||||
This feature can be disabled by setting ``allow_abbrev`` to ``False``::
|
||||
|
||||
>>> parser = argparse.ArgumentParser(prog='PROG', allow_abbrev=False)
|
||||
>>> parser.add_argument('--foobar', action='store_true')
|
||||
>>> parser.add_argument('--foonley', action='store_false')
|
||||
>>> parser.parse_args([--foon])
|
||||
usage: PROG [-h] [--foobar] [--foonley]
|
||||
PROG: error: unrecognized arguments: --foon
|
||||
|
||||
.. versionadded:: 3.5
|
||||
|
||||
|
||||
conflict_handler
|
||||
^^^^^^^^^^^^^^^^
|
||||
|
@ -1410,9 +1436,9 @@ argument::
|
|||
Argument abbreviations (prefix matching)
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
The :meth:`~ArgumentParser.parse_args` method allows long options to be
|
||||
abbreviated to a prefix, if the abbreviation is unambiguous (the prefix matches
|
||||
a unique option)::
|
||||
The :meth:`~ArgumentParser.parse_args` method :ref:`by default <allow_abbrev>`
|
||||
allows long options to be abbreviated to a prefix, if the abbreviation is
|
||||
unambiguous (the prefix matches a unique option)::
|
||||
|
||||
>>> parser = argparse.ArgumentParser(prog='PROG')
|
||||
>>> parser.add_argument('-bacon')
|
||||
|
@ -1426,6 +1452,7 @@ a unique option)::
|
|||
PROG: error: ambiguous option: -ba could match -badger, -bacon
|
||||
|
||||
An error is produced for arguments that could produce more than one options.
|
||||
This feature can be disabled by setting :ref:`allow_abbrev` to ``False``.
|
||||
|
||||
|
||||
Beyond ``sys.argv``
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue