diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-24 00:31:44 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-09-24 00:31:44 -0400 |
commit | 7587fed06026fb12a49b7f3f4deeb90af0895eb7 (patch) | |
tree | f73082da6c25b34624cf5f57710db0afd0f7f01c /cmd2/argparse_completer.py | |
parent | 502330358114fdb4cc4d21fc4277010cfc148290 (diff) | |
download | cmd2-git-7587fed06026fb12a49b7f3f4deeb90af0895eb7.tar.gz |
Placed options at beginning of usage statement like argparse does.
This is to prevent options appearing after the ... generated by argparse.REMAINDER.
Diffstat (limited to 'cmd2/argparse_completer.py')
-rwxr-xr-x | cmd2/argparse_completer.py | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 03ff4375..0c0bc6a1 100755 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -707,7 +707,7 @@ class ACHelpFormatter(argparse.RawTextHelpFormatter): def _format_usage(self, usage, actions, groups, prefix) -> str: if prefix is None: - prefix = _('Usage: ') + prefix = _('usage: ') # if usage is specified, use that if usage is not None: @@ -738,7 +738,7 @@ class ACHelpFormatter(argparse.RawTextHelpFormatter): # build full usage string format = self._format_actions_usage - action_usage = format(positionals + required_options + optionals, groups) + action_usage = format(required_options + optionals + positionals, groups) usage = ' '.join([s for s in [prog, action_usage] if s]) # wrap the usage parts if it's too long @@ -749,15 +749,15 @@ class ACHelpFormatter(argparse.RawTextHelpFormatter): # break usage into wrappable parts part_regexp = r'\(.*?\)+|\[.*?\]+|\S+' + req_usage = format(required_options, groups) opt_usage = format(optionals, groups) pos_usage = format(positionals, groups) - req_usage = format(required_options, groups) + req_parts = _re.findall(part_regexp, req_usage) opt_parts = _re.findall(part_regexp, opt_usage) pos_parts = _re.findall(part_regexp, pos_usage) - req_parts = _re.findall(part_regexp, req_usage) + assert ' '.join(req_parts) == req_usage assert ' '.join(opt_parts) == opt_usage assert ' '.join(pos_parts) == pos_usage - assert ' '.join(req_parts) == req_usage # End cmd2 customization @@ -787,13 +787,15 @@ class ACHelpFormatter(argparse.RawTextHelpFormatter): if len(prefix) + len(prog) <= 0.75 * text_width: indent = ' ' * (len(prefix) + len(prog) + 1) # Begin cmd2 customization - if opt_parts: - lines = get_lines([prog] + pos_parts, indent, prefix) - lines.extend(get_lines(req_parts, indent)) + if req_parts: + lines = get_lines([prog] + req_parts, indent, prefix) lines.extend(get_lines(opt_parts, indent)) + lines.extend(get_lines(pos_parts, indent)) + elif opt_parts: + lines = get_lines([prog] + opt_parts, indent, prefix) + lines.extend(get_lines(pos_parts, indent)) elif pos_parts: lines = get_lines([prog] + pos_parts, indent, prefix) - lines.extend(get_lines(req_parts, indent)) else: lines = [prog] # End cmd2 customization @@ -806,9 +808,9 @@ class ACHelpFormatter(argparse.RawTextHelpFormatter): lines = get_lines(parts, indent) if len(lines) > 1: lines = [] - lines.extend(get_lines(pos_parts, indent)) lines.extend(get_lines(req_parts, indent)) lines.extend(get_lines(opt_parts, indent)) + lines.extend(get_lines(pos_parts, indent)) # End cmd2 customization lines = [prog] + lines |