summaryrefslogtreecommitdiff
path: root/examples/modular_commands/commandset_complex.py
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-07-24 12:03:59 -0400
committeranselor <anselor@gmail.com>2020-08-04 13:38:08 -0400
commit787a31931ed4c4a18ae66a570d396b12b2b7b525 (patch)
tree89c103c71177ad8b0cac2e1355866c05a6c0c743 /examples/modular_commands/commandset_complex.py
parentc05d2b0480f284a83dfe868b1aad3d780174c289 (diff)
downloadcmd2-git-787a31931ed4c4a18ae66a570d396b12b2b7b525.tar.gz
Updates the example to remove usage of the now remove ability to
register arbitrary functions as commands. Added example that demonstrates use of each of the command decorators with CommandSets. Adds unit test that verifies that CommandSets containing decorators load and process commands correctly. Updated the constructor declaration for Cmd2ArgumentParser to explicitly re-declare argparse constructor parameters.
Diffstat (limited to 'examples/modular_commands/commandset_complex.py')
-rw-r--r--examples/modular_commands/commandset_complex.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/examples/modular_commands/commandset_complex.py b/examples/modular_commands/commandset_complex.py
new file mode 100644
index 00000000..5a031bd0
--- /dev/null
+++ b/examples/modular_commands/commandset_complex.py
@@ -0,0 +1,53 @@
+# coding=utf-8
+# flake8: noqa E302
+"""
+Test CommandSet
+"""
+
+import argparse
+from typing import List
+
+import cmd2
+from cmd2 import utils
+
+
+@cmd2.with_default_category('Fruits')
+class CommandSetA(cmd2.CommandSet):
+ def do_apple(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ cmd.poutput('Apple!')
+
+ def do_banana(self, cmd: cmd2.Cmd, statement: cmd2.Statement):
+ """Banana Command"""
+ cmd.poutput('Banana!!')
+
+ cranberry_parser = cmd2.Cmd2ArgumentParser('cranberry')
+ cranberry_parser.add_argument('arg1', choices=['lemonade', 'juice', 'sauce'])
+
+ @cmd2.with_argparser_and_unknown_args(cranberry_parser)
+ def do_cranberry(self, cmd: cmd2.Cmd, ns: argparse.Namespace, unknown: List[str]):
+ cmd.poutput('Cranberry {}!!'.format(ns.arg1))
+ if unknown and len(unknown):
+ cmd.poutput('Unknown: ' + ', '.join(['{}']*len(unknown)).format(*unknown))
+ cmd.last_result = {'arg1': ns.arg1,
+ 'unknown': unknown}
+
+ def help_cranberry(self, cmd: cmd2.Cmd):
+ cmd.stdout.write('This command does diddly squat...\n')
+
+ @cmd2.with_argument_list
+ @cmd2.with_category('Also Alone')
+ def do_durian(self, cmd: cmd2.Cmd, args: List[str]):
+ """Durian Command"""
+ cmd.poutput('{} Arguments: '.format(len(args)))
+ cmd.poutput(', '.join(['{}']*len(args)).format(*args))
+
+ def complete_durian(self, cmd: cmd2.Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]:
+ return utils.basic_complete(text, line, begidx, endidx, ['stinks', 'smells', 'disgusting'])
+
+ elderberry_parser = cmd2.Cmd2ArgumentParser('elderberry')
+ elderberry_parser.add_argument('arg1')
+
+ @cmd2.with_category('Alone')
+ @cmd2.with_argparser(elderberry_parser)
+ def do_elderberry(self, cmd: cmd2.Cmd, ns: argparse.Namespace):
+ cmd.poutput('Elderberry {}!!'.format(ns.arg1))