From 85c2c6bba46900af6012b54c31e650095194b1aa Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Fri, 20 Apr 2018 15:50:53 -0400 Subject: Changed cmd2 to use autocompleter by default for all argparse commands. Not all tests are passing yet. --- examples/subcommands.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'examples/subcommands.py') diff --git a/examples/subcommands.py b/examples/subcommands.py index 031b17b2..03088c93 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -53,15 +53,22 @@ class SubcommandsExample(cmd2.Cmd): # create the parser for the "bar" subcommand parser_bar = base_subparsers.add_parser('bar', help='bar help') - parser_bar.add_argument('z', help='string') parser_bar.set_defaults(func=base_bar) + bar_subparsers = parser_bar.add_subparsers(title='layer3', help='help for 3rd layer of commands') + parser_bar.add_argument('z', help='string') + + bar_subparsers.add_parser('apple', help='apple help') + bar_subparsers.add_parser('artichoke', help='artichoke help') + bar_subparsers.add_parser('cranberries', help='cranberries help') + # 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') + sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') + setattr(sport_arg, 'arg_choices', sport_item_strs) # Set both a function and tab completer for the "sport" subcommand - parser_sport.set_defaults(func=base_sport, completer=complete_base_sport) + parser_sport.set_defaults(func=base_sport) @with_argparser(base_parser) def do_base(self, args): @@ -75,7 +82,7 @@ class SubcommandsExample(cmd2.Cmd): self.do_help('base') # Enable tab completion of base to make sure the subcommands' completers get called. - complete_base = cmd2.Cmd.cmd_with_subs_completer + # complete_base = cmd2.Cmd.cmd_with_subs_completer if __name__ == '__main__': -- cgit v1.2.1 From 965fa83804fec8ba3df8209b253e11acfb015d37 Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Sat, 21 Apr 2018 22:29:18 -0400 Subject: Switched the default behavior in cmd2 for argparse commands to use the AutoCompleter by default. --- examples/subcommands.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'examples/subcommands.py') diff --git a/examples/subcommands.py b/examples/subcommands.py index 03088c93..8cdfb368 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 +from cmd2 import with_argparser, with_argparser_and_unknown_args sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] @@ -35,12 +35,6 @@ class SubcommandsExample(cmd2.Cmd): """sport subcommand of base command""" self.poutput('Sport is {}'.format(args.sport)) - # noinspection PyUnusedLocal - def complete_base_sport(self, text, line, begidx, endidx): - """ Adds tab completion to base sport subcommand """ - 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 base_parser = argparse.ArgumentParser(prog='base') base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') @@ -81,9 +75,6 @@ class SubcommandsExample(cmd2.Cmd): # No subcommand was provided, so call help self.do_help('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__': app = SubcommandsExample() -- cgit v1.2.1 From 102fc6741b5dfdbb02f28ee720214c70d1260cc0 Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Sat, 21 Apr 2018 23:11:07 -0400 Subject: Added some more comments for clarification. --- examples/subcommands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/subcommands.py') 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'] -- cgit v1.2.1