From 774fb39d7e259d0679c573b0d893293f9ed9aed9 Mon Sep 17 00:00:00 2001 From: Eric Lin Date: Wed, 12 Aug 2020 13:08:59 -0400 Subject: Breaking change: Removed cmd2 app as a required second parameter to CommandSet command functions (do_, complete_, help_). Renamed install_command_set and uninstall_command_set to register_command_set and unregister_command_set. --- examples/modular_commands/commandset_basic.py | 10 +++---- examples/modular_commands/commandset_complex.py | 32 +++++++++++----------- examples/modular_commands/commandset_custominit.py | 4 +-- examples/modular_commands_basic.py | 8 +++--- examples/modular_commands_dynamic.py | 24 ++++++++-------- examples/modular_subcommands.py | 24 ++++++++-------- 6 files changed, 51 insertions(+), 51 deletions(-) (limited to 'examples') diff --git a/examples/modular_commands/commandset_basic.py b/examples/modular_commands/commandset_basic.py index 105530e8..2ceda439 100644 --- a/examples/modular_commands/commandset_basic.py +++ b/examples/modular_commands/commandset_basic.py @@ -30,7 +30,7 @@ class BasicCompletionCommandSet(CommandSet): -s, --sport [completes sports] -p, --path [completes local file system paths] """ - cmd.poutput("Args: {}".format(statement.args)) + self._cmd.poutput("Args: {}".format(statement.args)) def complete_flag_based(self, cmd: Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]: """Completion function for do_flag_based""" @@ -53,7 +53,7 @@ class BasicCompletionCommandSet(CommandSet): def do_index_based(self, cmd: Cmd, statement: Statement): """Tab completes first 3 arguments using index_based_complete""" - cmd.poutput("Args: {}".format(statement.args)) + self._cmd.poutput("Args: {}".format(statement.args)) def complete_index_based(self, cmd: Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]: """Completion function for do_index_based""" @@ -68,14 +68,14 @@ class BasicCompletionCommandSet(CommandSet): def do_delimiter_complete(self, cmd: Cmd, statement: Statement): """Tab completes files from a list using delimiter_complete""" - cmd.poutput("Args: {}".format(statement.args)) + self._cmd.poutput("Args: {}".format(statement.args)) def complete_delimiter_complete(self, cmd: Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]: return cmd.delimiter_complete(text, line, begidx, endidx, match_against=self.file_strs, delimiter='/') def do_raise_error(self, cmd: Cmd, statement: Statement): """Demonstrates effect of raising CompletionError""" - cmd.poutput("Args: {}".format(statement.args)) + self._cmd.poutput("Args: {}".format(statement.args)) def complete_raise_error(self, cmd: Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]: """ @@ -89,4 +89,4 @@ class BasicCompletionCommandSet(CommandSet): @with_category('Not Basic Completion') def do_custom_category(self, cmd: Cmd, statement: Statement): - cmd.poutput('Demonstrates a command that bypasses the default category') + self._cmd.poutput('Demonstrates a command that bypasses the default category') diff --git a/examples/modular_commands/commandset_complex.py b/examples/modular_commands/commandset_complex.py index ec5a9e13..7c6b1300 100644 --- a/examples/modular_commands/commandset_complex.py +++ b/examples/modular_commands/commandset_complex.py @@ -13,35 +13,35 @@ 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_apple(self, statement: cmd2.Statement): + self._cmd.poutput('Apple!') - def do_banana(self, cmd: cmd2.Cmd, statement: cmd2.Statement): + def do_banana(self, statement: cmd2.Statement): """Banana Command""" - cmd.poutput('Banana!!') + self._cmd.poutput('Banana!!') cranberry_parser = cmd2.Cmd2ArgumentParser('cranberry') cranberry_parser.add_argument('arg1', choices=['lemonade', 'juice', 'sauce']) @cmd2.with_argparser(cranberry_parser, with_unknown_args=True) - def do_cranberry(self, cmd: cmd2.Cmd, ns: argparse.Namespace, unknown: List[str]): - cmd.poutput('Cranberry {}!!'.format(ns.arg1)) + def do_cranberry(self, ns: argparse.Namespace, unknown: List[str]): + self._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, + self._cmd.poutput('Unknown: ' + ', '.join(['{}']*len(unknown)).format(*unknown)) + self._cmd.last_result = {'arg1': ns.arg1, 'unknown': unknown} - def help_cranberry(self, cmd: cmd2.Cmd): - cmd.stdout.write('This command does diddly squat...\n') + def help_cranberry(self): + self._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]): + def do_durian(self, args: List[str]): """Durian Command""" - cmd.poutput('{} Arguments: '.format(len(args))) - cmd.poutput(', '.join(['{}']*len(args)).format(*args)) + self._cmd.poutput('{} Arguments: '.format(len(args))) + self._cmd.poutput(', '.join(['{}']*len(args)).format(*args)) - def complete_durian(self, cmd: cmd2.Cmd, text: str, line: str, begidx: int, endidx: int) -> List[str]: + def complete_durian(self, 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') @@ -49,5 +49,5 @@ class CommandSetA(cmd2.CommandSet): @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)) + def do_elderberry(self, ns: argparse.Namespace): + self._cmd.poutput('Elderberry {}!!'.format(ns.arg1)) diff --git a/examples/modular_commands/commandset_custominit.py b/examples/modular_commands/commandset_custominit.py index 5a574a59..2ef94a75 100644 --- a/examples/modular_commands/commandset_custominit.py +++ b/examples/modular_commands/commandset_custominit.py @@ -14,7 +14,7 @@ class CustomInitCommandSet(CommandSet): self._arg2 = arg2 def do_show_arg1(self, cmd: Cmd, _: Statement): - cmd.poutput('Arg1: ' + self._arg1) + self._cmd.poutput('Arg1: ' + self._arg1) def do_show_arg2(self, cmd: Cmd, _: Statement): - cmd.poutput('Arg2: ' + self._arg2) + self._cmd.poutput('Arg2: ' + self._arg2) diff --git a/examples/modular_commands_basic.py b/examples/modular_commands_basic.py index 9f4a0bd2..b9d4c4a2 100644 --- a/examples/modular_commands_basic.py +++ b/examples/modular_commands_basic.py @@ -13,11 +13,11 @@ class AutoLoadCommandSet(CommandSet): def __init__(self): super().__init__() - def do_hello(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Hello') + def do_hello(self, _: cmd2.Statement): + self._cmd.poutput('Hello') - def do_world(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('World') + def do_world(self, _: cmd2.Statement): + self._cmd.poutput('World') class ExampleApp(cmd2.Cmd): diff --git a/examples/modular_commands_dynamic.py b/examples/modular_commands_dynamic.py index 81dbad82..eb6283a7 100644 --- a/examples/modular_commands_dynamic.py +++ b/examples/modular_commands_dynamic.py @@ -19,11 +19,11 @@ class LoadableFruits(CommandSet): def __init__(self): super().__init__() - def do_apple(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Apple') + def do_apple(self, _: cmd2.Statement): + self._cmd.poutput('Apple') - def do_banana(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Banana') + def do_banana(self, _: cmd2.Statement): + self._cmd.poutput('Banana') @with_default_category('Vegetables') @@ -31,11 +31,11 @@ class LoadableVegetables(CommandSet): def __init__(self): super().__init__() - def do_arugula(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Arugula') + def do_arugula(self, _: cmd2.Statement): + self._cmd.poutput('Arugula') - def do_bokchoy(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Bok Choy') + def do_bokchoy(self, _: cmd2.Statement): + self._cmd.poutput('Bok Choy') class ExampleApp(cmd2.Cmd): @@ -58,14 +58,14 @@ class ExampleApp(cmd2.Cmd): def do_load(self, ns: argparse.Namespace): if ns.cmds == 'fruits': try: - self.install_command_set(self._fruits) + self.register_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.register_command_set(self._vegetables) self.poutput('Vegetables loaded') except ValueError: self.poutput('Vegetables already loaded') @@ -73,11 +73,11 @@ class ExampleApp(cmd2.Cmd): @with_argparser(load_parser) def do_unload(self, ns: argparse.Namespace): if ns.cmds == 'fruits': - self.uninstall_command_set(self._fruits) + self.unregister_command_set(self._fruits) self.poutput('Fruits unloaded') if ns.cmds == 'vegetables': - self.uninstall_command_set(self._vegetables) + self.unregister_command_set(self._vegetables) self.poutput('Vegetables unloaded') diff --git a/examples/modular_subcommands.py b/examples/modular_subcommands.py index 945fd54d..44d4edd8 100644 --- a/examples/modular_subcommands.py +++ b/examples/modular_subcommands.py @@ -20,17 +20,17 @@ class LoadableFruits(CommandSet): def __init__(self): super().__init__() - def do_apple(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Apple') + def do_apple(self, _: cmd2.Statement): + self._cmd.poutput('Apple') 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, help=banana_description.lower()) - def cut_banana(self, cmd: cmd2.Cmd, ns: argparse.Namespace): + def cut_banana(self, ns: argparse.Namespace): """Cut banana""" - cmd.poutput('cutting banana: ' + ns.direction) + self._cmd.poutput('cutting banana: ' + ns.direction) @with_default_category('Vegetables') @@ -38,16 +38,16 @@ class LoadableVegetables(CommandSet): def __init__(self): super().__init__() - def do_arugula(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Arugula') + def do_arugula(self, _: cmd2.Statement): + self._cmd.poutput('Arugula') 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, help=bokchoy_description.lower()) - def cut_bokchoy(self, cmd: cmd2.Cmd, _: cmd2.Statement): - cmd.poutput('Bok Choy') + def cut_bokchoy(self, _: cmd2.Statement): + self._cmd.poutput('Bok Choy') class ExampleApp(cmd2.Cmd): @@ -70,14 +70,14 @@ class ExampleApp(cmd2.Cmd): def do_load(self, ns: argparse.Namespace): if ns.cmds == 'fruits': try: - self.install_command_set(self._fruits) + self.register_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.register_command_set(self._vegetables) self.poutput('Vegetables loaded') except ValueError: self.poutput('Vegetables already loaded') @@ -85,11 +85,11 @@ class ExampleApp(cmd2.Cmd): @with_argparser(load_parser) def do_unload(self, ns: argparse.Namespace): if ns.cmds == 'fruits': - self.uninstall_command_set(self._fruits) + self.unregister_command_set(self._fruits) self.poutput('Fruits unloaded') if ns.cmds == 'vegetables': - self.uninstall_command_set(self._vegetables) + self.unregister_command_set(self._vegetables) self.poutput('Vegetables unloaded') cut_parser = cmd2.Cmd2ArgumentParser('cut') -- cgit v1.2.1