diff -r 9ea84f006892 Lib/argparse.py
--- a/Lib/argparse.py Wed May 01 15:15:50 2013 +0200
+++ b/Lib/argparse.py Tue Jul 16 11:49:43 2013 -0700
@@ -311,21 +311,15 @@
# build full usage string
format = self._format_actions_usage
- action_usage = format(optionals + positionals, groups)
- usage = ' '.join([s for s in [prog, action_usage] if s])
+ action_parts = format(optionals + positionals, groups)
+ usage = ' '.join([prog]+action_parts)
# wrap the usage parts if it's too long
text_width = self._width - self._current_indent
if len(prefix) + len(usage) > text_width:
- # break usage into wrappable parts
- part_regexp = r'\(.*?\)+|\[.*?\]+|\S+'
- opt_usage = format(optionals, groups)
- pos_usage = format(positionals, groups)
- opt_parts = _re.findall(part_regexp, opt_usage)
- pos_parts = _re.findall(part_regexp, pos_usage)
- assert ' '.join(opt_parts) == opt_usage
- assert ' '.join(pos_parts) == pos_usage
+ opt_parts = format(optionals, groups)
+ pos_parts = format(positionals, groups)
# helper for wrapping lines
def get_lines(parts, indent, prefix=None):
@@ -336,7 +330,7 @@
else:
line_len = len(indent) - 1
for part in parts:
- if line_len + 1 + len(part) > text_width:
+ if line and line_len + 1 + len(part) > text_width:
lines.append(indent + ' '.join(line))
line = []
line_len = len(indent) - 1
@@ -377,61 +371,75 @@
return '%s%s\n\n' % (prefix, usage)
def _format_actions_usage(self, actions, groups):
- # find group indices and identify actions in groups
- group_actions = set()
- inserts = {}
- for group in groups:
- try:
- start = actions.index(group._group_actions[0])
- except ValueError:
- continue
- else:
- end = start + len(group._group_actions)
- if actions[start:end] == group._group_actions:
- for action in group._group_actions:
- group_actions.add(action)
- if not group.required:
- if start in inserts:
- inserts[start] += ' ['
- else:
- inserts[start] = '['
- inserts[end] = ']'
- else:
- if start in inserts:
- inserts[start] += ' ('
- else:
- inserts[start] = '('
- inserts[end] = ')'
- for i in range(start + 1, end):
- inserts[i] = '|'
+ # format the usage using the actions list. Where possible
+ # format the groups that include those actions
+ # The actions list has priority
+ # This is a new version that formats the groups directly without
+ # needing inserts, (most) cleanup, or parsing into parts
+ parts = []
+ i = 0
+ # step through the actions list
+ while i