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.
Created on 2011年03月29日 04:21 by pwil3058, last changed 2022年04月11日 14:57 by admin.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| regroup.patch | paul.j3, 2014年04月25日 05:22 | review | ||
| test_regroup.py | paul.j3, 2014年04月25日 05:25 | |||
| Messages (6) | |||
|---|---|---|---|
| msg132462 - (view) | Author: Peter Williams (pwil3058) | Date: 2011年03月29日 04:21 | |
At present, if a number (e.g. 2) of optional positional arguments are defined (e.g. arg1 and arg2) argparse formats them as follows: usage: program [arg1] [arg2] in the usage message. I would like to suggest that a better format would be: usage: program [arg1 [arg2]] as this more accurately reflects the way argparse treats them during parsing of the command line. This formatting would be consistent with the way argparse currently formats a single optional positional argument with nargs='*', namely: usage: program [arg [arg ...]] |
|||
| msg141050 - (view) | Author: Petri Lehtinen (petri.lehtinen) * (Python committer) | Date: 2011年07月24日 18:22 | |
I think this is a good idea, and seems to me more like a bug than a feature request. |
|||
| msg149543 - (view) | Author: Steven Bethard (bethard) * (Python committer) | Date: 2011年12月15日 12:23 | |
I agree that this is a bug in current argparse formatting. It might be a little difficult to fix though because the current option formatting handles arguments one at a time, and producing something like [arg1 [arg2]] requires some understanding of how arguments interact, which the formatter currently doesn't have. But patches are certainly welcome. |
|||
| msg198038 - (view) | Author: paul j3 (paul.j3) * (Python triager) | Date: 2013年09月19日 01:40 | |
This is a HelpFormatter function that takes a list of formatted actions, and groups contiguous blocks of optional positional actions. It accounts for optionals (via prefix_chars) and mutually exclusive groups. Since it assumes 'parts' is a list, rather than string, it works best with the '_format_actions_usage' method in the patch that I submitted to http://bugs.python.org/issue11874 def _positionals_regroup(self, parts): # change '[arg1] [arg2]' to '[arg1 [arg2]]' # apply to a list of formatted arguments # don't apply to optionals (with prefix chars) or groups chars = getattr(self, 'prefix_chars',set('-')) R = _re.compile(r'\] \[(.*)\]') result = [] text = None while parts: part = parts.pop() if part: if part[0]=='[' and part[1] not in chars and '|' not in part: if text: text = ' '.join([part, text]) else: text = part if R.search(text): text = R.sub(' [\g<1>]]',text) else: if text: result.insert(0,text) text = None result.insert(0,part) if text: result.insert(0,text) return result To avoid compatibility issues it could implemented in a subclassed HelpFormatter. |
|||
| msg217153 - (view) | Author: paul j3 (paul.j3) * (Python triager) | Date: 2014年04月25日 05:22 | |
This patch adds a ReGroupHelpFormatter class, which regroups positional arguments in the usage line as discussed before. It builds on the reworked usage formatter in bugs/python.org/issue11874 (which keeps usage as a list longer). For a complicate parser, usage may look like: usage: regp [-h] foo [arg1 [arg2 [arg3 [arg3 ...] [arg4]]]] |
|||
| msg217154 - (view) | Author: paul j3 (paul.j3) * (Python triager) | Date: 2014年04月25日 05:25 | |
This is a testing script for this patch. It is not a unit test.
Example:
p = argparse.ArgumentParser()
p.formatter_class = argparse.ReGroupHelpFormatter
p.add_argument('foo')
p.add_argument('arg1',nargs='?')
p.add_argument('arg2',nargs='?')
a = p.add_argument('arg3',nargs='*')
b = p.add_argument('arg4', nargs='?')
# usage: regp [-h] foo [arg1 [arg2 [arg3 [arg3 ...] [arg4]]]]
|
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:15 | admin | set | github: 55917 |
| 2021年12月10日 15:50:50 | iritkatriel | set | type: enhancement versions: + Python 3.11, - Python 2.7, Python 3.2, Python 3.3 |
| 2014年04月25日 05:25:45 | paul.j3 | set | files:
+ test_regroup.py messages: + msg217154 |
| 2014年04月25日 05:22:17 | paul.j3 | set | files:
+ regroup.patch keywords: + patch messages: + msg217153 |
| 2013年09月19日 01:40:57 | paul.j3 | set | messages: + msg198038 |
| 2013年09月16日 06:46:39 | paul.j3 | set | nosy:
+ paul.j3 |
| 2011年12月15日 12:23:01 | bethard | set | messages: + msg149543 |
| 2011年07月24日 18:22:15 | petri.lehtinen | set | type: enhancement -> (no value) stage: needs patch messages: + msg141050 versions: + Python 3.2, Python 3.3 |
| 2011年06月21日 11:27:30 | petri.lehtinen | set | nosy:
+ bethard |
| 2011年06月21日 11:27:15 | petri.lehtinen | set | nosy:
+ petri.lehtinen |
| 2011年03月29日 04:21:10 | pwil3058 | create | |