Message188609
This is the formal patch corresponding to the `test_intermixed.py`. It includes changes to `argparse.rst`, plus tests in `test_argparse.py`. These tests are near the end, after those for `parse_known_args`. They are roughly equivalent to the examples in `test_intermixed.py`.
-----------------
The new documentation section is:
Some users expect to freely intermix optional and positional argument strings. For example, optparse, by default, allows interspersed argument strings. GNU getopt() permutes the argument strings so non-options are at the end. The parse_intermixed_args() method emulates this behavior by first calling parse_known_args() with just the optional arguments being active. It is then called a second time to parse the list of remaining argument strings using the positional arguments.
parse_intermixed_args() raises an error if the parser uses features that are incompatible with this two step parsing. These include subparsers, argparse.REMAINDER, and mutually exclusive groups that include both optionals and positionals.
In this example, parse_known_args() returns an unparsed list of arguments [‘2’, ‘3’], while parse_intermixed_args() returns rest=[1, 2, 3].
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo')
>>> parser.add_argument('cmd')
>>> parser.add_argument('rest', nargs='*', type=int)
>>> parser.parse_known_args('cmd1 1 --foo bar 2 3'.split())
(Namespace(cmd='cmd1', foo='bar', rest=[1]), ['2', '3'])
>>> parser.parse_intermixed_args('cmd1 1 --foo bar 2 3'.split())
Namespace(cmd='cmd1', foo='bar', rest=[1, 2, 3])
parse_known_intermixed_args() method, returns a two item tuple containing the populated namespace and the list of remaining argument strings. parse_intermixed_args() raises an error if there are any remaining unparsed argument strings. |
|
Date |
User |
Action |
Args |
2013-05-06 22:51:32 | paul.j3 | set | recipients:
+ paul.j3, amcnabb, bethard, v+python, r.david.murray, docs@python, guilherme-pg |
2013-05-06 22:51:32 | paul.j3 | set | messageid: <1367880692.77.0.168236530924.issue14191@psf.upfronthosting.co.za> |
2013-05-06 22:51:32 | paul.j3 | link | issue14191 messages |
2013-05-06 22:51:32 | paul.j3 | create | |
|