summaryrefslogtreecommitdiff
path: root/examples/modular_commands_dynamic.py
diff options
context:
space:
mode:
authorEric Lin <anselor@gmail.com>2020-07-24 12:21:43 -0400
committeranselor <anselor@gmail.com>2020-08-04 13:38:08 -0400
commit06cee9126839c465a356f8b44a5f008853eb8cad (patch)
tree88de1a9f07f20fb6a7e1a8f77b1c48fb41382d19 /examples/modular_commands_dynamic.py
parent787a31931ed4c4a18ae66a570d396b12b2b7b525 (diff)
downloadcmd2-git-06cee9126839c465a356f8b44a5f008853eb8cad.tar.gz
updated imports
Added additional documentation
Diffstat (limited to 'examples/modular_commands_dynamic.py')
-rw-r--r--examples/modular_commands_dynamic.py86
1 files changed, 86 insertions, 0 deletions
diff --git a/examples/modular_commands_dynamic.py b/examples/modular_commands_dynamic.py
new file mode 100644
index 00000000..81dbad82
--- /dev/null
+++ b/examples/modular_commands_dynamic.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+# coding=utf-8
+"""
+Simple example demonstrating dynamic CommandSet loading and unloading.
+
+There are 2 CommandSets defined. ExampleApp sets the `auto_load_commands` flag to false.
+
+The `load` and `unload` commands will load and unload the CommandSets. The available commands will change depending
+on which CommandSets are loaded
+"""
+
+import argparse
+import cmd2
+from cmd2 import CommandSet, with_argparser, with_category, with_default_category
+
+
+@with_default_category('Fruits')
+class LoadableFruits(CommandSet):
+ def __init__(self):
+ super().__init__()
+
+ def do_apple(self, cmd: cmd2.Cmd, _: cmd2.Statement):
+ cmd.poutput('Apple')
+
+ def do_banana(self, cmd: cmd2.Cmd, _: cmd2.Statement):
+ cmd.poutput('Banana')
+
+
+@with_default_category('Vegetables')
+class LoadableVegetables(CommandSet):
+ def __init__(self):
+ super().__init__()
+
+ def do_arugula(self, cmd: cmd2.Cmd, _: cmd2.Statement):
+ cmd.poutput('Arugula')
+
+ def do_bokchoy(self, cmd: cmd2.Cmd, _: cmd2.Statement):
+ cmd.poutput('Bok Choy')
+
+
+class ExampleApp(cmd2.Cmd):
+ """
+ CommandSets are loaded via the `load` and `unload` commands
+ """
+
+ def __init__(self, *args, **kwargs):
+ # gotta have this or neither the plugin or cmd2 will initialize
+ super().__init__(*args, auto_load_commands=False, **kwargs)
+
+ self._fruits = LoadableFruits()
+ self._vegetables = LoadableVegetables()
+
+ load_parser = cmd2.Cmd2ArgumentParser('load')
+ load_parser.add_argument('cmds', choices=['fruits', 'vegetables'])
+
+ @with_argparser(load_parser)
+ @with_category('Command Loading')
+ def do_load(self, ns: argparse.Namespace):
+ if ns.cmds == 'fruits':
+ try:
+ self.install_command_set(self._fruits)
+ self.poutput('Fruits loaded')
+ except ValueError:
+ self.poutput('Fruits already loaded')
+
+ if ns.cmds == 'vegetables':
+ try:
+ self.install_command_set(self._vegetables)
+ self.poutput('Vegetables loaded')
+ except ValueError:
+ self.poutput('Vegetables already loaded')
+
+ @with_argparser(load_parser)
+ def do_unload(self, ns: argparse.Namespace):
+ if ns.cmds == 'fruits':
+ self.uninstall_command_set(self._fruits)
+ self.poutput('Fruits unloaded')
+
+ if ns.cmds == 'vegetables':
+ self.uninstall_command_set(self._vegetables)
+ self.poutput('Vegetables unloaded')
+
+
+if __name__ == '__main__':
+ app = ExampleApp()
+ app.cmdloop()