| CARVIEW |
Select Language
HTTP/2 200
server: nginx
date: Sun, 28 Dec 2025 05:46:10 GMT
content-type: text/html; charset=utf-8
expires: Sun, 28 Dec 2025 05:46:09 GMT
cache-control: no-cache
x-frame-options: sameorigin
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-permitted-cross-domain-policies: none
content-encoding: gzip
x-clacks-overhead: GNU Terry Pratchett
strict-transport-security: max-age=315360000; includeSubDomains; preload
Message 187051 - Python tracker
- Python Home
- About
- News
- Documentation
- Downloads
- Community
- Foundation
- Developer's Guide
- Issue Tracker
- Issues
- Summaries
- User
- Administration
- Help
Message187051
This issue tracker has been migrated to GitHub,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
| Author | paul.j3 |
|---|---|
| Recipients | amcnabb, bethard, docs@python, guilherme-pg, paul.j3, r.david.murray, v+python |
| Date | 2013-04-16.07:09:43 |
| SpamBayes Score | -1.0 |
| Marked as misclassified | Yes |
| Message-id | <1366096185.32.0.672667122425.issue14191@psf.upfronthosting.co.za> |
| In-reply-to |
| Content | |
|---|---|
This patch permits the mixing of optionals with positionals, with the caveat that a particular positional cannot be split up.
If:
parser = ArgumentParser()
parser.add_argument('-f','--foo')
parser.add_argument('cmd')
parser.add_argument('rest', nargs='*')
'-f1 cmd 1 2 3',
'cmd -f1 1 2 3',
'cmd 1 2 3 -f1'
all give {cmd='cmd', rest=['1','2','3'], foo='1'}.
But 'cmd 1 -f1 2 3', does not recognize ['2','3'].
Previously 'cmd -f1 1 2 3' would return rest=[], and not recognize ['1','2','3']. With this change the nargs='*' behaves more like nargs='+', surviving to parse the 2nd group of positional strings.
The trick is to modify arg_counts in consume_positionals(), removing matches that don't do anything (don't consume argument strings).
if 'O' in arg_strings_pattern[start_index:]:
# if there is an optional after this, remove
# 'empty' positionals from the current match
while len(arg_counts)>1 and arg_counts[-1]==0:
arg_counts = arg_counts[:-1]
This change passes all of the existing test_argparse.py tests. It also passes the optparse tests that I added in https://bugs.python.org/issue9334#msg184987
I added 4 cases to illustrate this change. |
|
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2013-04-16 07:09:45 | paul.j3 | set | recipients: + paul.j3, amcnabb, bethard, v+python, r.david.murray, docs@python, guilherme-pg |
| 2013-04-16 07:09:45 | paul.j3 | set | messageid: <1366096185.32.0.672667122425.issue14191@psf.upfronthosting.co.za> |
| 2013-04-16 07:09:45 | paul.j3 | link | issue14191 messages |
| 2013-04-16 07:09:44 | paul.j3 | create | |
