Skip to content

Commit 68f6ab0

Browse files
committed
Added required=True to add_subparsers() call in order to simplify implementation for do_calculate
1 parent f5b1d66 commit 68f6ab0

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

examples/argparse_example.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def do_print_unknown(self, args: argparse.Namespace, unknown: list[str]) -> None
108108

109109
# create the top-level parser for the base command
110110
calculate_parser = cmd2.Cmd2ArgumentParser(description="Perform simple mathematical calculations.")
111-
calculate_subparsers = calculate_parser.add_subparsers(title='operation', help='Available operations')
111+
calculate_subparsers = calculate_parser.add_subparsers(title='operation', help='Available operations', required=True)
112112

113113
# create the parser for the "add" subcommand
114114
add_description = "Add two numbers"
@@ -137,14 +137,10 @@ def subtract(self, args: argparse.Namespace) -> None:
137137

138138
@cmd2.with_argparser(calculate_parser)
139139
@cmd2.with_category(ARGPARSE_SUBCOMMANDS)
140-
def do_calculate(self, ns: argparse.Namespace) -> None:
140+
def do_calculate(self, args: argparse.Namespace) -> None:
141141
"""Calculate a simple mathematical operation on two integers."""
142-
handler = ns.cmd2_handler.get()
143-
if handler is not None:
144-
handler(ns)
145-
else:
146-
# No subcommand was provided, so call help
147-
self.do_help('calculate')
142+
handler = args.cmd2_handler.get()
143+
handler(args)
148144

149145

150146
if __name__ == '__main__':

0 commit comments

Comments
 (0)