1
1
#!/usr/bin/env python3
2
2
# coding=utf-8
3
- """A simple example demonstracting modular subcommand loading through CommandSets
3
+ """A simple example demonstrating modular subcommand loading through CommandSets
4
4
5
5
In this example, there are loadable CommandSets defined. Each CommandSet has 1 subcommand defined that will be
6
6
attached to the 'cut' command.
@@ -23,10 +23,11 @@ def __init__(self):
23
23
def do_apple (self , cmd : cmd2 .Cmd , _ : cmd2 .Statement ):
24
24
cmd .poutput ('Apple' )
25
25
26
- banana_parser = cmd2 .Cmd2ArgumentParser (add_help = False )
26
+ banana_description = "Cut a banana"
27
+ banana_parser = cmd2 .Cmd2ArgumentParser (add_help = False , description = banana_description )
27
28
banana_parser .add_argument ('direction' , choices = ['discs' , 'lengthwise' ])
28
29
29
- @cmd2 .as_subcommand_to ('cut' , 'banana' , banana_parser )
30
+ @cmd2 .as_subcommand_to ('cut' , 'banana' , banana_parser , help = banana_description . lower () )
30
31
def cut_banana (self , cmd : cmd2 .Cmd , ns : argparse .Namespace ):
31
32
"""Cut banana"""
32
33
cmd .poutput ('cutting banana: ' + ns .direction )
@@ -40,10 +41,11 @@ def __init__(self):
40
41
def do_arugula (self , cmd : cmd2 .Cmd , _ : cmd2 .Statement ):
41
42
cmd .poutput ('Arugula' )
42
43
43
- bokchoy_parser = cmd2 .Cmd2ArgumentParser (add_help = False )
44
+ bokchoy_description = "Cut some bokchoy"
45
+ bokchoy_parser = cmd2 .Cmd2ArgumentParser (add_help = False , description = bokchoy_description )
44
46
bokchoy_parser .add_argument ('style' , choices = ['quartered' , 'diced' ])
45
47
46
- @cmd2 .as_subcommand_to ('cut' , 'bokchoy' , bokchoy_parser )
48
+ @cmd2 .as_subcommand_to ('cut' , 'bokchoy' , bokchoy_parser , help = bokchoy_description . lower () )
47
49
def cut_bokchoy (self , cmd : cmd2 .Cmd , _ : cmd2 .Statement ):
48
50
cmd .poutput ('Bok Choy' )
49
51
@@ -95,9 +97,9 @@ def do_unload(self, ns: argparse.Namespace):
95
97
96
98
@with_argparser (cut_parser )
97
99
def do_cut (self , ns : argparse .Namespace ):
100
+ # Call handler for whatever subcommand was selected
98
101
handler = ns .get_handler ()
99
102
if handler is not None :
100
- # Call whatever subcommand function was selected
101
103
handler (ns )
102
104
else :
103
105
# No subcommand was provided, so call help
0 commit comments