summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/argparse_completer.py22
-rwxr-xr-xcmd2/cmd2.py19
2 files changed, 21 insertions, 20 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py
index d205d366..df835e71 100644
--- a/cmd2/argparse_completer.py
+++ b/cmd2/argparse_completer.py
@@ -23,8 +23,8 @@ from .rl_utils import rl_force_redisplay
DEFAULT_DESCRIPTIVE_HEADER = 'Description'
# Name of the choice/completer function argument that, if present, will be passed a Namespace of
-# parsed command line tokens prior to the token being completed
-PARSED_ARGS = 'parsed_args'
+# command line tokens up through the token being completed mapped to their argparse destination.
+ARG_TOKENS = 'arg_tokens'
def _single_prefix_char(token: str, parser: argparse.ArgumentParser) -> bool:
@@ -455,15 +455,21 @@ class AutoCompleter(object):
if arg_choices.is_method:
args.append(self._cmd2_app)
- # If arg_choices.to_call accepts an argument called parsed_args, then convert
+ # If arg_choices.to_call accepts an argument called arg_tokens, then convert
# consumed_arg_values into an argparse Namespace and pass it to the function
to_call_params = inspect.signature(arg_choices.to_call).parameters
- if PARSED_ARGS in to_call_params:
- parsed_args = argparse.Namespace()
+ if ARG_TOKENS in to_call_params:
+ arg_tokens = argparse.Namespace()
for action, tokens in consumed_arg_values.items():
- setattr(parsed_args, action.dest, tokens)
- parsed_args.__parser__ = self._parser
- kwargs[PARSED_ARGS] = parsed_args
+ setattr(arg_tokens, action.dest, tokens)
+
+ # Include the token being completed in the Namespace
+ tokens = getattr(arg_tokens, arg_action.dest, [])
+ tokens.append(text)
+ setattr(arg_tokens, arg_action.dest, tokens)
+
+ # Add the namespace to the keyword arguments for the function we are calling
+ kwargs[ARG_TOKENS] = arg_tokens
# Check if the argument uses a specific tab completion function to provide its choices
if isinstance(arg_choices, ChoicesCallable) and arg_choices.is_completer:
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 900a4c75..f859ad64 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2669,31 +2669,26 @@ class Cmd(cmd.Cmd):
return utils.basic_complete(text, line, begidx, endidx, strs_to_match)
def complete_help_subcommand(self, text: str, line: str, begidx: int, endidx: int,
- parsed_args: argparse.Namespace) -> List[str]:
+ arg_tokens: argparse.Namespace) -> List[str]:
"""Completes the subcommand argument of help"""
# Make sure we have a command whose subcommands we will complete
- parsed_args.command = parsed_args.command[0]
- if not parsed_args.command:
+ command = arg_tokens.command[0]
+ if not command:
return []
# Check if this command uses argparse
- func = self.cmd_func(parsed_args.command)
+ func = self.cmd_func(command)
argparser = getattr(func, CMD_ATTR_ARGPARSER, None)
if func is None or argparser is None:
return []
- # Get all tokens through the one being completed
- tokens, _ = self.tokens_for_completion(line, begidx, endidx)
- if not tokens:
- return []
-
- # Get the index of the command
- cmd_index = tokens.index(parsed_args.command)
+ # Combine the command and its subcommand tokens for the AutoCompleter
+ tokens = [command] + arg_tokens.subcommand
from .argparse_completer import AutoCompleter
completer = AutoCompleter(argparser, self)
- return completer.complete_subcommand_help(tokens[cmd_index:], text, line, begidx, endidx)
+ 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")