diff options
| author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-06-16 16:14:14 -0400 |
|---|---|---|
| committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2021-06-17 14:10:24 -0400 |
| commit | ebb939ba0494648a7eb521023649816cd28be6c4 (patch) | |
| tree | da77637440bb65f26bcb05646fcb7396ddf97b8f /examples | |
| parent | 525d32cfde6c2f9fecb0ee44972c18d4b0bf614f (diff) | |
| download | cmd2-git-ebb939ba0494648a7eb521023649816cd28be6c4.tar.gz | |
Updated all examples to use Cmd2ArgumentParser instead of argparse.ArgumentParser.
This is best practice for consistency of appearance between built-in and custom commands.
Diffstat (limited to 'examples')
| -rwxr-xr-x | examples/arg_decorators.py | 2 | ||||
| -rwxr-xr-x | examples/arg_print.py | 5 | ||||
| -rwxr-xr-x | examples/cmd_as_argument.py | 6 | ||||
| -rwxr-xr-x | examples/colors.py | 3 | ||||
| -rwxr-xr-x | examples/decorator_example.py | 6 | ||||
| -rwxr-xr-x | examples/example.py | 5 | ||||
| -rwxr-xr-x | examples/first_app.py | 3 | ||||
| -rwxr-xr-x | examples/pirate.py | 3 | ||||
| -rwxr-xr-x | examples/plumbum_colors.py | 3 | ||||
| -rwxr-xr-x | examples/python_scripting.py | 3 | ||||
| -rwxr-xr-x | examples/subcommands.py | 5 |
11 files changed, 18 insertions, 26 deletions
diff --git a/examples/arg_decorators.py b/examples/arg_decorators.py index 8785fe4f..7e4f0f3a 100755 --- a/examples/arg_decorators.py +++ b/examples/arg_decorators.py @@ -42,7 +42,7 @@ class ArgparsingApp(cmd2.Cmd): self.poutput('{} {}'.format(size, args.unit)) # do_pow parser - pow_parser = argparse.ArgumentParser() + pow_parser = cmd2.Cmd2ArgumentParser() pow_parser.add_argument('base', type=int) pow_parser.add_argument('exponent', type=int, choices=range(-5, 6)) diff --git a/examples/arg_print.py b/examples/arg_print.py index 9663a67a..2cade9e4 100755 --- a/examples/arg_print.py +++ b/examples/arg_print.py @@ -9,7 +9,6 @@ experiment with and understand how command and argument parsing work. It also serves as an example of how to create shortcuts. """ -import argparse import cmd2 @@ -40,7 +39,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): """Print the argument list this basic command is called with (with quotes preserved).""" self.poutput('rprint was called with the following list of arguments: {!r}'.format(arglist)) - oprint_parser = argparse.ArgumentParser() + oprint_parser = cmd2.Cmd2ArgumentParser() oprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') oprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') oprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') @@ -51,7 +50,7 @@ class ArgumentAndOptionPrinter(cmd2.Cmd): """Print the options and argument list this options command was called with.""" self.poutput('oprint was called with the following\n\toptions: {!r}'.format(args)) - pprint_parser = argparse.ArgumentParser() + pprint_parser = cmd2.Cmd2ArgumentParser() pprint_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') pprint_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') pprint_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') diff --git a/examples/cmd_as_argument.py b/examples/cmd_as_argument.py index 88010668..75d53ed7 100755 --- a/examples/cmd_as_argument.py +++ b/examples/cmd_as_argument.py @@ -38,7 +38,7 @@ class CmdLineApp(cmd2.Cmd): # Make maxrepeats settable at runtime self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command', self)) - speak_parser = argparse.ArgumentParser() + speak_parser = cmd2.Cmd2ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') @@ -62,7 +62,7 @@ class CmdLineApp(cmd2.Cmd): do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input - mumble_parser = argparse.ArgumentParser() + mumble_parser = cmd2.Cmd2ArgumentParser() mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat') mumble_parser.add_argument('words', nargs='+', help='words to say') @@ -86,7 +86,7 @@ class CmdLineApp(cmd2.Cmd): def main(argv=None): """Run when invoked from the operating system shell""" - parser = argparse.ArgumentParser(description='Commands as arguments') + parser = cmd2.Cmd2ArgumentParser(description='Commands as arguments') command_help = 'optional command to run, if no command given, enter an interactive shell' parser.add_argument('command', nargs='?', help=command_help) arg_help = 'optional arguments for command' diff --git a/examples/colors.py b/examples/colors.py index 50c9b906..24a5cf09 100755 --- a/examples/colors.py +++ b/examples/colors.py @@ -23,7 +23,6 @@ Always poutput(), pfeedback(), and ppaged() never strip ANSI style sequences, regardless of the output destination """ -import argparse from typing import ( Any, ) @@ -54,7 +53,7 @@ class CmdLineApp(cmd2.Cmd): # Should ANSI color output be allowed self.allow_style = ansi.STYLE_TERMINAL - speak_parser = argparse.ArgumentParser() + speak_parser = cmd2.Cmd2ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') diff --git a/examples/decorator_example.py b/examples/decorator_example.py index 6848335c..75bc8dff 100755 --- a/examples/decorator_example.py +++ b/examples/decorator_example.py @@ -37,7 +37,7 @@ class CmdLineApp(cmd2.Cmd): # Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist # self.default_to_shell = True - speak_parser = argparse.ArgumentParser() + speak_parser = cmd2.Cmd2ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') @@ -60,7 +60,7 @@ class CmdLineApp(cmd2.Cmd): do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input - tag_parser = argparse.ArgumentParser() + tag_parser = cmd2.Cmd2ArgumentParser() tag_parser.add_argument('tag', help='tag') tag_parser.add_argument('content', nargs='+', help='content to surround with tag') @@ -89,7 +89,7 @@ if __name__ == '__main__': import sys # You can do your custom Argparse parsing here to meet your application's needs - parser = argparse.ArgumentParser(description='Process the arguments however you like.') + parser = cmd2.Cmd2ArgumentParser(description='Process the arguments however you like.') # Add a few arguments which aren't really used, but just to get the gist parser.add_argument('-p', '--port', type=int, help='TCP port') diff --git a/examples/example.py b/examples/example.py index 4c527076..da6c3c9f 100755 --- a/examples/example.py +++ b/examples/example.py @@ -10,7 +10,6 @@ Running `python example.py -t transcript_regex.txt` will run all the commands in the transcript against example.py, verifying that the output produced matches the transcript. """ -import argparse import random import cmd2 @@ -34,7 +33,7 @@ class CmdLineApp(cmd2.Cmd): self.maxrepeats = 3 self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command', self)) - speak_parser = argparse.ArgumentParser() + speak_parser = cmd2.Cmd2ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') @@ -58,7 +57,7 @@ class CmdLineApp(cmd2.Cmd): do_say = do_speak # now "say" is a synonym for "speak" do_orate = do_speak # another synonym, but this one takes multi-line input - mumble_parser = argparse.ArgumentParser() + mumble_parser = cmd2.Cmd2ArgumentParser() mumble_parser.add_argument('-r', '--repeat', type=int, help='how many times to repeat') mumble_parser.add_argument('words', nargs='+', help='words to say') diff --git a/examples/first_app.py b/examples/first_app.py index b6335e25..57a76f70 100755 --- a/examples/first_app.py +++ b/examples/first_app.py @@ -12,7 +12,6 @@ A simple application using cmd2 which demonstrates 8 key features: * Multiline Commands * History """ -import argparse import cmd2 @@ -29,7 +28,7 @@ class FirstApp(cmd2.Cmd): self.maxrepeats = 3 self.add_settable(cmd2.Settable('maxrepeats', int, 'max repetitions for speak command', self)) - speak_parser = argparse.ArgumentParser() + speak_parser = cmd2.Cmd2ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') diff --git a/examples/pirate.py b/examples/pirate.py index 3a9b7b36..2de832eb 100755 --- a/examples/pirate.py +++ b/examples/pirate.py @@ -6,7 +6,6 @@ presented as part of her PyCon 2010 talk. It demonstrates many features of cmd2. """ -import argparse import cmd2 import cmd2.ansi @@ -75,7 +74,7 @@ class Pirate(cmd2.Cmd): """Sing a colorful song.""" self.poutput(cmd2.ansi.style(arg, fg=self.songcolor)) - yo_parser = argparse.ArgumentParser() + yo_parser = cmd2.Cmd2ArgumentParser() yo_parser.add_argument('--ho', type=int, default=2, help="How often to chant 'ho'") yo_parser.add_argument('-c', '--commas', action='store_true', help='Intersperse commas') yo_parser.add_argument('beverage', help='beverage to drink with the chant') diff --git a/examples/plumbum_colors.py b/examples/plumbum_colors.py index d138c433..6b0625f7 100755 --- a/examples/plumbum_colors.py +++ b/examples/plumbum_colors.py @@ -25,7 +25,6 @@ Always WARNING: This example requires the plumbum package, which isn't normally required by cmd2. """ -import argparse from plumbum.colors import ( bg, @@ -88,7 +87,7 @@ class CmdLineApp(cmd2.Cmd): # Should ANSI color output be allowed self.allow_style = ansi.STYLE_TERMINAL - speak_parser = argparse.ArgumentParser() + speak_parser = cmd2.Cmd2ArgumentParser() speak_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') speak_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') speak_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') diff --git a/examples/python_scripting.py b/examples/python_scripting.py index 7e729202..39787085 100755 --- a/examples/python_scripting.py +++ b/examples/python_scripting.py @@ -20,7 +20,6 @@ scripts inside a cmd2 application via the run_pyscript command and the This application and the "examples/scripts/conditional.py" script serve as an example for one way in which this can be done. """ -import argparse import os import cmd2 @@ -95,7 +94,7 @@ class CmdLineApp(cmd2.Cmd): # Tab complete only directories return self.path_complete(text, line, begidx, endidx, path_filter=os.path.isdir) - dir_parser = argparse.ArgumentParser() + dir_parser = cmd2.Cmd2ArgumentParser() dir_parser.add_argument('-l', '--long', action='store_true', help="display in long format with one item per line") @cmd2.with_argparser(dir_parser, with_unknown_args=True) diff --git a/examples/subcommands.py b/examples/subcommands.py index 56c91b14..455768e3 100755 --- a/examples/subcommands.py +++ b/examples/subcommands.py @@ -6,14 +6,13 @@ This example shows an easy way for a single command to have many subcommands, each of which takes different arguments and provides separate contextual help. """ -import argparse import cmd2 sport_item_strs = ['Bat', 'Basket', 'Basketball', 'Football', 'Space Ball'] # create the top-level parser for the base command -base_parser = argparse.ArgumentParser() +base_parser = cmd2.Cmd2ArgumentParser() base_subparsers = base_parser.add_subparsers(title='subcommands', help='subcommand help') # create the parser for the "foo" subcommand @@ -39,7 +38,7 @@ sport_arg = parser_sport.add_argument('sport', help='Enter name of a sport', cho # create the top-level parser for the alternate command # The alternate command doesn't provide its own help flag -base2_parser = argparse.ArgumentParser(add_help=False) +base2_parser = cmd2.Cmd2ArgumentParser(add_help=False) base2_subparsers = base2_parser.add_subparsers(title='subcommands', help='subcommand help') # create the parser for the "foo" subcommand |
