diff options
author | R. David Murray <rdmurray@bitdance.com> | 2017-09-07 00:25:40 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-07 00:25:40 (GMT) |
commit | 0f6b9d230674da784ca79a0cf1a03d2af5a8b6a8 (patch) | |
tree | be36691f027da9af59e6ae400b7608a5f28794e8 /Doc/library | |
parent | ad0ffa033ea79f7c7cb14b1b1cc10888ea9e9913 (diff) | |
download | cpython-0f6b9d230674da784ca79a0cf1a03d2af5a8b6a8.zip cpython-0f6b9d230674da784ca79a0cf1a03d2af5a8b6a8.tar.gz cpython-0f6b9d230674da784ca79a0cf1a03d2af5a8b6a8.tar.bz2 |
bpo-14191 Add parse_intermixed_args. (#3319)
This adds support for parsing a command line where options and positionals are intermixed as is common in many unix commands. This is paul.j3's patch with a few tweaks.
Diffstat (limited to 'Doc/library')
-rw-r--r-- | Doc/library/argparse.rst | 44 |
1 files changed, 41 insertions, 3 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst index a16aa10..ab4bc92 100644 --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -1985,6 +1985,45 @@ Exiting methods This method prints a usage message including the *message* to the standard error and terminates the program with a status code of 2. + +Intermixed parsing +^^^^^^^^^^^^^^^^^^ + +.. method:: ArgumentParser.parse_intermixed_args(args=None, namespace=None) +.. method:: ArgumentParser.parse_known_intermixed_args(args=None, namespace=None) + +A number of Unix commands allow the user to intermix optional arguments with +positional arguments. The :meth:`~ArgumentParser.parse_intermixed_args` +and :meth:`~ArgumentParser.parse_known_intermixed_args` methods +support this parsing style. + +These parsers do not support all the argparse features, and will raise +exceptions if unsupported features are used. In particular, subparsers, +``argparse.REMAINDER``, and mutually exclusive groups that include both +optionals and positionals are not supported. + +The following example shows the difference between +:meth:`~ArgumentParser.parse_known_args` and +:meth:`~ArgumentParser.parse_intermixed_args`: the former returns ``['2', +'3']`` as unparsed arguments, while the latter collects all the positionals +into ``rest``. :: + + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('--foo') + >>> parser.add_argument('cmd') + >>> parser.add_argument('rest', nargs='*', type=int) + >>> parser.parse_known_args('doit 1 --foo bar 2 3'.split()) + (Namespace(cmd='doit', foo='bar', rest=[1]), ['2', '3']) + >>> parser.parse_intermixed_args('doit 1 --foo bar 2 3'.split()) + Namespace(cmd='doit', foo='bar', rest=[1, 2, 3]) + +:meth:`~ArgumentParser.parse_known_intermixed_args` returns a two item tuple +containing the populated namespace and the list of remaining argument strings. +:meth:`~ArgumentParser.parse_intermixed_args` raises an error if there are any +remaining unparsed argument strings. + +.. versionadded:: 3.7 + .. _upgrading-optparse-code: Upgrading optparse code @@ -2018,9 +2057,8 @@ A partial upgrade path from :mod:`optparse` to :mod:`argparse`: called ``options``, now in the :mod:`argparse` context is called ``args``. * Replace :meth:`optparse.OptionParser.disable_interspersed_args` - by setting ``nargs`` of a positional argument to `argparse.REMAINDER`_, or - use :meth:`~ArgumentParser.parse_known_args` to collect unparsed argument - strings in a separate list. + by using :meth:`~ArgumentParser.parse_intermixed_args` instead of + :meth:`~ArgumentParser.parse_args`. * Replace callback actions and the ``callback_*`` keyword arguments with ``type`` or ``action`` arguments. |