diff options
-rwxr-xr-x | cmd2.py | 30 | ||||
-rw-r--r-- | tests/conftest.py | 2 | ||||
-rw-r--r-- | tests/test_cmd2.py | 28 | ||||
-rw-r--r-- | tests/test_transcript.py | 4 | ||||
-rw-r--r-- | tests/transcripts/from_cmdloop.txt | 4 |
5 files changed, 35 insertions, 33 deletions
@@ -258,6 +258,7 @@ def parse_quoted_string(cmdline): lexed_arglist = temp_arglist return lexed_arglist + def with_argument_list(func): """A decorator to alter the arguments passed to a do_* cmd2 method. Default passes a string of whatever the user typed. @@ -1315,15 +1316,10 @@ class Cmd(cmd.Cmd): len(fulloptions))) return result - show_parser = argparse.ArgumentParser(description='show value of a parameter') - show_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter') - show_parser.add_argument('param', nargs='?', help='name of parameter, if not supplied show all parameters') - - @with_argument_parser(show_parser) - def do_show(self, args): + def show(self, args, parameter): param = '' - if args.param: - param = args.param.strip().lower() + if parameter: + param = parameter.strip().lower() result = {} maxlen = 0 for p in self.settable: @@ -1339,14 +1335,19 @@ class Cmd(cmd.Cmd): else: raise LookupError("Parameter '%s' not supported (type 'show' for list of parameters)." % param) - def do_set(self, arg): - """Sets a settable parameter. + set_parser = argparse.ArgumentParser(description='show or set value of a parameter') + set_parser.add_argument('-l', '--long', action='store_true', help='describe function of parameter') + set_parser.add_argument('settable', nargs='*', help='[param_name] [value]') + + @with_argument_parser(set_parser) + def do_set(self, args): + """Sets a settable parameter or shows current settings of parameters. Accepts abbreviated parameter names so long as there is no ambiguity. Call without arguments for a list of settable parameters with their values. """ try: - statement, param_name, val = arg.parsed.raw.split(None, 2) + param_name, val = args.settable val = val.strip() param_name = param_name.strip().lower() if param_name not in self.settable: @@ -1354,7 +1355,7 @@ class Cmd(cmd.Cmd): if len(hits) == 1: param_name = hits[0] else: - return self.do_show(param_name) + return self.show(args, param_name) current_val = getattr(self, param_name) if (val[0] == val[-1]) and val[0] in ("'", '"'): val = val[1:-1] @@ -1369,7 +1370,10 @@ class Cmd(cmd.Cmd): except AttributeError: pass except (ValueError, AttributeError): - self.do_show(arg) + param = '' + if args.settable: + param = args.settable[0] + self.show(args, param) def do_shell(self, command): """Execute a command as if at the OS prompt. diff --git a/tests/conftest.py b/tests/conftest.py index 05721eb4..c9a9ab0e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -16,7 +16,7 @@ import cmd2 BASE_HELP = """Documented commands (type help <topic>): ======================================== cmdenvironment help load pyscript run set shortcuts -edit history py quit save shell show +edit history py quit save shell """ # Help text for the history command diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index f48c2a79..3295bb60 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -44,19 +44,17 @@ def test_base_help_history(base_app): expected = normalize(HELP_HISTORY) assert out == expected -def test_base_options_help(base_app, capsys): - run_cmd(base_app, 'show -h') +def test_base_argparse_help(base_app, capsys): + run_cmd(base_app, 'set -h') out, err = capsys.readouterr() - expected = run_cmd(base_app, 'help show') - # 'show -h' is the same as 'help show', other than whitespace differences of an extra newline present in 'help show' - assert normalize(str(out)) == expected + expected = run_cmd(base_app, 'help set') + assert normalize(base_app.do_set.__doc__ + str(err)) == expected def test_base_invalid_option(base_app, capsys): - run_cmd(base_app, 'show -z') + run_cmd(base_app, 'set -z') out, err = capsys.readouterr() - show_help = run_cmd(base_app, 'help show') - expected = ['usage: show [-h] [-l] [param]', 'show: error: unrecognized arguments: -z'] - # 'show -h' is the same as 'help show', other than whitespace differences of an extra newline present in 'help show' + run_cmd(base_app, 'help set') + expected = ['usage: set [-h] [-l] [settable [settable ...]]', 'set: error: unrecognized arguments: -z'] assert normalize(str(err)) == expected def test_base_shortcuts(base_app): @@ -68,7 +66,7 @@ def test_base_shortcuts(base_app): def test_base_show(base_app): # force editor to be 'vim' so test is repeatable across platforms base_app.editor = 'vim' - out = run_cmd(base_app, 'show') + out = run_cmd(base_app, 'set') expected = normalize(SHOW_TXT) assert out == expected @@ -76,7 +74,7 @@ def test_base_show(base_app): def test_base_show_long(base_app): # force editor to be 'vim' so test is repeatable across platforms base_app.editor = 'vim' - out = run_cmd(base_app, 'show -l') + out = run_cmd(base_app, 'set -l') expected = normalize(SHOW_LONG) assert out == expected @@ -89,7 +87,7 @@ now: True """) assert out == expected - out = run_cmd(base_app, 'show quiet') + out = run_cmd(base_app, 'set quiet') assert out == ['quiet: True'] def test_set_not_supported(base_app, capsys): @@ -109,7 +107,7 @@ now: True """) assert out == expected - out = run_cmd(base_app, 'show quiet') + out = run_cmd(base_app, 'set quiet') assert out == ['quiet: True'] @@ -1099,8 +1097,8 @@ def test_custom_help_menu(help_app): expected = normalize(""" Documented commands (type help <topic>): ======================================== -cmdenvironment help load pyscript run set shortcuts squat -edit history py quit save shell show +cmdenvironment help load pyscript run set shortcuts +edit history py quit save shell squat Undocumented commands: ====================== diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 151cf5e6..08cde665 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -130,8 +130,8 @@ def test_base_with_transcript(_cmdline_app): Documented commands (type help <topic>): ======================================== -cmdenvironment help load orate pyscript run say shell show -edit history mumble py quit save set shortcuts speak +cmdenvironment help load orate pyscript run say shell speak +edit history mumble py quit save set shortcuts (Cmd) help say Repeats what you tell me to. diff --git a/tests/transcripts/from_cmdloop.txt b/tests/transcripts/from_cmdloop.txt index 09072440..7a926d2c 100644 --- a/tests/transcripts/from_cmdloop.txt +++ b/tests/transcripts/from_cmdloop.txt @@ -5,8 +5,8 @@ Documented commands (type help <topic>): ======================================== -cmdenvironment help load orate pyscript run say shell show/ */ -edit history mumble py quit save set shortcuts speak/ */ +cmdenvironment help load orate pyscript run say shell speak/ */ +edit history mumble py quit save set shortcuts/ */ (Cmd) help say Repeats what you tell me to. |