summaryrefslogtreecommitdiff
path: root/examples/modular_subcommands.py
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-07 15:26:40 -0400
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-08-07 15:26:40 -0400
commit3ada4bd53925ca20fdeb16fa971814637dc42e4d (patch)
treecb8cbcf6595b797a35131fba7a94ad4f7afd463c /examples/modular_subcommands.py
parent2049496815bc0e46db2371f9173c0cef7496e706 (diff)
downloadcmd2-git-3ada4bd53925ca20fdeb16fa971814637dc42e4d.tar.gz
Moved code which copies subparser's settings to _register_subcommands().
Changed alias and macro commands to use as_subcommand_to() decorator. Updated CommandSet subcommand example to use help and description text.
Diffstat (limited to 'examples/modular_subcommands.py')
-rw-r--r--examples/modular_subcommands.py12
1 files changed, 7 insertions, 5 deletions
diff --git a/examples/modular_subcommands.py b/examples/modular_subcommands.py
index bf4a08ae..49620dfb 100644
--- a/examples/modular_subcommands.py
+++ b/examples/modular_subcommands.py
@@ -23,10 +23,11 @@ class LoadableFruits(CommandSet):
def do_apple(self, cmd: cmd2.Cmd, _: cmd2.Statement):
cmd.poutput('Apple')
- banana_parser = cmd2.Cmd2ArgumentParser(add_help=False)
+ banana_description = "Cut a banana"
+ banana_parser = cmd2.Cmd2ArgumentParser(add_help=False, description=banana_description)
banana_parser.add_argument('direction', choices=['discs', 'lengthwise'])
- @cmd2.as_subcommand_to('cut', 'banana', banana_parser)
+ @cmd2.as_subcommand_to('cut', 'banana', banana_parser, help=banana_description.lower())
def cut_banana(self, cmd: cmd2.Cmd, ns: argparse.Namespace):
"""Cut banana"""
cmd.poutput('cutting banana: ' + ns.direction)
@@ -40,10 +41,11 @@ class LoadableVegetables(CommandSet):
def do_arugula(self, cmd: cmd2.Cmd, _: cmd2.Statement):
cmd.poutput('Arugula')
- bokchoy_parser = cmd2.Cmd2ArgumentParser(add_help=False)
+ bokchoy_description = "Cut some bokchoy"
+ bokchoy_parser = cmd2.Cmd2ArgumentParser(add_help=False, description=bokchoy_description)
bokchoy_parser.add_argument('style', choices=['quartered', 'diced'])
- @cmd2.as_subcommand_to('cut', 'bokchoy', bokchoy_parser)
+ @cmd2.as_subcommand_to('cut', 'bokchoy', bokchoy_parser, help=bokchoy_description.lower())
def cut_bokchoy(self, cmd: cmd2.Cmd, _: cmd2.Statement):
cmd.poutput('Bok Choy')
@@ -95,9 +97,9 @@ class ExampleApp(cmd2.Cmd):
@with_argparser(cut_parser)
def do_cut(self, ns: argparse.Namespace):
+ # Call handler for whatever subcommand was selected
handler = ns.get_handler()
if handler is not None:
- # Call whatever subcommand function was selected
handler(ns)
else:
# No subcommand was provided, so call help