diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-03-16 19:29:33 -0400 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-03-16 19:29:33 -0400 |
commit | 9a1d22de8b65c143252a2dbe389616a212e57ce4 (patch) | |
tree | 58c619c08878b02e7e4a82da89ab1ad4fb923ab7 /examples/subcommands.py | |
parent | 70b340da1ea080e42ec91d0d37a8c266d095df94 (diff) | |
download | cmd2-git-9a1d22de8b65c143252a2dbe389616a212e57ce4.tar.gz |
Added unit tests and examples for tab completion of subcommands
Diffstat (limited to 'examples/subcommands.py')
-rwxr-xr-x | examples/subcommands.py | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/examples/subcommands.py b/examples/subcommands.py index 59ebe4cb..bc276dff 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -1,15 +1,16 @@ #!/usr/bin/env python # coding=utf-8 -"""A simple example demonstrating how to use Argparse to support sub-commands. +"""A simple example demonstrating how to use Argparse to support subcommands. This example shows an easy way for a single command to have many subcommands, each of which takes different arguments and provides separate contextual help. """ import argparse +import functools import cmd2 -from cmd2 import with_argparser +from cmd2 import with_argparser, index_based_complete class SubcommandsExample(cmd2.Cmd): @@ -18,7 +19,7 @@ class SubcommandsExample(cmd2.Cmd): def __init__(self): cmd2.Cmd.__init__(self) - # sub-command functions for the base command + # subcommand functions for the base command def base_foo(self, args): """foo subcommand of base command""" self.poutput(args.x * args.y) @@ -27,31 +28,50 @@ class SubcommandsExample(cmd2.Cmd): """bar subcommand of base command""" self.poutput('((%s))' % args.z) + def base_sport(self, args): + """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 """ + sports = ['Football', 'Hockey', 'Soccer', 'Baseball'] + index_dict = {1: sports} + return 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') - # create the parser for the "foo" sub-command + # create the parser for the "foo" subcommand parser_foo = base_subparsers.add_parser('foo', help='foo help') parser_foo.add_argument('-x', type=int, default=1, help='integer') parser_foo.add_argument('y', type=float, help='float') parser_foo.set_defaults(func=base_foo) - # create the parser for the "bar" sub-command + # 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) + # 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) + @with_argparser(base_parser) def do_base(self, args): """Base command help""" try: - # Call whatever sub-command function was selected + # Call whatever subcommand function was selected args.func(self, args) except AttributeError: - # No sub-command was provided, so as called + # No subcommand was provided, so as called self.do_help('base') + # This makes sure correct tab completion functions are called based on the selected subcommand + complete_base = functools.partialmethod(cmd2.Cmd.cmd_with_subs_completer, base='base') + if __name__ == '__main__': app = SubcommandsExample() |