1
- # coding=utf-8
2
- # flake8: noqa C901
3
- # NOTE: Ignoring flake8 cyclomatic complexity in this file
4
1
"""
5
2
This module defines the ArgparseCompleter class which provides argparse-based tab completion to cmd2 apps.
6
3
See the header of argparse_custom.py for instructions on how to use these features.
14
11
)
15
12
from typing import (
16
13
TYPE_CHECKING ,
17
- Dict ,
18
- List ,
19
14
Optional ,
20
15
Type ,
21
16
Union ,
@@ -172,7 +167,7 @@ class ArgparseCompleter:
172
167
"""Automatic command line tab completion based on argparse parameters"""
173
168
174
169
def __init__ (
175
- self , parser : argparse .ArgumentParser , cmd2_app : 'Cmd' , * , parent_tokens : Optional [Dict [str , List [str ]]] = None
170
+ self , parser : argparse .ArgumentParser , cmd2_app : 'Cmd' , * , parent_tokens : Optional [dict [str , list [str ]]] = None
176
171
) -> None :
177
172
"""
178
173
Create an ArgparseCompleter
@@ -187,7 +182,7 @@ def __init__(
187
182
self ._cmd2_app = cmd2_app
188
183
189
184
if parent_tokens is None :
190
- parent_tokens = dict ()
185
+ parent_tokens = {}
191
186
self ._parent_tokens = parent_tokens
192
187
193
188
self ._flags = [] # all flags in this command
@@ -213,8 +208,8 @@ def __init__(
213
208
self ._subcommand_action = action
214
209
215
210
def complete (
216
- self , text : str , line : str , begidx : int , endidx : int , tokens : List [str ], * , cmd_set : Optional [CommandSet ] = None
217
- ) -> List [str ]:
211
+ self , text : str , line : str , begidx : int , endidx : int , tokens : list [str ], * , cmd_set : Optional [CommandSet ] = None
212
+ ) -> list [str ]:
218
213
"""
219
214
Complete text using argparse metadata
220
215
@@ -245,13 +240,13 @@ def complete(
245
240
flag_arg_state : Optional [_ArgumentState ] = None
246
241
247
242
# Non-reusable flags that we've parsed
248
- matched_flags : List [str ] = []
243
+ matched_flags : list [str ] = []
249
244
250
245
# Keeps track of arguments we've seen and any tokens they consumed
251
- consumed_arg_values : Dict [str , List [str ]] = dict () # dict(arg_name -> List [tokens])
246
+ consumed_arg_values : dict [str , list [str ]] = {} # dict(arg_name -> list [tokens])
252
247
253
248
# Completed mutually exclusive groups
254
- completed_mutex_groups : Dict [argparse ._MutuallyExclusiveGroup , argparse .Action ] = dict ()
249
+ completed_mutex_groups : dict [argparse ._MutuallyExclusiveGroup , argparse .Action ] = {}
255
250
256
251
def consume_argument (arg_state : _ArgumentState ) -> None :
257
252
"""Consuming token as an argument"""
@@ -507,7 +502,7 @@ def update_mutex_groups(arg_action: argparse.Action) -> None:
507
502
508
503
return completion_results
509
504
510
- def _complete_flags (self , text : str , line : str , begidx : int , endidx : int , matched_flags : List [str ]) -> List [str ]:
505
+ def _complete_flags (self , text : str , line : str , begidx : int , endidx : int , matched_flags : list [str ]) -> list [str ]:
511
506
"""Tab completion routine for a parsers unused flags"""
512
507
513
508
# Build a list of flags that can be tab completed
@@ -524,7 +519,7 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche
524
519
matches = self ._cmd2_app .basic_complete (text , line , begidx , endidx , match_against )
525
520
526
521
# Build a dictionary linking actions with their matched flag names
527
- matched_actions : Dict [argparse .Action , List [str ]] = dict ()
522
+ matched_actions : dict [argparse .Action , list [str ]] = {}
528
523
for flag in matches :
529
524
action = self ._flag_to_action [flag ]
530
525
matched_actions .setdefault (action , [])
@@ -541,14 +536,14 @@ def _complete_flags(self, text: str, line: str, begidx: int, endidx: int, matche
541
536
542
537
return matches
543
538
544
- def _format_completions (self , arg_state : _ArgumentState , completions : Union [List [str ], List [CompletionItem ]]) -> List [str ]:
539
+ def _format_completions (self , arg_state : _ArgumentState , completions : Union [list [str ], list [CompletionItem ]]) -> list [str ]:
545
540
"""Format CompletionItems into hint table"""
546
541
547
542
# Nothing to do if we don't have at least 2 completions which are all CompletionItems
548
543
if len (completions ) < 2 or not all (isinstance (c , CompletionItem ) for c in completions ):
549
- return cast (List [str ], completions )
544
+ return cast (list [str ], completions )
550
545
551
- completion_items = cast (List [CompletionItem ], completions )
546
+ completion_items = cast (list [CompletionItem ], completions )
552
547
553
548
# Check if the data being completed have a numerical type
554
549
all_nums = all (isinstance (c .orig_value , numbers .Number ) for c in completion_items )
@@ -599,7 +594,7 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[List
599
594
item .description = item .description .replace ('\t ' , four_spaces )
600
595
desc_width = max (widest_line (item .description ), desc_width )
601
596
602
- cols = list ()
597
+ cols = []
603
598
dest_alignment = HorizontalAlignment .RIGHT if all_nums else HorizontalAlignment .LEFT
604
599
cols .append (
605
600
Column (
@@ -616,17 +611,17 @@ def _format_completions(self, arg_state: _ArgumentState, completions: Union[List
616
611
self ._cmd2_app .formatted_completions = hint_table .generate_table (table_data , row_spacing = 0 )
617
612
618
613
# Return sorted list of completions
619
- return cast (List [str ], completions )
614
+ return cast (list [str ], completions )
620
615
621
- def complete_subcommand_help (self , text : str , line : str , begidx : int , endidx : int , tokens : List [str ]) -> List [str ]:
616
+ def complete_subcommand_help (self , text : str , line : str , begidx : int , endidx : int , tokens : list [str ]) -> list [str ]:
622
617
"""
623
618
Supports cmd2's help command in the completion of subcommand names
624
619
:param text: the string prefix we are attempting to match (all matches must begin with it)
625
620
:param line: the current input line with leading whitespace removed
626
621
:param begidx: the beginning index of the prefix text
627
622
:param endidx: the ending index of the prefix text
628
623
:param tokens: arguments passed to command/subcommand
629
- :return: List of subcommand completions
624
+ :return: list of subcommand completions
630
625
"""
631
626
# If our parser has subcommands, we must examine the tokens and check if they are subcommands
632
627
# If so, we will let the subcommand's parser handle the rest of the tokens via another ArgparseCompleter.
@@ -645,7 +640,7 @@ def complete_subcommand_help(self, text: str, line: str, begidx: int, endidx: in
645
640
break
646
641
return []
647
642
648
- def format_help (self , tokens : List [str ]) -> str :
643
+ def format_help (self , tokens : list [str ]) -> str :
649
644
"""
650
645
Supports cmd2's help command in the retrieval of help text
651
646
:param tokens: arguments passed to help command
@@ -672,17 +667,17 @@ def _complete_arg(
672
667
begidx : int ,
673
668
endidx : int ,
674
669
arg_state : _ArgumentState ,
675
- consumed_arg_values : Dict [str , List [str ]],
670
+ consumed_arg_values : dict [str , list [str ]],
676
671
* ,
677
672
cmd_set : Optional [CommandSet ] = None ,
678
- ) -> List [str ]:
673
+ ) -> list [str ]:
679
674
"""
680
675
Tab completion routine for an argparse argument
681
676
:return: list of completions
682
677
:raises CompletionError: if the completer or choices function this calls raises one
683
678
"""
684
679
# Check if the arg provides choices to the user
685
- arg_choices : Union [List [str ], ChoicesCallable ]
680
+ arg_choices : Union [list [str ], ChoicesCallable ]
686
681
if arg_state .action .choices is not None :
687
682
arg_choices = list (arg_state .action .choices )
688
683
if not arg_choices :
@@ -739,7 +734,7 @@ def _complete_arg(
739
734
# Otherwise use basic_complete on the choices
740
735
else :
741
736
# Check if the choices come from a function
742
- completion_items : List [str ] = []
737
+ completion_items : list [str ] = []
743
738
if isinstance (arg_choices , ChoicesCallable ):
744
739
if not arg_choices .is_completer :
745
740
choices_func = arg_choices .choices_provider
0 commit comments