diff options
-rwxr-xr-x | examples/arg_print.py | 14 | ||||
-rwxr-xr-x | examples/help_categories.py | 6 | ||||
-rwxr-xr-x | examples/pirate.py | 4 | ||||
-rwxr-xr-x | examples/tab_autocompletion.py | 5 |
4 files changed, 17 insertions, 12 deletions
diff --git a/examples/arg_print.py b/examples/arg_print.py index 1e6babc1..4f0ca709 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -2,10 +2,11 @@ # coding=utf-8 """A simple example demonstrating the following: 1) How arguments and options get parsed and passed to commands - 2) How to change what syntax get parsed as a comment and stripped from the arguments + 2) How to change what syntax get parsed as a comment and stripped from + the arguments -This is intended to serve as a live demonstration so that developers can experiment with and understand how command -and argument parsing is intended to work. +This is intended to serve as a live demonstration so that developers can +experiment with and understand how command and argument parsing work. It also serves as an example of how to create command aliases (shortcuts). """ @@ -25,9 +26,12 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): # NOTE: It is critical that the super class __init__ method be called AFTER updating certain parameters which # are not settable at runtime. This includes the shortcuts, multiline_commands, etc. - def do_aprint(self, arg): + def do_aprint(self, statement): """Print the argument string this basic command is called with.""" - self.poutput('aprint was called with argument: {!r}'.format(arg)) + self.poutput('aprint was called with argument: {!r}'.format(statement)) + self.poutput('statement.raw = {!r}'.format(statement.raw)) + self.poutput('statement.argv = {!r}'.format(statement.argv)) + self.poutput('statement.command = {!r}'.format(statement.command)) @cmd2.with_argument_list def do_lprint(self, arglist): diff --git a/examples/help_categories.py b/examples/help_categories.py index a8cf528c..b4a2c977 100755 --- a/examples/help_categories.py +++ b/examples/help_categories.py @@ -25,7 +25,7 @@ class HelpCategories(cmd2.Cmd): self.poutput('Connect') # Tag the above command functions under the category Connecting - categorize(do_connect, CMD_CAT_CONNECTING) + cmd2.categorize(do_connect, CMD_CAT_CONNECTING) @cmd2.with_category(CMD_CAT_CONNECTING) def do_which(self, _): @@ -80,7 +80,7 @@ class HelpCategories(cmd2.Cmd): self.poutput('Find Leakers') # Tag the above command functions under the category Application Management - categorize((do_list, + cmd2.categorize((do_list, do_deploy, do_start, do_sessions, @@ -137,7 +137,7 @@ class HelpCategories(cmd2.Cmd): def do_version(self, _): """Version command""" - self.poutput(__version__) + self.poutput(cmd2.__version__) if __name__ == '__main__': diff --git a/examples/pirate.py b/examples/pirate.py index 9da634aa..34906a9f 100755 --- a/examples/pirate.py +++ b/examples/pirate.py @@ -16,7 +16,7 @@ class Pirate(cmd2.Cmd): def __init__(self): self.default_to_shell = True self.multiline_commands = ['sing'] - self.terminators = Cmd.terminators + ['...'] + self.terminators = self.terminators + ['...'] self.songcolor = 'blue' # Add stuff to settable and/or shortcuts before calling base class initializer @@ -75,7 +75,7 @@ class Pirate(cmd2.Cmd): yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas') yo_parser.add_argument('beverage', help='beverage to drink with the chant') - @cmd.with_argparser(yo_parser) + @cmd2.with_argparser(yo_parser) def do_yo(self, args): """Compose a yo-ho-ho type chant with flexible options.""" chant = ['yo'] + ['ho'] * args.ho diff --git a/examples/tab_autocompletion.py b/examples/tab_autocompletion.py index e2c5b3da..d1726841 100755 --- a/examples/tab_autocompletion.py +++ b/examples/tab_autocompletion.py @@ -11,6 +11,7 @@ import itertools from typing import List import cmd2 +from cmd2 import argparse_completer actors = ['Mark Hamill', 'Harrison Ford', 'Carrie Fisher', 'Alec Guinness', 'Peter Mayhew', 'Anthony Daniels', 'Adam Driver', 'Daisy Ridley', 'John Boyega', 'Oscar Isaac', @@ -113,7 +114,7 @@ class TabCompleteExample(cmd2.Cmd): # - The help output for arguments with multiple flags or with append=True is more concise # - ACArgumentParser adds the ability to specify ranges of argument counts in 'nargs' - suggest_parser = cmd2.argparse_completer.ACArgumentParser() + suggest_parser = argparse_completer.ACArgumentParser() suggest_parser.add_argument('-t', '--type', choices=['movie', 'show'], required=True) suggest_parser.add_argument('-d', '--duration', nargs=(1, 2), action='append', @@ -138,7 +139,7 @@ class TabCompleteExample(cmd2.Cmd): suggest_parser_hybrid = argparse.ArgumentParser() # This registers the custom narg range handling - cmd2.argparse_completer.register_custom_actions(suggest_parser_hybrid) + argparse_completer.register_custom_actions(suggest_parser_hybrid) suggest_parser_hybrid.add_argument('-t', '--type', choices=['movie', 'show'], required=True) suggest_parser_hybrid.add_argument('-d', '--duration', nargs=(1, 2), action='append', |