|
37 | 37 | setattr(sport_arg, 'arg_choices', sport_item_strs) |
38 | 38 |
|
39 | 39 |
|
| 40 | +# create the top-level parser for the alternate command |
| 41 | +# The alternate command doesn't provide its own help flag |
| 42 | +base2_parser = argparse.ArgumentParser(prog='alternate', add_help=False) |
| 43 | +base2_subparsers = base2_parser.add_subparsers(title='subcommands', help='subcommand help') |
| 44 | + |
| 45 | +# create the parser for the "foo" subcommand |
| 46 | +parser_foo2 = base2_subparsers.add_parser('foo', help='foo help') |
| 47 | +parser_foo2.add_argument('-x', type=int, default=1, help='integer') |
| 48 | +parser_foo2.add_argument('y', type=float, help='float') |
| 49 | +parser_foo2.add_argument('input_file', type=str, help='Input File') |
| 50 | + |
| 51 | +# create the parser for the "bar" subcommand |
| 52 | +parser_bar2 = base2_subparsers.add_parser('bar', help='bar help') |
| 53 | + |
| 54 | +bar2_subparsers = parser_bar2.add_subparsers(title='layer3', help='help for 3rd layer of commands') |
| 55 | +parser_bar2.add_argument('z', help='string') |
| 56 | + |
| 57 | +bar2_subparsers.add_parser('apple', help='apple help') |
| 58 | +bar2_subparsers.add_parser('artichoke', help='artichoke help') |
| 59 | +bar2_subparsers.add_parser('cranberries', help='cranberries help') |
| 60 | + |
| 61 | +# create the parser for the "sport" subcommand |
| 62 | +parser_sport2 = base2_subparsers.add_parser('sport', help='sport help') |
| 63 | +sport2_arg = parser_sport2.add_argument('sport', help='Enter name of a sport') |
| 64 | +setattr(sport2_arg, 'arg_choices', sport_item_strs) |
| 65 | + |
| 66 | + |
40 | 67 | class SubcommandsExample(cmd2.Cmd): |
41 | 68 | """ |
42 | 69 | Example cmd2 application where we a base command which has a couple subcommands |
@@ -74,6 +101,17 @@ def do_base(self, args): |
74 | 101 | # No subcommand was provided, so call help |
75 | 102 | self.do_help('base') |
76 | 103 |
|
| 104 | + @cmd2.with_argparser(base2_parser) |
| 105 | + def do_alternate(self, args): |
| 106 | + """Alternate command help""" |
| 107 | + func = getattr(args, 'func', None) |
| 108 | + if func is not None: |
| 109 | + # Call whatever subcommand function was selected |
| 110 | + func(self, args) |
| 111 | + else: |
| 112 | + # No subcommand was provided, so call help |
| 113 | + self.do_help('alternate') |
| 114 | + |
77 | 115 |
|
78 | 116 | if __name__ == '__main__': |
79 | 117 | app = SubcommandsExample() |
|
0 commit comments