|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # coding=utf-8 |
3 | | -# PYTHON_ARGCOMPLETE_OK |
4 | 3 | """A simple example demonstrating how to use Argparse to support subcommands. |
5 | 4 |
|
6 | 5 |
|
7 | 6 | This example shows an easy way for a single command to have many subcommands, each of which takes different arguments |
8 | 7 | and provides separate contextual help. |
9 | 8 | """ |
10 | 9 | import argparse |
| 10 | +import cmd2 |
11 | 11 |
|
12 | 12 | sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] |
13 | 13 |
|
|
19 | 19 | parser_foo = base_subparsers.add_parser('foo', help='foo help') |
20 | 20 | parser_foo.add_argument('-x', type=int, default=1, help='integer') |
21 | 21 | parser_foo.add_argument('y', type=float, help='float') |
22 | | -input_file = parser_foo.add_argument('input_file', type=str, help='Input File') |
23 | | -if __name__ == '__main__': |
24 | | - from cmd2.argcomplete_bridge import bash_complete |
25 | | - bash_complete(input_file) |
| 22 | +parser_foo.add_argument('input_file', type=str, help='Input File') |
26 | 23 |
|
27 | 24 | # create the parser for the "bar" subcommand |
28 | 25 | parser_bar = base_subparsers.add_parser('bar', help='bar help') |
|
39 | 36 | sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport') |
40 | 37 | setattr(sport_arg, 'arg_choices', sport_item_strs) |
41 | 38 |
|
42 | | -# Handle bash completion if it's installed |
43 | | -try: |
44 | | - # only move forward if we can import CompletionFinder and AutoCompleter |
45 | | - from cmd2.argcomplete_bridge import CompletionFinder |
46 | | - from cmd2.argparse_completer import AutoCompleter |
47 | | - if __name__ == '__main__': |
48 | | - completer = CompletionFinder() |
49 | | - completer(base_parser, AutoCompleter(base_parser)) |
50 | | -except ImportError: |
51 | | - pass |
52 | | - |
53 | | - |
54 | | -# Intentionally below the bash completion code to reduce tab completion lag |
55 | | -import cmd2 |
56 | | - |
57 | 39 |
|
58 | 40 | class SubcommandsExample(cmd2.Cmd): |
59 | 41 | """ |
60 | 42 | Example cmd2 application where we a base command which has a couple subcommands |
61 | 43 | and the "sport" subcommand has tab completion enabled. |
62 | 44 | """ |
63 | | - |
64 | 45 | def __init__(self): |
65 | 46 | super().__init__() |
66 | 47 |
|
|
0 commit comments