summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/modular_commands/commandset_custominit.py2
-rw-r--r--examples/modular_commands_basic.py37
-rw-r--r--examples/modular_commands_dynamic.py86
-rw-r--r--examples/modular_commands_main.py5
-rwxr-xr-xexamples/plumbum_colors.py3
5 files changed, 128 insertions, 5 deletions
diff --git a/examples/modular_commands/commandset_custominit.py b/examples/modular_commands/commandset_custominit.py
index fa26644b..5a574a59 100644
--- a/examples/modular_commands/commandset_custominit.py
+++ b/examples/modular_commands/commandset_custominit.py
@@ -2,7 +2,7 @@
"""
A simple example demonstrating a loadable command set
"""
-from cmd2 import Cmd, CommandSet, Statement, with_category, with_default_category
+from cmd2 import Cmd, CommandSet, Statement, with_default_category
@with_default_category('Custom Init')
diff --git a/examples/modular_commands_basic.py b/examples/modular_commands_basic.py
new file mode 100644
index 00000000..9f4a0bd2
--- /dev/null
+++ b/examples/modular_commands_basic.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+# coding=utf-8
+"""
+Simple example demonstrating basic CommandSet usage.
+"""
+
+import cmd2
+from cmd2 import CommandSet, with_default_category
+
+
+@with_default_category('My Category')
+class AutoLoadCommandSet(CommandSet):
+ def __init__(self):
+ super().__init__()
+
+ def do_hello(self, cmd: cmd2.Cmd, _: cmd2.Statement):
+ cmd.poutput('Hello')
+
+ def do_world(self, cmd: cmd2.Cmd, _: cmd2.Statement):
+ cmd.poutput('World')
+
+
+class ExampleApp(cmd2.Cmd):
+ """
+ CommandSets are automatically loaded. Nothing needs to be done.
+ """
+
+ def __init__(self):
+ super(ExampleApp, self).__init__()
+
+ def do_something(self, arg):
+ self.poutput('this is the something command')
+
+
+if __name__ == '__main__':
+ app = ExampleApp()
+ app.cmdloop()
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()
diff --git a/examples/modular_commands_main.py b/examples/modular_commands_main.py
index fd10d8d3..b698e00f 100644
--- a/examples/modular_commands_main.py
+++ b/examples/modular_commands_main.py
@@ -1,7 +1,8 @@
#!/usr/bin/env python
# coding=utf-8
"""
-A simple example demonstrating how to integrate tab completion with argparse-based commands.
+A complex example demonstrating a variety of methods to load CommandSets using a mix of command decorators
+with examples of how to integrate tab completion with argparse-based commands.
"""
import argparse
from typing import Dict, Iterable, List, Optional
@@ -9,8 +10,8 @@ from typing import Dict, Iterable, List, Optional
from cmd2 import Cmd, Cmd2ArgumentParser, CommandSet, CompletionItem, with_argparser
from cmd2.utils import CompletionError, basic_complete
from modular_commands.commandset_basic import BasicCompletionCommandSet # noqa: F401
-from modular_commands.commandset_custominit import CustomInitCommandSet # noqa: F401
from modular_commands.commandset_complex import CommandSetA # noqa: F401
+from modular_commands.commandset_custominit import CustomInitCommandSet # noqa: F401
# Data source for argparse.choices
food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato']
diff --git a/examples/plumbum_colors.py b/examples/plumbum_colors.py
index ed65f245..a30e4c70 100755
--- a/examples/plumbum_colors.py
+++ b/examples/plumbum_colors.py
@@ -27,10 +27,9 @@ WARNING: This example requires the plumbum package, which isn't normally require
"""
import argparse
-from plumbum.colors import bg, fg
-
import cmd2
from cmd2 import ansi
+from plumbum.colors import bg, fg
class FgColors(ansi.ColorBase):