27
27
28
28
29
29
class ArgparsingApp (cmd2 .Cmd ):
30
- def __init__ (self , color ) -> None :
30
+ def __init__ (self , color : str ) -> None :
31
+ """Cmd2 application for demonstrating the use of argparse for command argument parsing."""
31
32
super ().__init__ (include_ipy = True )
32
33
self .intro = stylize (
33
34
'cmd2 has awesome decorators to make it easy to use Argparse to parse command arguments' , style = color
@@ -54,16 +55,15 @@ def do_fsize(self, args: argparse.Namespace) -> None:
54
55
return
55
56
56
57
if args .unit == 'KB' :
57
- size /= 1024
58
+ size // = 1024
58
59
elif args .unit == 'MB' :
59
- size /= 1024 * 1024
60
+ size // = 1024 * 1024
60
61
else :
61
62
args .unit = 'bytes'
62
63
size = round (size , 2 )
63
64
64
- if args .comma :
65
- size = f'{ size :,} '
66
- self .poutput (f'{ size } { args .unit } ' )
65
+ size_str = f'{ size :,} ' if args .comma else f'{ size } '
66
+ self .poutput (f'{ size_str } { args .unit } ' )
67
67
68
68
# do_pow parser
69
69
pow_parser = cmd2 .Cmd2ArgumentParser ()
@@ -89,7 +89,7 @@ def do_pow(self, args: argparse.Namespace) -> None:
89
89
90
90
@cmd2 .with_argparser (argprint_parser )
91
91
@cmd2 .with_category (ARGPARSE_PRINTING )
92
- def do_print_args (self , args ) -> None :
92
+ def do_print_args (self , args : argparse . Namespace ) -> None :
93
93
"""Print the arpgarse argument list this command was called with."""
94
94
self .poutput (f'print_args was called with the following\n \t arguments: { args !r} ' )
95
95
@@ -100,7 +100,7 @@ def do_print_args(self, args) -> None:
100
100
101
101
@cmd2 .with_argparser (unknownprint_parser , with_unknown_args = True )
102
102
@cmd2 .with_category (ARGPARSE_PRINTING )
103
- def do_print_unknown (self , args , unknown ) -> None :
103
+ def do_print_unknown (self , args : argparse . Namespace , unknown : list [ str ] ) -> None :
104
104
"""Print the arpgarse argument list this command was called with, including unknown arguments."""
105
105
self .poutput (f'print_unknown was called with the following arguments\n \t known: { args !r} \n \t unknown: { unknown } ' )
106
106
@@ -158,15 +158,15 @@ def do_print_unknown(self, args, unknown) -> None:
158
158
sport2_arg = parser_sport2 .add_argument ('sport' , help = 'Enter name of a sport' , choices = sport_item_strs )
159
159
160
160
# subcommand functions for the base command
161
- def base_foo (self , args ) -> None :
161
+ def base_foo (self , args : argparse . Namespace ) -> None :
162
162
"""Foo subcommand of base command."""
163
163
self .poutput (args .x * args .y )
164
164
165
- def base_bar (self , args ) -> None :
165
+ def base_bar (self , args : argparse . Namespace ) -> None :
166
166
"""Bar subcommand of base command."""
167
167
self .poutput (f'(({ args .z } ))' )
168
168
169
- def base_sport (self , args ) -> None :
169
+ def base_sport (self , args : argparse . Namespace ) -> None :
170
170
"""Sport subcommand of base command."""
171
171
self .poutput (f'Sport is { args .sport } ' )
172
172
@@ -177,7 +177,7 @@ def base_sport(self, args) -> None:
177
177
178
178
@cmd2 .with_argparser (base_parser )
179
179
@cmd2 .with_category (ARGPARSE_SUBCOMMANDS )
180
- def do_base (self , args ) -> None :
180
+ def do_base (self , args : argparse . Namespace ) -> None :
181
181
"""Base command help."""
182
182
func = getattr (args , 'func' , None )
183
183
if func is not None :
@@ -189,7 +189,7 @@ def do_base(self, args) -> None:
189
189
190
190
@cmd2 .with_argparser (base2_parser )
191
191
@cmd2 .with_category (ARGPARSE_SUBCOMMANDS )
192
- def do_alternate (self , args ) -> None :
192
+ def do_alternate (self , args : argparse . Namespace ) -> None :
193
193
"""Alternate command help."""
194
194
func = getattr (args , 'func' , None )
195
195
if func is not None :
0 commit comments