Skip to content

Commit 39b877a

Browse files
committed
Added type hints to argparse_example.py and fixed other ruff issues
1 parent d5f5c02 commit 39b877a

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

examples/argparse_example.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828

2929
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."""
3132
super().__init__(include_ipy=True)
3233
self.intro = stylize(
3334
'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:
5455
return
5556

5657
if args.unit == 'KB':
57-
size /= 1024
58+
size //= 1024
5859
elif args.unit == 'MB':
59-
size /= 1024 * 1024
60+
size //= 1024 * 1024
6061
else:
6162
args.unit = 'bytes'
6263
size = round(size, 2)
6364

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}')
6767

6868
# do_pow parser
6969
pow_parser = cmd2.Cmd2ArgumentParser()
@@ -89,7 +89,7 @@ def do_pow(self, args: argparse.Namespace) -> None:
8989

9090
@cmd2.with_argparser(argprint_parser)
9191
@cmd2.with_category(ARGPARSE_PRINTING)
92-
def do_print_args(self, args) -> None:
92+
def do_print_args(self, args: argparse.Namespace) -> None:
9393
"""Print the arpgarse argument list this command was called with."""
9494
self.poutput(f'print_args was called with the following\n\targuments: {args!r}')
9595

@@ -100,7 +100,7 @@ def do_print_args(self, args) -> None:
100100

101101
@cmd2.with_argparser(unknownprint_parser, with_unknown_args=True)
102102
@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:
104104
"""Print the arpgarse argument list this command was called with, including unknown arguments."""
105105
self.poutput(f'print_unknown was called with the following arguments\n\tknown: {args!r}\n\tunknown: {unknown}')
106106

@@ -158,15 +158,15 @@ def do_print_unknown(self, args, unknown) -> None:
158158
sport2_arg = parser_sport2.add_argument('sport', help='Enter name of a sport', choices=sport_item_strs)
159159

160160
# subcommand functions for the base command
161-
def base_foo(self, args) -> None:
161+
def base_foo(self, args: argparse.Namespace) -> None:
162162
"""Foo subcommand of base command."""
163163
self.poutput(args.x * args.y)
164164

165-
def base_bar(self, args) -> None:
165+
def base_bar(self, args: argparse.Namespace) -> None:
166166
"""Bar subcommand of base command."""
167167
self.poutput(f'(({args.z}))')
168168

169-
def base_sport(self, args) -> None:
169+
def base_sport(self, args: argparse.Namespace) -> None:
170170
"""Sport subcommand of base command."""
171171
self.poutput(f'Sport is {args.sport}')
172172

@@ -177,7 +177,7 @@ def base_sport(self, args) -> None:
177177

178178
@cmd2.with_argparser(base_parser)
179179
@cmd2.with_category(ARGPARSE_SUBCOMMANDS)
180-
def do_base(self, args) -> None:
180+
def do_base(self, args: argparse.Namespace) -> None:
181181
"""Base command help."""
182182
func = getattr(args, 'func', None)
183183
if func is not None:
@@ -189,7 +189,7 @@ def do_base(self, args) -> None:
189189

190190
@cmd2.with_argparser(base2_parser)
191191
@cmd2.with_category(ARGPARSE_SUBCOMMANDS)
192-
def do_alternate(self, args) -> None:
192+
def do_alternate(self, args: argparse.Namespace) -> None:
193193
"""Alternate command help."""
194194
func = getattr(args, 'func', None)
195195
if func is not None:

0 commit comments

Comments
 (0)