summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py3
-rw-r--r--cmd2/utils.py9
-rwxr-xr-xtests/test_cmd2.py7
-rw-r--r--tests/test_utils.py19
4 files changed, 33 insertions, 5 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index ccd96991..563e328d 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -2845,7 +2845,8 @@ class Cmd(cmd.Cmd):
def do_set(self, args: argparse.Namespace) -> None:
"""Set a settable parameter or show current settings of parameters"""
if not self.settables:
- self.poutput("There are no settable parameters")
+ self.pwarning("There are no settable parameters")
+ return
if args.param:
try:
diff --git a/cmd2/utils.py b/cmd2/utils.py
index 2f2efefa..c200085a 100644
--- a/cmd2/utils.py
+++ b/cmd2/utils.py
@@ -64,10 +64,11 @@ def str_to_bool(val: str) -> bool:
:return: boolean value expressed in the string
:raises: ValueError if the string does not contain a value corresponding to a boolean value
"""
- if val.capitalize() == str(True):
- return True
- elif val.capitalize() == str(False):
- return False
+ if isinstance(val, str):
+ if val.capitalize() == str(True):
+ return True
+ elif val.capitalize() == str(False):
+ return False
raise ValueError("must be True or False")
diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py
index 671a6685..9aaebc99 100755
--- a/tests/test_cmd2.py
+++ b/tests/test_cmd2.py
@@ -127,6 +127,13 @@ Parameter 'qqq' not supported (type 'set' for list of parameters).
assert err == expected
+def test_set_no_settables(base_app):
+ base_app.settables = {}
+ out, err = run_cmd(base_app, 'set quiet True')
+ expected = normalize("There are no settable parameters")
+ assert err == expected
+
+
@pytest.mark.parametrize('new_val, is_valid, expected', [
(ansi.STYLE_NEVER, False, ansi.STYLE_NEVER),
('neVeR', False, ansi.STYLE_NEVER),
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 9dd54ee2..804e58be 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -531,3 +531,22 @@ def test_align_right_wide_fill_needs_padding():
width = 6
aligned = cu.align_right(text, fill_char=fill_char, width=width)
assert aligned == fill_char + ' ' + text
+
+
+def test_str_to_bool_true():
+ assert cu.str_to_bool('true')
+ assert cu.str_to_bool('True')
+ assert cu.str_to_bool('TRUE')
+
+def test_str_to_bool_false():
+ assert not cu.str_to_bool('false')
+ assert not cu.str_to_bool('False')
+ assert not cu.str_to_bool('FALSE')
+
+def test_str_to_bool_invalid():
+ with pytest.raises(ValueError):
+ cu.str_to_bool('other')
+
+def test_str_to_bool_bad_input():
+ with pytest.raises(ValueError):
+ cu.str_to_bool(1)