diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-09-16 19:33:11 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-16 19:33:11 -0400 |
commit | 60ab11c3166a2a76bf67774c67b583d7d30fe5be (patch) | |
tree | 89bea336649b0e270b62c43ac501b1339992b967 /cmd2/cmd2.py | |
parent | f10674e6db245da5b4062aef3391d54c33277255 (diff) | |
parent | c7bf16f3ab46d5b6f4c48c038b620930fbe4551b (diff) | |
download | cmd2-git-60ab11c3166a2a76bf67774c67b583d7d30fe5be.tar.gz |
Merge pull request #774 from python-cmd2/completion_state
Completion state
Diffstat (limited to 'cmd2/cmd2.py')
-rwxr-xr-x | cmd2/cmd2.py | 71 |
1 files changed, 31 insertions, 40 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 610ec897..69de58b0 100755 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -119,6 +119,9 @@ CMD_ATTR_HELP_CATEGORY = 'help_category' # The argparse parser for the command CMD_ATTR_ARGPARSER = 'argparser' +# Whether or not tokens are unquoted before sending to argparse +CMD_ATTR_PRESERVE_QUOTES = 'preserve_quotes' + def categorize(func: Union[Callable, Iterable[Callable]], category: str) -> None: """Categorize a function. @@ -225,8 +228,9 @@ def with_argparser_and_unknown_args(argparser: argparse.ArgumentParser, *, # Set the command's help text as argparser.description (which can be None) cmd_wrapper.__doc__ = argparser.description - # Mark this function as having an argparse ArgumentParser + # Set some custom attributes for this command setattr(cmd_wrapper, CMD_ATTR_ARGPARSER, argparser) + setattr(cmd_wrapper, CMD_ATTR_PRESERVE_QUOTES, preserve_quotes) return cmd_wrapper @@ -283,8 +287,9 @@ def with_argparser(argparser: argparse.ArgumentParser, *, # Set the command's help text as argparser.description (which can be None) cmd_wrapper.__doc__ = argparser.description - # Mark this function as having an argparse ArgumentParser + # Set some custom attributes for this command setattr(cmd_wrapper, CMD_ATTR_ARGPARSER, argparser) + setattr(cmd_wrapper, CMD_ATTR_PRESERVE_QUOTES, preserve_quotes) return cmd_wrapper @@ -1431,7 +1436,8 @@ class Cmd(cmd.Cmd): if func is not None and argparser is not None: import functools compfunc = functools.partial(self._autocomplete_default, - argparser=argparser) + argparser=argparser, + preserve_quotes=getattr(func, CMD_ATTR_PRESERVE_QUOTES)) else: compfunc = self.completedefault @@ -1588,13 +1594,17 @@ class Cmd(cmd.Cmd): self.pexcept(e) return None - def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int, - argparser: argparse.ArgumentParser) -> List[str]: + def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int, *, + argparser: argparse.ArgumentParser, preserve_quotes: bool) -> List[str]: """Default completion function for argparse commands""" from .argparse_completer import AutoCompleter completer = AutoCompleter(argparser, self) - tokens, _ = self.tokens_for_completion(line, begidx, endidx) - return completer.complete_command(tokens, text, line, begidx, endidx) + tokens, raw_tokens = self.tokens_for_completion(line, begidx, endidx) + + # To have tab-completion parsing match command line parsing behavior, + # use preserve_quotes to determine if we parse the quoted or unquoted tokens. + tokens_to_parse = raw_tokens if preserve_quotes else tokens + return completer.complete_command(tokens_to_parse, text, line, begidx, endidx) def get_names(self): """Return an alphabetized list of names comprising the attributes of the cmd2 class instance.""" @@ -2662,42 +2672,27 @@ class Cmd(cmd.Cmd): strs_to_match = list(topics | visible_commands) return utils.basic_complete(text, line, begidx, endidx, strs_to_match) - def complete_help_subcommand(self, text: str, line: str, begidx: int, endidx: int) -> List[str]: + def complete_help_subcommand(self, text: str, line: str, begidx: int, endidx: int, + arg_tokens: Dict[str, List[str]]) -> List[str]: """Completes the subcommand argument of help""" - # Get all tokens through the one being completed - tokens, _ = self.tokens_for_completion(line, begidx, endidx) - - if not tokens: - return [] - - # Must have at least 3 args for 'help command subcommand' - if len(tokens) < 3: + # Make sure we have a command whose subcommands we will complete + command = arg_tokens['command'][0] + if not command: return [] - # Find where the command is by skipping past any flags - cmd_index = 1 - for cur_token in tokens[cmd_index:]: - if not cur_token.startswith('-'): - break - cmd_index += 1 - - if cmd_index >= len(tokens): - return [] - - command = tokens[cmd_index] - matches = [] - # Check if this command uses argparse func = self.cmd_func(command) argparser = getattr(func, CMD_ATTR_ARGPARSER, None) + if func is None or argparser is None: + return [] - if func is not None and argparser is not None: - from .argparse_completer import AutoCompleter - completer = AutoCompleter(argparser, self) - matches = completer.complete_command_help(tokens[cmd_index:], text, line, begidx, endidx) + # Combine the command and its subcommand tokens for the AutoCompleter + tokens = [command] + arg_tokens['subcommand'] - return matches + from .argparse_completer import AutoCompleter + completer = AutoCompleter(argparser, self) + return completer.complete_subcommand_help(tokens, text, line, begidx, endidx) help_parser = Cmd2ArgumentParser(description="List available commands or provide " "detailed help for a specific command") @@ -2963,14 +2958,11 @@ class Cmd(cmd.Cmd): choice = int(response) if choice < 1: raise IndexError - result = fulloptions[choice - 1][0] - break + return fulloptions[choice - 1][0] except (ValueError, IndexError): self.poutput("{!r} isn't a valid choice. Pick a number between 1 and {}:".format( response, len(fulloptions))) - return result - def _get_read_only_settings(self) -> str: """Return a summary report of read-only settings which the user cannot modify at runtime. @@ -4125,8 +4117,7 @@ class Cmd(cmd.Cmd): if getattr(func, CMD_ATTR_HELP_CATEGORY, None) == category: self.disable_command(cmd_name, message_to_print) - # noinspection PyUnusedLocal - def _report_disabled_command_usage(self, *args, message_to_print: str, **kwargs) -> None: + def _report_disabled_command_usage(self, *_args, message_to_print: str, **_kwargs) -> None: """ Report when a disabled command has been run or had help called on it :param args: not used |