forked from python-cmd2/cmd2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodular_commands_main.py
72 lines (59 loc) · 2.32 KB
/
modular_commands_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# coding=utf-8
"""
A complex example demonstrating a variety of methods to load CommandSets using a mix of command decorators
with examples of how to integrate tab completion with argparse-based commands.
"""
import argparse
from typing import (
Iterable,
List,
Optional,
)
from modular_commands.commandset_basic import ( # noqa: F401
BasicCompletionCommandSet,
)
from modular_commands.commandset_complex import ( # noqa: F401
CommandSetA,
)
from modular_commands.commandset_custominit import ( # noqa: F401
CustomInitCommandSet,
)
from cmd2 import (
Cmd,
Cmd2ArgumentParser,
CommandSet,
with_argparser,
)
class WithCommandSets(Cmd):
def __init__(self, command_sets: Optional[Iterable[CommandSet]] = None):
super().__init__(command_sets=command_sets)
self.sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball']
def choices_provider(self) -> List[str]:
"""A choices provider is useful when the choice list is based on instance data of your application"""
return self.sport_item_strs
# Parser for example command
example_parser = Cmd2ArgumentParser(
description="Command demonstrating tab completion with argparse\n" "Notice even the flags of this command tab complete"
)
# Tab complete from a list using argparse choices. Set metavar if you don't
# want the entire choices list showing in the usage text for this command.
example_parser.add_argument(
'--choices', choices=['some', 'choices', 'here'], metavar="CHOICE", help="tab complete using choices"
)
# Tab complete from choices provided by a choices provider
example_parser.add_argument(
'--choices_provider', choices_provider=choices_provider, help="tab complete using a choices_provider"
)
# Tab complete using a completer
example_parser.add_argument('--completer', completer=Cmd.path_complete, help="tab complete using a completer")
@with_argparser(example_parser)
def do_example(self, _: argparse.Namespace) -> None:
"""The example command"""
self.poutput("I do nothing")
if __name__ == '__main__':
import sys
print("Starting")
my_sets = [CustomInitCommandSet('First argument', 'Second argument')]
app = WithCommandSets(command_sets=my_sets)
sys.exit(app.cmdloop())