diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/modular_commands/commandset_basic.py | 31 | ||||
-rw-r--r-- | examples/modular_commands/commandset_complex.py | 53 | ||||
-rw-r--r-- | examples/modular_commands/commandset_custominit.py | 14 | ||||
-rw-r--r-- | examples/modular_commands_main.py | 1 |
4 files changed, 56 insertions, 43 deletions
diff --git a/examples/modular_commands/commandset_basic.py b/examples/modular_commands/commandset_basic.py index 25ba976d..105530e8 100644 --- a/examples/modular_commands/commandset_basic.py +++ b/examples/modular_commands/commandset_basic.py @@ -4,39 +4,10 @@ A simple example demonstrating a loadable command set """ from typing import List -from cmd2 import Cmd, CommandSet, Statement, register_command, with_category, with_default_category +from cmd2 import Cmd, CommandSet, Statement, with_category, with_default_category from cmd2.utils import CompletionError -@register_command -@with_category("AAA") -def do_unbound(cmd: Cmd, statement: Statement): - """This is an example of registering an unbound function - - :param cmd: - :param statement: - :return: - """ - cmd.poutput('Unbound Command: {}'.format(statement.args)) - - -@register_command -@with_category("AAA") -def do_func_with_help(cmd: Cmd, statement: Statement): - """ - This is an example of registering an unbound function - - :param cmd: - :param statement: - :return: - """ - cmd.poutput('Unbound Command: {}'.format(statement.args)) - - -def help_func_with_help(cmd: Cmd): - cmd.poutput('Help for func_with_help') - - @with_default_category('Basic Completion') class BasicCompletionCommandSet(CommandSet): # List of strings used with completion functions 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)) diff --git a/examples/modular_commands/commandset_custominit.py b/examples/modular_commands/commandset_custominit.py index d96c5f1c..fa26644b 100644 --- a/examples/modular_commands/commandset_custominit.py +++ b/examples/modular_commands/commandset_custominit.py @@ -2,19 +2,7 @@ """ A simple example demonstrating a loadable command set """ -from cmd2 import Cmd, CommandSet, Statement, register_command, with_category, with_default_category - - -@register_command -@with_category("AAA") -def do_another_command(cmd: Cmd, statement: Statement): - """ - This is an example of registering an unbound function - :param cmd: - :param statement: - :return: - """ - cmd.poutput('Another Unbound Command: {}'.format(statement.args)) +from cmd2 import Cmd, CommandSet, Statement, with_category, with_default_category @with_default_category('Custom Init') diff --git a/examples/modular_commands_main.py b/examples/modular_commands_main.py index 9e7f79cc..fd10d8d3 100644 --- a/examples/modular_commands_main.py +++ b/examples/modular_commands_main.py @@ -10,6 +10,7 @@ from cmd2 import Cmd, Cmd2ArgumentParser, CommandSet, CompletionItem, with_argpa 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 # Data source for argparse.choices food_item_strs = ['Pizza', 'Ham', 'Ham Sandwich', 'Potato'] |