-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathmodular_commandsets.py
More file actions
executable file
·69 lines (54 loc) · 2.39 KB
/
modular_commandsets.py
File metadata and controls
executable file
·69 lines (54 loc) · 2.39 KB
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
#!/usr/bin/env python
"""A complex example demonstrating a variety of methods to load CommandSets using a mix of command decorators.
Includes examples of how to integrate tab completion with argparse-based commands.
"""
import argparse
from collections.abc import Iterable
from modular_commands.commandset_basic import (
BasicCompletionCommandSet,
)
from modular_commands.commandset_complex import (
CommandSetA,
)
from modular_commands.commandset_custominit import (
CustomInitCommandSet,
)
from cmd2 import (
Cmd,
Cmd2ArgumentParser,
CommandSet,
with_argparser,
)
class WithCommandSets(Cmd):
def __init__(self, command_sets: Iterable[CommandSet] | None = None) -> None:
"""Cmd2 application to demonstrate a variety of methods for loading CommandSets."""
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\nNotice 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:
"""An example command."""
self.poutput("I do nothing")
if __name__ == "__main__":
import sys
print("Starting")
my_sets = [BasicCompletionCommandSet(), CommandSetA(), CustomInitCommandSet("First argument", "Second argument")]
app = WithCommandSets(command_sets=my_sets)
sys.exit(app.cmdloop())