diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-15 01:34:50 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-01-15 01:34:50 -0500 |
commit | 141d95194b30d959f6c21f4546100551c442b13d (patch) | |
tree | 5d1602a6d2117aaa4910adfff31ad919b85fea07 /tests | |
parent | 2fcc883f8a7fd5c9fd3beb30465cfa2b86c66ffc (diff) | |
download | cmd2-git-141d95194b30d959f6c21f4546100551c442b13d.tar.gz |
Made a couple cleanup changes
1) cmd2 no longer imports make_option from optparse
- test files and examples now import this directly
- this helps emphasize that this old optparse methodology of adding options to commands is deprecated
2) All argparsers have been given custom names instead of just "argparser"
- this helps with readability and maintainability, especially with IDE renaming and such
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_argparse.py | 42 | ||||
-rw-r--r-- | tests/test_cmd2.py | 5 | ||||
-rw-r--r-- | tests/test_transcript.py | 7 |
3 files changed, 30 insertions, 24 deletions
diff --git a/tests/test_argparse.py b/tests/test_argparse.py index cfe0ec5c..0222af30 100644 --- a/tests/test_argparse.py +++ b/tests/test_argparse.py @@ -14,12 +14,13 @@ class ArgparseApp(cmd2.Cmd): self.maxrepeats = 3 cmd2.Cmd.__init__(self) - argparser = argparse.ArgumentParser() - argparser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') - argparser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') - argparser.add_argument('-r', '--repeat', type=int, help='output [n] times') - argparser.add_argument('words', nargs='+', help='words to say') - @cmd2.with_argument_parser(argparser) + say_parser = argparse.ArgumentParser() + say_parser.add_argument('-p', '--piglatin', action='store_true', help='atinLay') + say_parser.add_argument('-s', '--shout', action='store_true', help='N00B EMULATION MODE') + say_parser.add_argument('-r', '--repeat', type=int, help='output [n] times') + say_parser.add_argument('words', nargs='+', help='words to say') + + @cmd2.with_argument_parser(say_parser) def do_say(self, arglist, args=None): """Repeat what you tell me to.""" words = [] @@ -36,17 +37,19 @@ class ArgparseApp(cmd2.Cmd): self.stdout.write(' '.join(words)) self.stdout.write('\n') - argparser = argparse.ArgumentParser(description='create a html tag') - argparser.add_argument('tag', nargs=1, help='tag') - argparser.add_argument('content', nargs='+', help='content to surround with tag') - @cmd2.with_argument_parser(argparser) + tag_parser = argparse.ArgumentParser(description='create a html tag') + tag_parser.add_argument('tag', nargs=1, help='tag') + tag_parser.add_argument('content', nargs='+', help='content to surround with tag') + + @cmd2.with_argument_parser(tag_parser) def do_tag(self, arglist, args=None): self.stdout.write('<{0}>{1}</{0}>'.format(args.tag[0], ' '.join(args.content))) self.stdout.write('\n') - argparser = argparse.ArgumentParser() - argparser.add_argument('args', nargs='*') - @cmd2.with_argument_parser(argparser) + compare_parser = argparse.ArgumentParser() + compare_parser.add_argument('args', nargs='*') + + @cmd2.with_argument_parser(compare_parser) def do_compare(self, arglist, args=None): cmdline_str = re.sub('\s+', ' ', ' '.join(arglist)) args_str = re.sub('\s+', ' ', ' '.join(args.args)) @@ -55,9 +58,10 @@ class ArgparseApp(cmd2.Cmd): else: self.stdout.write('False') - argparser = argparse.ArgumentParser() - argparser.add_argument('args', nargs='*') - @cmd2.with_argument_parser(argparser) + argpasre_arglist_parser = argparse.ArgumentParser() + argpasre_arglist_parser.add_argument('args', nargs='*') + + @cmd2.with_argument_parser(argpasre_arglist_parser) def do_argparse_arglist(self, arglist, args=None): if isinstance(arglist, list): self.stdout.write('True') @@ -65,10 +69,10 @@ class ArgparseApp(cmd2.Cmd): self.stdout.write('False') - argparser = argparse.ArgumentParser() - argparser.add_argument('args', nargs='*') + arglist_and_argparser_parser = argparse.ArgumentParser() + arglist_and_argparser_parser.add_argument('args', nargs='*') @cmd2.with_argument_list - @cmd2.with_argument_parser(argparser) + @cmd2.with_argument_parser(arglist_and_argparser_parser) def do_arglistandargparser(self, arglist, args=None): if isinstance(arglist, list): self.stdout.write(' '.join(arglist)) diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 156b9a73..3fc7c4ea 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -15,6 +15,7 @@ import pytest import six from code import InteractiveConsole +from optparse import make_option # Used for sm.input: raw_input() for Python 2 or input() for Python 3 import six.moves as sm @@ -1273,7 +1274,7 @@ arg 2: 'bar' class OptionApp(cmd2.Cmd): - @cmd2.options([cmd2.make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE")]) + @cmd2.options([make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE")]) def do_greet(self, arg, opts=None): arg = ''.join(arg) if opts.shout: @@ -1316,7 +1317,7 @@ class MultilineApp(cmd2.Cmd): # Need to use this older form of invoking super class constructor to support Python 2.x and Python 3.x cmd2.Cmd.__init__(self, *args, **kwargs) - @cmd2.options([cmd2.make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE")]) + @cmd2.options([make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE")]) def do_orate(self, arg, opts=None): arg = ''.join(arg) if opts.shout: diff --git a/tests/test_transcript.py b/tests/test_transcript.py index e572e919..0daad18a 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -17,9 +17,10 @@ import six # Used for sm.input: raw_input() for Python 2 or input() for Python 3 import six.moves as sm -from cmd2 import (Cmd, make_option, options, Cmd2TestCase, set_use_arg_list, +from cmd2 import (Cmd, options, Cmd2TestCase, set_use_arg_list, set_posix_shlex, set_strip_quotes) from conftest import run_cmd, StdOut, normalize +from optparse import make_option class CmdLineApp(Cmd): @@ -27,7 +28,7 @@ class CmdLineApp(Cmd): MUMBLES = ['like', '...', 'um', 'er', 'hmmm', 'ahh'] MUMBLE_FIRST = ['so', 'like', 'well'] MUMBLE_LAST = ['right?'] - + def __init__(self, *args, **kwargs): self.abbrev = True self.multilineCommands = ['orate'] @@ -332,7 +333,7 @@ def test_transcript(request, capsys, filename, feedback_to_output): ]) def test_parse_transcript_expected(expected, transformed): app = CmdLineApp() - + class TestMyAppCase(Cmd2TestCase): cmdapp = app |