diff options
author | Eric Lin <anselor@gmail.com> | 2018-04-21 23:11:07 -0400 |
---|---|---|
committer | Eric Lin <anselor@gmail.com> | 2018-04-21 23:11:07 -0400 |
commit | 102fc6741b5dfdbb02f28ee720214c70d1260cc0 (patch) | |
tree | c4dac853652ea9f009b84593ad95e2325a48676e | |
parent | 967f320e888fcf671768fc4d7d8838a6f6609b4f (diff) | |
download | cmd2-git-102fc6741b5dfdbb02f28ee720214c70d1260cc0.tar.gz |
Added some more comments for clarification.
-rwxr-xr-x | cmd2/argparse_completer.py | 8 | ||||
-rwxr-xr-x | cmd2/cmd2.py | 1 | ||||
-rwxr-xr-x | examples/subcommands.py | 2 |
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'] |