diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2020-08-07 18:01:12 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-07 18:01:12 -0400 |
commit | 267434118955c2edfc59042d4a8c9515f63ef719 (patch) | |
tree | df3c5326f4110a680c8afe05cfab9da728afaff9 /examples/modular_subcommands.py | |
parent | 47568c04373f679de2a39ef1d6f3168150d9c368 (diff) | |
parent | a04fdb50b372b3e1dda5200cc56150b2079ea596 (diff) | |
download | cmd2-git-267434118955c2edfc59042d4a8c9515f63ef719.tar.gz |
Merge pull request #969 from python-cmd2/subcmd_fix
Subcmd fix
Diffstat (limited to 'examples/modular_subcommands.py')
-rw-r--r-- | examples/modular_subcommands.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/examples/modular_subcommands.py b/examples/modular_subcommands.py index bf4a08ae..945fd54d 100644 --- a/examples/modular_subcommands.py +++ b/examples/modular_subcommands.py @@ -1,6 +1,6 @@ #!/usr/bin/env python3 # coding=utf-8 -"""A simple example demonstracting modular subcommand loading through CommandSets +"""A simple example demonstrating modular subcommand loading through CommandSets In this example, there are loadable CommandSets defined. Each CommandSet has 1 subcommand defined that will be attached to the 'cut' command. @@ -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 |