1
1
#!/usr/bin/env python3
2
- """This example revolves around the CommandSet feature for modularizing commands.
2
+ """Example revolving around the CommandSet feature for modularizing commands.
3
3
4
4
It attempts to cover basic usage as well as more complex usage including dynamic loading and unloading of CommandSets, using
5
5
CommandSets to add subcommands, as well as how to categorize command in CommandSets. Here we have kept the implementation for
32
32
@with_default_category (COMMANDSET_BASIC )
33
33
class AutoLoadCommandSet (CommandSet ):
34
34
def __init__ (self ) -> None :
35
+ """CommandSet class for auto-loading commands at startup."""
35
36
super ().__init__ ()
36
37
37
38
def do_hello (self , _ : cmd2 .Statement ) -> None :
38
- """Prints hello."""
39
+ """Print hello."""
39
40
self ._cmd .poutput ('Hello' )
40
41
41
42
def do_world (self , _ : cmd2 .Statement ) -> None :
42
- """Prints World."""
43
+ """Print World."""
43
44
self ._cmd .poutput ('World' )
44
45
45
46
46
47
@with_default_category (COMMANDSET_DYNAMIC )
47
48
class LoadableFruits (CommandSet ):
48
49
def __init__ (self ) -> None :
50
+ """CommandSet class for dynamically loading commands related to fruits."""
49
51
super ().__init__ ()
50
52
51
53
def do_apple (self , _ : cmd2 .Statement ) -> None :
52
- """Prints Apple."""
54
+ """Print Apple."""
53
55
self ._cmd .poutput ('Apple' )
54
56
55
57
def do_banana (self , _ : cmd2 .Statement ) -> None :
56
- """Prints Banana"""
58
+ """Print Banana. """
57
59
self ._cmd .poutput ('Banana' )
58
60
59
61
banana_description = "Cut a banana"
@@ -69,14 +71,15 @@ def cut_banana(self, ns: argparse.Namespace) -> None:
69
71
@with_default_category (COMMANDSET_DYNAMIC )
70
72
class LoadableVegetables (CommandSet ):
71
73
def __init__ (self ) -> None :
74
+ """CommandSet class for dynamically loading commands related to vegetables."""
72
75
super ().__init__ ()
73
76
74
77
def do_arugula (self , _ : cmd2 .Statement ) -> None :
75
- "Prints Arguula."
78
+ "Print Arguula."
76
79
self ._cmd .poutput ('Arugula' )
77
80
78
81
def do_bokchoy (self , _ : cmd2 .Statement ) -> None :
79
- """Prints Bok Choy."""
82
+ """Print Bok Choy."""
80
83
self ._cmd .poutput ('Bok Choy' )
81
84
82
85
bokchoy_description = "Cut some bokchoy"
@@ -85,13 +88,15 @@ def do_bokchoy(self, _: cmd2.Statement) -> None:
85
88
86
89
@cmd2 .as_subcommand_to ('cut' , 'bokchoy' , bokchoy_parser , help = bokchoy_description .lower ())
87
90
def cut_bokchoy (self , ns : argparse .Namespace ) -> None :
91
+ """Cut bokchoy."""
88
92
self ._cmd .poutput ('Bok Choy: ' + ns .style )
89
93
90
94
91
95
class CommandSetApp (cmd2 .Cmd ):
92
96
"""CommandSets are automatically loaded. Nothing needs to be done."""
93
97
94
98
def __init__ (self ) -> None :
99
+ """Cmd2 application for demonstrating the CommandSet features."""
95
100
# This prevents all CommandSets from auto-loading, which is necessary if you don't want some to load at startup
96
101
super ().__init__ (auto_load_commands = False )
97
102
@@ -109,6 +114,7 @@ def __init__(self) -> None:
109
114
@with_argparser (load_parser )
110
115
@with_category (COMMANDSET_LOAD_UNLOAD )
111
116
def do_load (self , ns : argparse .Namespace ) -> None :
117
+ """Load a CommandSet at runtime."""
112
118
if ns .cmds == 'fruits' :
113
119
try :
114
120
self .register_command_set (self ._fruits )
@@ -126,6 +132,7 @@ def do_load(self, ns: argparse.Namespace) -> None:
126
132
@with_argparser (load_parser )
127
133
@with_category (COMMANDSET_LOAD_UNLOAD )
128
134
def do_unload (self , ns : argparse .Namespace ) -> None :
135
+ """Unload a CommandSet at runtime."""
129
136
if ns .cmds == 'fruits' :
130
137
self .unregister_command_set (self ._fruits )
131
138
self .poutput ('Fruits unloaded' )
0 commit comments