Skip to content

Commit 19ebad9

Browse files
authored
Merge pull request #428 from python-cmd2/simplify_subcommand_example
Simplify subcommnads.py example
2 parents 9852270 + f732235 commit 19ebad9

File tree

1 file changed

+2
-21
lines changed

1 file changed

+2
-21
lines changed

examples/subcommands.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#!/usr/bin/env python3
22
# coding=utf-8
3-
# PYTHON_ARGCOMPLETE_OK
43
"""A simple example demonstrating how to use Argparse to support subcommands.
54
65
76
This example shows an easy way for a single command to have many subcommands, each of which takes different arguments
87
and provides separate contextual help.
98
"""
109
import argparse
10+
import cmd2
1111

1212
sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
1313

@@ -19,10 +19,7 @@
1919
parser_foo = base_subparsers.add_parser('foo', help='foo help')
2020
parser_foo.add_argument('-x', type=int, default=1, help='integer')
2121
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')
2623

2724
# create the parser for the "bar" subcommand
2825
parser_bar = base_subparsers.add_parser('bar', help='bar help')
@@ -39,28 +36,12 @@
3936
sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport')
4037
setattr(sport_arg, 'arg_choices', sport_item_strs)
4138

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-
5739

5840
class SubcommandsExample(cmd2.Cmd):
5941
"""
6042
Example cmd2 application where we a base command which has a couple subcommands
6143
and the "sport" subcommand has tab completion enabled.
6244
"""
63-
6445
def __init__(self):
6546
super().__init__()
6647

0 commit comments

Comments
 (0)