summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcmd2/argparse_completer.py8
-rwxr-xr-xcmd2/cmd2.py1
-rwxr-xr-xexamples/subcommands.py2
3 files changed, 7 insertions, 4 deletions
diff --git a/cmd2/argparse_completer.py b/cmd2/argparse_completer.py
index 6c291e86..6e53a518 100755
--- a/cmd2/argparse_completer.py
+++ b/cmd2/argparse_completer.py
@@ -70,8 +70,10 @@ import re as _re
from .rl_utils import rl_force_redisplay
-ACTION_ARG_CHOICES = 'arg_choices'
+# attribute that can optionally added to an argparse argument (called an Action) to
+# define the completion choices for the argument. You may provide a Collection or a Function.
+ACTION_ARG_CHOICES = 'arg_choices'
class _RangeAction(object):
def __init__(self, nargs: Union[int, str, Tuple[int, int], None]):
@@ -222,6 +224,7 @@ class AutoCompleter(object):
# if there are choices defined, record them in the arguments dictionary
if action.choices is not None:
self._arg_choices[action.dest] = action.choices
+ # if completion choices are tagged on the action, record them
elif hasattr(action, ACTION_ARG_CHOICES):
action_arg_choices = getattr(action, ACTION_ARG_CHOICES)
self._arg_choices[action.dest] = action_arg_choices
@@ -412,9 +415,8 @@ class AutoCompleter(object):
return completion_results
def complete_command_help(self, tokens: List[str], text: str, line: str, begidx: int, endidx: int) -> List[str]:
+ """Supports the completion of sub-commands for commands thhrough the cmd2 help command."""
for idx, token in enumerate(tokens):
- is_last_token = idx > len(tokens) - 1
-
if idx >= self._token_start_index:
if self._positional_completers:
# For now argparse only allows 1 sub-command group per level
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index f9940b44..10f790c0 100755
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -1841,6 +1841,7 @@ class Cmd(cmd.Cmd):
def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int,
argparser: argparse.ArgumentParser) -> List[str]:
+ """Default completion function for argparse commands."""
completer = AutoCompleter(argparser)
tokens, _ = self.tokens_for_completion(line, begidx, endidx)
diff --git a/examples/subcommands.py b/examples/subcommands.py
index 8cdfb368..75c0733e 100755
--- a/examples/subcommands.py
+++ b/examples/subcommands.py
@@ -9,7 +9,7 @@ and provides separate contextual help.
import argparse
import cmd2
-from cmd2 import with_argparser, with_argparser_and_unknown_args
+from cmd2 import with_argparser
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']