summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-25 19:14:29 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-09-25 19:14:29 -0400
commit2d38b72ba2203eb5cacbe0cb72cf7b8e27b5bb21 (patch)
treeda5ed281bb9d3e1b63c01b5aaba9f998cf2e4a6b /examples
parent39e43d6329eb517ac58f95a8ca37e7e489d446cb (diff)
parent8b12ab9f01c5a2b08c72fd4d2e1aaa15b402ead4 (diff)
downloadcmd2-git-2d38b72ba2203eb5cacbe0cb72cf7b8e27b5bb21.tar.gz
Merge branch 'master' into alert_printer
Diffstat (limited to 'examples')
-rwxr-xr-xexamples/subcommands.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/examples/subcommands.py b/examples/subcommands.py
index 9d51fbee..02b06412 100755
--- a/examples/subcommands.py
+++ b/examples/subcommands.py
@@ -37,6 +37,33 @@ sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport')
setattr(sport_arg, 'arg_choices', sport_item_strs)
+# create the top-level parser for the alternate command
+# The alternate command doesn't provide its own help flag
+base2_parser = argparse.ArgumentParser(prog='alternate', add_help=False)
+base2_subparsers = base2_parser.add_subparsers(title='subcommands', help='subcommand help')
+
+# create the parser for the "foo" subcommand
+parser_foo2 = base2_subparsers.add_parser('foo', help='foo help')
+parser_foo2.add_argument('-x', type=int, default=1, help='integer')
+parser_foo2.add_argument('y', type=float, help='float')
+parser_foo2.add_argument('input_file', type=str, help='Input File')
+
+# create the parser for the "bar" subcommand
+parser_bar2 = base2_subparsers.add_parser('bar', help='bar help')
+
+bar2_subparsers = parser_bar2.add_subparsers(title='layer3', help='help for 3rd layer of commands')
+parser_bar2.add_argument('z', help='string')
+
+bar2_subparsers.add_parser('apple', help='apple help')
+bar2_subparsers.add_parser('artichoke', help='artichoke help')
+bar2_subparsers.add_parser('cranberries', help='cranberries help')
+
+# create the parser for the "sport" subcommand
+parser_sport2 = base2_subparsers.add_parser('sport', help='sport help')
+sport2_arg = parser_sport2.add_argument('sport', help='Enter name of a sport')
+setattr(sport2_arg, 'arg_choices', sport_item_strs)
+
+
class SubcommandsExample(cmd2.Cmd):
"""
Example cmd2 application where we a base command which has a couple subcommands
@@ -74,6 +101,17 @@ class SubcommandsExample(cmd2.Cmd):
# No subcommand was provided, so call help
self.do_help('base')
+ @cmd2.with_argparser(base2_parser)
+ def do_alternate(self, args):
+ """Alternate command help"""
+ func = getattr(args, 'func', None)
+ if func is not None:
+ # Call whatever subcommand function was selected
+ func(self, args)
+ else:
+ # No subcommand was provided, so call help
+ self.do_help('alternate')
+
if __name__ == '__main__':
app = SubcommandsExample()