Skip to content

Commit 878601b

Browse files
committed
Renamed AutoCompleter to ArgparseCompleter for clarity
1 parent 2221e08 commit 878601b

File tree

6 files changed

+53
-52
lines changed

6 files changed

+53
-52
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Only tab complete after redirection tokens if redirection is allowed
1616
* Other
1717
* Removed undocumented `py run` command since it was replaced by `run_pyscript` a while ago
18+
* Renamed `AutoCompleter` to `ArgparseCompleter` for clarity
1819

1920
## 0.10.0 (February 7, 2020)
2021
* Enhancements

cmd2/argparse_completer.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# flake8: noqa C901
33
# NOTE: Ignoring flake8 cyclomatic complexity in this file
44
"""
5-
This module defines the AutoCompleter class which provides argparse-based tab completion to cmd2 apps.
5+
This module defines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps.
66
See the header of argparse_custom.py for instructions on how to use these features.
77
"""
88

@@ -64,7 +64,7 @@ def _looks_like_flag(token: str, parser: argparse.ArgumentParser) -> bool:
6464

6565

6666
# noinspection PyProtectedMember
67-
class AutoCompleter:
67+
class ArgparseCompleter:
6868
"""Automatic command line tab completion based on argparse parameters"""
6969

7070
class _ArgumentState:
@@ -103,12 +103,12 @@ def __init__(self, arg_action: argparse.Action) -> None:
103103
def __init__(self, parser: argparse.ArgumentParser, cmd2_app: cmd2.Cmd, *,
104104
parent_tokens: Optional[Dict[str, List[str]]] = None) -> None:
105105
"""
106-
Create an AutoCompleter
106+
Create an ArgparseCompleter
107107
108108
:param parser: ArgumentParser instance
109-
:param cmd2_app: reference to the Cmd2 application that owns this AutoCompleter
109+
:param cmd2_app: reference to the Cmd2 application that owns this ArgparseCompleter
110110
:param parent_tokens: optional dictionary mapping parent parsers' arg names to their tokens
111-
this is only used by AutoCompleter when recursing on subcommand parsers
111+
This is only used by ArgparseCompleter when recursing on subcommand parsers
112112
Defaults to None
113113
"""
114114
self._parser = parser
@@ -167,7 +167,7 @@ def complete_command(self, tokens: List[str], text: str, line: str, begidx: int,
167167
# Completed mutually exclusive groups
168168
completed_mutex_groups = dict() # dict(argparse._MutuallyExclusiveGroup -> Action which completed group)
169169

170-
def consume_argument(arg_state: AutoCompleter._ArgumentState) -> None:
170+
def consume_argument(arg_state: ArgparseCompleter._ArgumentState) -> None:
171171
"""Consuming token as an argument"""
172172
arg_state.count += 1
173173
consumed_arg_values.setdefault(arg_state.action.dest, [])
@@ -286,7 +286,7 @@ def update_mutex_groups(arg_action: argparse.Action) -> bool:
286286
# earlier in the command line. Reset them now for this use of it.
287287
consumed_arg_values[action.dest] = []
288288

289-
new_arg_state = AutoCompleter._ArgumentState(action)
289+
new_arg_state = ArgparseCompleter._ArgumentState(action)
290290

291291
# Keep track of this flag if it can receive arguments
292292
if new_arg_state.max > 0:
@@ -319,16 +319,16 @@ def update_mutex_groups(arg_action: argparse.Action) -> bool:
319319
if action.dest != argparse.SUPPRESS:
320320
parent_tokens[action.dest] = [token]
321321

322-
completer = AutoCompleter(self._subcommand_action.choices[token], self._cmd2_app,
323-
parent_tokens=parent_tokens)
322+
completer = ArgparseCompleter(self._subcommand_action.choices[token], self._cmd2_app,
323+
parent_tokens=parent_tokens)
324324
return completer.complete_command(tokens[token_index:], text, line, begidx, endidx)
325325
else:
326326
# Invalid subcommand entered, so no way to complete remaining tokens
327327
return []
328328

329329
# Otherwise keep track of the argument
330330
else:
331-
pos_arg_state = AutoCompleter._ArgumentState(action)
331+
pos_arg_state = ArgparseCompleter._ArgumentState(action)
332332

333333
# Check if we have a positional to consume this token
334334
if pos_arg_state is not None:
@@ -393,7 +393,7 @@ def update_mutex_groups(arg_action: argparse.Action) -> bool:
393393
# If we aren't current tracking a positional, then get the next positional arg to handle this token
394394
if pos_arg_state is None:
395395
action = remaining_positionals.popleft()
396-
pos_arg_state = AutoCompleter._ArgumentState(action)
396+
pos_arg_state = ArgparseCompleter._ArgumentState(action)
397397

398398
try:
399399
completion_results = self._complete_for_arg(pos_arg_state.action, text, line,
@@ -483,11 +483,11 @@ def complete_subcommand_help(self, tokens: List[str], text: str, line: str, begi
483483
:return: List of subcommand completions
484484
"""
485485
# If our parser has subcommands, we must examine the tokens and check if they are subcommands
486-
# If so, we will let the subcommand's parser handle the rest of the tokens via another AutoCompleter.
486+
# If so, we will let the subcommand's parser handle the rest of the tokens via another ArgparseCompleter.
487487
if self._subcommand_action is not None:
488488
for token_index, token in enumerate(tokens[1:], start=1):
489489
if token in self._subcommand_action.choices:
490-
completer = AutoCompleter(self._subcommand_action.choices[token], self._cmd2_app)
490+
completer = ArgparseCompleter(self._subcommand_action.choices[token], self._cmd2_app)
491491
return completer.complete_subcommand_help(tokens[token_index:], text, line, begidx, endidx)
492492
elif token_index == len(tokens) - 1:
493493
# Since this is the last token, we will attempt to complete it
@@ -503,11 +503,11 @@ def format_help(self, tokens: List[str]) -> str:
503503
:return: help text of the command being queried
504504
"""
505505
# If our parser has subcommands, we must examine the tokens and check if they are subcommands
506-
# If so, we will let the subcommand's parser handle the rest of the tokens via another AutoCompleter.
506+
# If so, we will let the subcommand's parser handle the rest of the tokens via another ArgparseCompleter.
507507
if self._subcommand_action is not None:
508508
for token_index, token in enumerate(tokens[1:], start=1):
509509
if token in self._subcommand_action.choices:
510-
completer = AutoCompleter(self._subcommand_action.choices[token], self._cmd2_app)
510+
completer = ArgparseCompleter(self._subcommand_action.choices[token], self._cmd2_app)
511511
return completer.format_help(tokens[token_index:])
512512
else:
513513
break

cmd2/argparse_custom.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class called Cmd2ArgumentParser which improves error and help output over normal
2626
parser.add_argument('-f', nargs=(3, 5))
2727
2828
Tab Completion:
29-
cmd2 uses its AutoCompleter class to enable argparse-based tab completion on all commands that use the
29+
cmd2 uses its ArgparseCompleter class to enable argparse-based tab completion on all commands that use the
3030
@with_argparse wrappers. Out of the box you get tab completion of commands, subcommands, and flag names,
3131
as well as instructive hints about the current argument that print when tab is pressed. In addition,
3232
you can add tab completion for each argument's values using parameters passed to add_argument().
@@ -53,7 +53,7 @@ def my_choices_function():
5353
5454
choices_method
5555
This is exactly like choices_function, but the function needs to be an instance method of a cmd2-based class.
56-
When AutoCompleter calls the method, it will pass the app instance as the self argument. This is good in
56+
When ArgparseCompleter calls the method, it will pass the app instance as the self argument. This is good in
5757
cases where the list of choices being generated relies on state data of the cmd2-based app
5858
5959
Example:
@@ -74,7 +74,7 @@ def my_completer_function(text, line, begidx, endidx):
7474
7575
completer_method
7676
This is exactly like completer_function, but the function needs to be an instance method of a cmd2-based class.
77-
When AutoCompleter calls the method, it will pass the app instance as the self argument. cmd2 provides
77+
When ArgparseCompleter calls the method, it will pass the app instance as the self argument. cmd2 provides
7878
a few completer methods for convenience (e.g., path_complete, delimiter_complete)
7979
8080
Example:
@@ -113,12 +113,12 @@ def my_choices_method(self, arg_tokens)
113113
def my_completer_method(self, text, line, begidx, endidx, arg_tokens)
114114
115115
All values of the arg_tokens dictionary are lists, even if a particular argument expects only 1 token. Since
116-
AutoCompleter is for tab completion, it does not convert the tokens to their actual argument types or validate
116+
ArgparseCompleter is for tab completion, it does not convert the tokens to their actual argument types or validate
117117
their values. All tokens are stored in the dictionary as the raw strings provided on the command line. It is up to
118118
the developer to determine if the user entered the correct argument type (e.g. int) and validate their values.
119119
120120
CompletionError Class:
121-
Raised during tab completion operations to report any sort of error you want printed by the AutoCompleter
121+
Raised during tab completion operations to report any sort of error you want printed by the ArgparseCompleter
122122
123123
Example use cases
124124
- Reading a database to retrieve a tab completion data set failed
@@ -127,7 +127,7 @@ def my_completer_method(self, text, line, begidx, endidx, arg_tokens)
127127
CompletionItem Class:
128128
This class was added to help in cases where uninformative data is being tab completed. For instance,
129129
tab completing ID numbers isn't very helpful to a user without context. Returning a list of CompletionItems
130-
instead of a regular string for completion results will signal the AutoCompleter to output the completion
130+
instead of a regular string for completion results will signal the ArgparseCompleter to output the completion
131131
results in a table of completion tokens with descriptions instead of just a table of tokens.
132132
133133
Instead of this:
@@ -231,7 +231,7 @@ def generate_range_error(range_min: int, range_max: Union[int, float]) -> str:
231231

232232
class CompletionError(Exception):
233233
"""
234-
Raised during tab completion operations to report any sort of error you want printed by the AutoCompleter
234+
Raised during tab completion operations to report any sort of error you want printed by the ArgparseCompleter
235235
236236
Example use cases
237237
- Reading a database to retrieve a tab completion data set failed
@@ -353,15 +353,15 @@ def _add_argument_wrapper(self, *args,
353353
:param nargs: extends argparse nargs functionality by allowing tuples which specify a range (min, max)
354354
to specify a max value with no upper bound, use a 1-item tuple (min,)
355355
356-
# Added args used by AutoCompleter
356+
# Added args used by ArgparseCompleter
357357
:param choices_function: function that provides choices for this argument
358358
:param choices_method: cmd2-app method that provides choices for this argument
359359
:param completer_function: tab completion function that provides choices for this argument
360360
:param completer_method: cmd2-app tab completion method that provides choices for this argument
361-
:param suppress_tab_hint: when AutoCompleter has no results to show during tab completion, it displays the current
362-
argument's help text as a hint. Set this to True to suppress the hint. If this argument's
363-
help text is set to argparse.SUPPRESS, then tab hints will not display regardless of the
364-
value passed for suppress_tab_hint. Defaults to False.
361+
:param suppress_tab_hint: when ArgparseCompleter has no results to show during tab completion, it displays the
362+
current argument's help text as a hint. Set this to True to suppress the hint. If this
363+
argument's help text is set to argparse.SUPPRESS, then tab hints will not display
364+
regardless of the value passed for suppress_tab_hint. Defaults to False.
365365
:param descriptive_header: if the provided choices are CompletionItems, then this header will display
366366
during tab completion. Defaults to None.
367367

cmd2/cmd2.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,8 @@ def complete(self, text: str, state: int) -> Optional[str]:
14251425
def _autocomplete_default(self, text: str, line: str, begidx: int, endidx: int, *,
14261426
argparser: argparse.ArgumentParser, preserve_quotes: bool) -> List[str]:
14271427
"""Default completion function for argparse commands"""
1428-
from .argparse_completer import AutoCompleter
1429-
completer = AutoCompleter(argparser, self)
1428+
from .argparse_completer import ArgparseCompleter
1429+
completer = ArgparseCompleter(argparser, self)
14301430
tokens, raw_tokens = self.tokens_for_completion(line, begidx, endidx)
14311431

14321432
# To have tab-completion parsing match command line parsing behavior,
@@ -2560,11 +2560,11 @@ def complete_help_subcommands(self, text: str, line: str, begidx: int, endidx: i
25602560
if func is None or argparser is None:
25612561
return []
25622562

2563-
# Combine the command and its subcommand tokens for the AutoCompleter
2563+
# Combine the command and its subcommand tokens for the ArgparseCompleter
25642564
tokens = [command] + arg_tokens['subcommands']
25652565

2566-
from .argparse_completer import AutoCompleter
2567-
completer = AutoCompleter(argparser, self)
2566+
from .argparse_completer import ArgparseCompleter
2567+
completer = ArgparseCompleter(argparser, self)
25682568
return completer.complete_subcommand_help(tokens, text, line, begidx, endidx)
25692569

25702570
help_parser = DEFAULT_ARGUMENT_PARSER(description="List available commands or provide "
@@ -2576,7 +2576,7 @@ def complete_help_subcommands(self, text: str, line: str, begidx: int, endidx: i
25762576
help_parser.add_argument('-v', '--verbose', action='store_true',
25772577
help="print a list of all commands with descriptions of each")
25782578

2579-
# Get rid of cmd's complete_help() functions so AutoCompleter will complete the help command
2579+
# Get rid of cmd's complete_help() functions so ArgparseCompleter will complete the help command
25802580
if getattr(cmd.Cmd, 'complete_help', None) is not None:
25812581
delattr(cmd.Cmd, 'complete_help')
25822582

@@ -2594,8 +2594,8 @@ def do_help(self, args: argparse.Namespace) -> None:
25942594

25952595
# If the command function uses argparse, then use argparse's help
25962596
if func is not None and argparser is not None:
2597-
from .argparse_completer import AutoCompleter
2598-
completer = AutoCompleter(argparser, self)
2597+
from .argparse_completer import ArgparseCompleter
2598+
completer = ArgparseCompleter(argparser, self)
25992599
tokens = [args.command] + args.subcommands
26002600

26012601
# Set end to blank so the help output matches how it looks when "command -h" is used
@@ -2838,8 +2838,8 @@ def complete_set_value(self, text: str, line: str, begidx: int, endidx: int,
28382838
completer_function=settable.completer_function,
28392839
completer_method=settable.completer_method)
28402840

2841-
from .argparse_completer import AutoCompleter
2842-
completer = AutoCompleter(settable_parser, self)
2841+
from .argparse_completer import ArgparseCompleter
2842+
completer = ArgparseCompleter(settable_parser, self)
28432843

28442844
# Use raw_tokens since quotes have been preserved
28452845
_, raw_tokens = self.tokens_for_completion(line, begidx, endidx)
@@ -2860,7 +2860,7 @@ def complete_set_value(self, text: str, line: str, begidx: int, endidx: int,
28602860
set_parser = DEFAULT_ARGUMENT_PARSER(parents=[set_parser_parent])
28612861

28622862
# Suppress tab-completion hints for this field. The completer method is going to create an
2863-
# AutoCompleter based on the actual parameter being completed and we only want that hint printing.
2863+
# ArgparseCompleter based on the actual parameter being completed and we only want that hint printing.
28642864
set_parser.add_argument('value', nargs=argparse.OPTIONAL, help='new value for settable',
28652865
completer_method=complete_set_value, suppress_tab_hint=True)
28662866

cmd2/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ def __init__(self, name: str, val_type: Callable, description: str, *,
109109
for this argument (See note below)
110110
111111
Note:
112-
For choices_method and completer_method, do not set them to a bound method. This is because AutoCompleter
113-
passes the self argument explicitly to these functions.
112+
For choices_method and completer_method, do not set them to a bound method. This is because
113+
ArgparseCompleter passes the self argument explicitly to these functions.
114114
115115
Therefore instead of passing something like self.path_complete, pass cmd2.Cmd.path_complete.
116116
"""

0 commit comments

Comments
 (0)