77- index_based_complete
88- delimiter_completer
99
10- For an example enabling tab completion with argparse, see argparse_completion.py
10+ For an example integrating tab completion with argparse, see argparse_completion.py
1111"""
12- import argparse
1312import functools
1413
1514import cmd2
1817food_item_strs = ['Pizza' , 'Ham' , 'Ham Sandwich' , 'Potato' ]
1918sport_item_strs = ['Bat' , 'Basket' , 'Basketball' , 'Football' , 'Space Ball' ]
2019
20+ # This data is used to demonstrate delimiter_complete
2121file_strs = \
2222 [
2323 '/home/user/file.db' ,
2828 ]
2929
3030
31- class TabCompleteExample (cmd2 .Cmd ):
32- """ Example cmd2 application where we a base command which has a couple subcommands."""
33- def __init__ (self ):
34- super ().__init__ ()
35-
36- # The add_item command uses flag_based_complete
37- add_item_parser = argparse .ArgumentParser ()
38- add_item_group = add_item_parser .add_mutually_exclusive_group ()
39- add_item_group .add_argument ('-f' , '--food' , help = 'Adds food item' )
40- add_item_group .add_argument ('-s' , '--sport' , help = 'Adds sport item' )
41- add_item_group .add_argument ('-o' , '--other' , help = 'Adds other item' )
42-
43- @cmd2 .with_argparser (add_item_parser )
44- def do_add_item (self , args ):
45- """Add item command help"""
46- if args .food :
47- add_item = args .food
48- elif args .sport :
49- add_item = args .sport
50- elif args .other :
51- add_item = args .other
52- else :
53- add_item = 'no items'
54-
55- self .poutput ("You added {}" .format (add_item ))
56-
57- # Add flag-based tab-completion to add_item command
58- def complete_add_item (self , text , line , begidx , endidx ):
31+ class BasicCompletion (cmd2 .Cmd ):
32+ def __init__ (self , * args , ** kwargs ):
33+ super ().__init__ (* args , ** kwargs )
34+
35+ def do_flag_based (self , statement : cmd2 .Statement ):
36+ """Tab completes arguments based on a preceding flag using flag_based_complete
37+ -f, --food [completes food items]
38+ -s, --sport [completes sports]
39+ -p, --path [completes local file system paths]
40+ """
41+ self .poutput ("Args: {}" .format (statement .args ))
42+
43+ def complete_flag_based (self , text , line , begidx , endidx ):
44+ """Completion function for do_flag_based"""
5945 flag_dict = \
6046 {
6147 # Tab-complete food items after -f and --food flags in command line
@@ -66,21 +52,19 @@ def complete_add_item(self, text, line, begidx, endidx):
6652 '-s' : sport_item_strs ,
6753 '--sport' : sport_item_strs ,
6854
69- # Tab-complete using path_complete function after -o and --other flags in command line
70- '-o ' : self .path_complete ,
71- '--other ' : self .path_complete ,
55+ # Tab-complete using path_complete function after -p and --path flags in command line
56+ '-p ' : self .path_complete ,
57+ '--path ' : self .path_complete ,
7258 }
7359
7460 return self .flag_based_complete (text , line , begidx , endidx , flag_dict = flag_dict )
7561
76- # The list_item command uses index_based_complete
77- @cmd2 .with_argument_list
78- def do_list_item (self , args ):
79- """List item command help"""
80- self .poutput ("You listed {}" .format (args ))
62+ def do_index_based (self , statement : cmd2 .Statement ):
63+ """Tab completes first 3 arguments using index_based_complete"""
64+ self .poutput ("Args: {}" .format (statement .args ))
8165
82- # Add index-based tab-completion to list_item command
83- def complete_list_item ( self , text , line , begidx , endidx ):
66+ def complete_index_based ( self , text , line , begidx , endidx ):
67+ """Completion function for do_index_based"""
8468 index_dict = \
8569 {
8670 1 : food_item_strs , # Tab-complete food items at index 1 in command line
@@ -90,16 +74,16 @@ def complete_list_item(self, text, line, begidx, endidx):
9074
9175 return self .index_based_complete (text , line , begidx , endidx , index_dict = index_dict )
9276
93- # The file_list command uses delimiter_complete
94- def do_file_list (self , statement : cmd2 .Statement ):
95- """List files entered on command line"""
96- self .poutput ("You selected: {}" .format (statement .args ))
77+ def do_delimiter_complete (self , statement : cmd2 .Statement ):
78+ """Tab completes files from a list using delimiter_complete"""
79+ self .poutput ("Args: {}" .format (statement .args ))
9780
9881 # Use a partialmethod to set arguments to delimiter_complete
99- complete_file_list = functools .partialmethod (cmd2 .Cmd .delimiter_complete , match_against = file_strs , delimiter = '/' )
82+ complete_delimiter_complete = functools .partialmethod (cmd2 .Cmd .delimiter_complete ,
83+ match_against = file_strs , delimiter = '/' )
10084
10185
10286if __name__ == '__main__' :
10387 import sys
104- app = TabCompleteExample ()
88+ app = BasicCompletion ()
10589 sys .exit (app .cmdloop ())
0 commit comments