diff options
Diffstat (limited to 'examples/subcommands.py')
-rwxr-xr-x | examples/subcommands.py | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/examples/subcommands.py b/examples/subcommands.py index 7efed093..cbe4f634 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -11,9 +11,13 @@ import argparse import cmd2 from cmd2 import with_argparser +sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] class SubcommandsExample(cmd2.Cmd): - """ Example cmd2 application where we a base command which has a couple subcommands.""" + """ + Example cmd2 application where we a base command which has a couple subcommands + and the "sport" subcommand has tab completion enabled. + """ def __init__(self): cmd2.Cmd.__init__(self) @@ -34,8 +38,7 @@ class SubcommandsExample(cmd2.Cmd): # noinspection PyUnusedLocal def complete_base_sport(self, text, line, begidx, endidx): """ Adds tab completion to base sport subcommand """ - sports = ['Football', 'Hockey', 'Soccer', 'Baseball'] - index_dict = {1: sports} + index_dict = {1: sport_item_strs} return self.index_based_complete(text, line, begidx, endidx, index_dict) # create the top-level parser for the base command @@ -56,7 +59,9 @@ class SubcommandsExample(cmd2.Cmd): # create the parser for the "sport" subcommand parser_sport = base_subparsers.add_parser('sport', help='sport help') parser_sport.add_argument('sport', help='Enter name of a sport') - parser_sport.set_defaults(func=base_sport) + + # Set both a function and tab completer for the "sport" subcommand + parser_sport.set_defaults(func=base_sport, completer=complete_base_sport) @with_argparser(base_parser) def do_base(self, args): @@ -69,8 +74,8 @@ class SubcommandsExample(cmd2.Cmd): # No subcommand was provided, so call help self.do_help('base') - def complete_base(self, text, line, begidx, endidx): - return self.cmd_with_subs_completer(text, line, begidx, endidx, base='base') + # Enable tab completion of base to make sure the subcommands' completers get called. + complete_base = cmd2.Cmd.cmd_with_subs_completer if __name__ == '__main__': |