Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(47)

Delta Between Two Patch Sets: Doc/library/argparse.rst

Issue 14191: argparse: nargs='*' doesn't get out-of-order positional parameters
Left Patch Set: Created 6 years, 11 months ago
Right Patch Set: Created 6 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | Lib/argparse.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 :mod:`argparse` --- Parser for command-line options, arguments and sub-commands 1 :mod:`argparse` --- Parser for command-line options, arguments and sub-commands
2 =============================================================================== 2 ===============================================================================
3 3
4 .. module:: argparse 4 .. module:: argparse
5 :synopsis: Command-line option and argument parsing library. 5 :synopsis: Command-line option and argument parsing library.
6 .. moduleauthor:: Steven Bethard <steven.bethard@gmail.com> 6 .. moduleauthor:: Steven Bethard <steven.bethard@gmail.com>
7 .. sectionauthor:: Steven Bethard <steven.bethard@gmail.com> 7 .. sectionauthor:: Steven Bethard <steven.bethard@gmail.com>
8 8
9 .. versionadded:: 3.2 9 .. versionadded:: 3.2
10 10
(...skipping 1847 matching lines...) Expand 10 before | Expand all | Expand 10 after
1858 .. method:: ArgumentParser.exit(status=0, message=None) 1858 .. method:: ArgumentParser.exit(status=0, message=None)
1859 1859
1860 This method terminates the program, exiting with the specified *status* 1860 This method terminates the program, exiting with the specified *status*
1861 and, if given, it prints a *message* before that. 1861 and, if given, it prints a *message* before that.
1862 1862
1863 .. method:: ArgumentParser.error(message) 1863 .. method:: ArgumentParser.error(message)
1864 1864
1865 This method prints a usage message including the *message* to the 1865 This method prints a usage message including the *message* to the
1866 standard error and terminates the program with a status code of 2. 1866 standard error and terminates the program with a status code of 2.
1867 1867
1868
1869 Intermixed parsing
1870 ^^^^^^^^^^^^^^^^^^
1871
1872 .. method:: ArgumentParser.parse_intermixed_args(args=None, namespace=None)
1873 .. method:: ArgumentParser.parse_known_intermixed_args(args=None, namespace=None )
1874
1875 Some users expect to freely intermix optional and positional argument strings. F or
1876 example, :mod:`optparse`, by default, allows interspersed argument strings.
1877 GNU :c:func:`getopt`
1878 permutes the argument strings so non-options are at the end.
1879 The :meth:`~ArgumentParser.parse_intermixed_args` method emulates this behavior
1880 by first calling :meth:`~ArgumentParser.parse_known_args` with just the
1881 optional arguments being active. It is then called a second time to parse the l ist
1882 of remaining argument strings using the positional arguments.
1883
1884 :meth:`~ArgumentParser.parse_intermixed_args` raises an error if the
1885 parser uses features that are incompatible with this two step parsing.
1886 These include subparsers, ``argparse.REMAINDER``, and mutually exclusive
1887 groups that include both optionals and positionals.
1888
1889 In this example, :meth:`~ArgumentParser.parse_known_args` returns an unparsed
1890 list of arguments `['2', '3']`, while :meth:`~ArgumentParser.parse_intermixed_ar gs`
1891 returns `rest=[1, 2, 3]`.
1892 ::
1893
1894 >>> parser = argparse.ArgumentParser()
1895 >>> parser.add_argument('--foo')
1896 >>> parser.add_argument('cmd')
1897 >>> parser.add_argument('rest', nargs='*', type=int)
1898 >>> parser.parse_known_args('cmd1 1 --foo bar 2 3'.split())
1899 (Namespace(cmd='cmd1', foo='bar', rest=[1]), ['2', '3'])
1900 >>> parser.parse_intermixed_args('cmd1 1 --foo bar 2 3'.split())
1901 Namespace(cmd='cmd1', foo='bar', rest=[1, 2, 3])
1902
1903 :meth:`~ArgumentParser.parse_known_intermixed_args` method, returns a
1904 two item tuple containing the populated namespace and the list of
1905 remaining argument strings. :meth:`~ArgumentParser.parse_intermixed_args`
1906 raises an error if there are any remaining unparsed argument strings.
1907
1868 .. _upgrading-optparse-code: 1908 .. _upgrading-optparse-code:
1869 1909
1870 Upgrading optparse code 1910 Upgrading optparse code
1871 ----------------------- 1911 -----------------------
1872 1912
1873 Originally, the :mod:`argparse` module had attempted to maintain compatibility 1913 Originally, the :mod:`argparse` module had attempted to maintain compatibility
1874 with :mod:`optparse`. However, :mod:`optparse` was difficult to extend 1914 with :mod:`optparse`. However, :mod:`optparse` was difficult to extend
1875 transparently, particularly with the changes required to support the new 1915 transparently, particularly with the changes required to support the new
1876 ``nargs=`` specifiers and better usage messages. When most everything in 1916 ``nargs=`` specifiers and better usage messages. When most everything in
1877 :mod:`optparse` had either been copy-pasted over or monkey-patched, it no 1917 :mod:`optparse` had either been copy-pasted over or monkey-patched, it no
(...skipping 18 matching lines...) Expand all
1896 * Replace :class:`optparse.Values` with :class:`Namespace` and 1936 * Replace :class:`optparse.Values` with :class:`Namespace` and
1897 :exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with 1937 :exc:`optparse.OptionError` and :exc:`optparse.OptionValueError` with
1898 :exc:`ArgumentError`. 1938 :exc:`ArgumentError`.
1899 1939
1900 * Replace strings with implicit arguments such as ``%default`` or ``%prog`` with 1940 * Replace strings with implicit arguments such as ``%default`` or ``%prog`` with
1901 the standard Python syntax to use dictionaries to format strings, that is, 1941 the standard Python syntax to use dictionaries to format strings, that is,
1902 ``%(default)s`` and ``%(prog)s``. 1942 ``%(default)s`` and ``%(prog)s``.
1903 1943
1904 * Replace the OptionParser constructor ``version`` argument with a call to 1944 * Replace the OptionParser constructor ``version`` argument with a call to
1905 ``parser.add_argument('--version', action='version', version='<the version>')` ` 1945 ``parser.add_argument('--version', action='version', version='<the version>')` `
1946
1947 * Use :meth:`~ArgumentParser.parse_intermixed_args` if
1948 interspersing switches with command arguments is important.
LEFTRIGHT
« no previous file | Lib/argparse.py » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+