diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-05 03:30:43 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-09-09 00:06:18 -0400 |
commit | 11117ce9f88782ef1fca3e28d2dbb19df8865610 (patch) | |
tree | a7dba453580c35a8b44b7d8505bbef31836e336c /cmd2 | |
parent | a8d3ab2f64c3f8d76e687ecffdfd2211fc61b158 (diff) | |
download | cmd2-git-11117ce9f88782ef1fca3e28d2dbb19df8865610.tar.gz |
argparse tab completion now groups flag names which run the same action. Optional flags are wrapped in brackets
like it is done in argparse usage text.
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/argparse_completer.py | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py index 04e18d4a..7484358d 100644 --- a/cmd2/argparse_completer.py +++ b/cmd2/argparse_completer.py @@ -465,7 +465,25 @@ class ArgparseCompleter: if action.help != argparse.SUPPRESS: match_against.append(flag) - return basic_complete(text, line, begidx, endidx, match_against) + matches = basic_complete(text, line, begidx, endidx, match_against) + + # Build a dictionary linking actions with their matched flag names + matched_actions = dict() # type: Dict[argparse.Action, List[str]] + for flag in matches: + action = self._flag_to_action[flag] + matched_actions.setdefault(action, []) + matched_actions[action].append(flag) + + # For tab completion suggestions, group matched flags by action + for action, option_strings in matched_actions.items(): + flag_text = ', '.join(option_strings) + + # Mark optional flags with brackets + if not action.required: + flag_text = '[' + flag_text + ']' + self._cmd2_app.display_matches.append(flag_text) + + return matches def _format_completions(self, arg_state: _ArgumentState, completions: List[Union[str, CompletionItem]]) -> List[str]: # Check if the results are CompletionItems and that there aren't too many to display |