[issue11708] argparse: suggestion for formatting optional positional args
paul j3
report at bugs.python.org
Thu Sep 19 03:40:57 CEST 2013
paul j3 added the comment:
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.
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11708>
_______________________________________
More information about the Python-bugs-list
mailing list