summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2020-02-05 09:35:44 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2020-02-05 09:35:44 -0500
commitce7cbe22916eef1693c0a9d0b0c43297d53f9d3b (patch)
tree3a26bfb92618782785f5f5ed5a4b81f67e4915de
parent80adeb66420d45b7f22320a5de3e32d1b57b4e1b (diff)
downloadcmd2-git-ce7cbe22916eef1693c0a9d0b0c43297d53f9d3b.tar.gz
Added support to do_set() for setting a parameter to an empty string as well as something resembling an argparse flag (e.g. -h)
-rw-r--r--cmd2/cmd2.py10
-rwxr-xr-xtests/test_cmd2.py12
2 files changed, 18 insertions, 4 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index d6870d06..37779f56 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2827,8 +2827,9 @@ class Cmd(cmd.Cmd):
from .argparse_completer import AutoCompleter
completer = AutoCompleter(settable_parser, self)
- tokens, _ = self.tokens_for_completion(line, begidx, endidx)
- return completer.complete_command(tokens, text, line, begidx, endidx)
+ # Use raw_tokens since quotes have been preserved
+ _, raw_tokens = self.tokens_for_completion(line, begidx, endidx)
+ return completer.complete_command(raw_tokens, text, line, begidx, endidx)
# When tab completing value, we recreate the set command parser with a value argument specific to
# the settable being edited. To make this easier, define a parent parser with all the common elements.
@@ -2849,7 +2850,8 @@ class Cmd(cmd.Cmd):
set_parser.add_argument('value', nargs=argparse.OPTIONAL, help='new value for settable',
completer_method=complete_set_value, suppress_tab_hint=True)
- @with_argparser(set_parser)
+ # Preserve quotes so users can pass in quoted empty strings and flags (e.g. -h) as the value
+ @with_argparser(set_parser, preserve_quotes=True)
def do_set(self, args: argparse.Namespace) -> None:
"""Set a settable parameter or show current settings of parameters"""
if not self.settables:
@@ -2864,6 +2866,8 @@ class Cmd(cmd.Cmd):
return
if args.value:
+ args.value = utils.strip_quotes(args.value)
+
# Try to update the settable's value
try:
orig_value = getattr(self, args.param)
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 9aaebc99..0615ed46 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -108,7 +108,7 @@ def test_base_show_long(base_app):
assert out == expected
-def test_base_set(base_app):
+def test_set(base_app):
out, err = run_cmd(base_app, 'set quiet True')
expected = normalize("""
quiet - was: False
@@ -119,6 +119,16 @@ now: True
out, err = run_cmd(base_app, 'set quiet')
assert out == ['quiet: True']
+def test_set_val_empty(base_app):
+ base_app.editor = "fake"
+ out, err = run_cmd(base_app, 'set editor ""')
+ assert base_app.editor == ''
+
+def test_set_val_is_flag(base_app):
+ base_app.editor = "fake"
+ out, err = run_cmd(base_app, 'set editor "-h"')
+ assert base_app.editor == '-h'
+
def test_set_not_supported(base_app):
out, err = run_cmd(base_app, 'set qqq True')
expected = normalize("""