diff options
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | cmd2/cmd2.py | 10 | ||||
-rw-r--r-- | cmd2/utils.py | 42 | ||||
-rwxr-xr-x | tests/test_cmd2.py | 39 |
4 files changed, 12 insertions, 83 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 342aeaa0..d9c1e774 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ * `continuation_prompt` * `self_in_py` * `prompt` + * `self.settable` changed to `self.settables` + * It is now a Dict[str, Settable] instead of Dict[str, str] + * **set** command now supports tab-completion of values + * Removed `cast()` utility function ## 0.9.25 (January 26, 2020) * Enhancements diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py index 52971ddd..ccd96991 100644 --- a/cmd2/cmd2.py +++ b/cmd2/cmd2.py @@ -415,13 +415,9 @@ class Cmd(cmd.Cmd): @allow_style.setter def allow_style(self, new_val: str) -> None: """Setter property needed to support do_set when it updates allow_style""" - new_val = new_val.lower() - if new_val == ansi.STYLE_TERMINAL.lower(): - ansi.allow_style = ansi.STYLE_TERMINAL - elif new_val == ansi.STYLE_ALWAYS.lower(): - ansi.allow_style = ansi.STYLE_ALWAYS - elif new_val == ansi.STYLE_NEVER.lower(): - ansi.allow_style = ansi.STYLE_NEVER + new_val = new_val.capitalize() + if new_val in [ansi.STYLE_TERMINAL, ansi.STYLE_ALWAYS, ansi.STYLE_NEVER]: + ansi.allow_style = new_val else: raise ValueError('Invalid value: {} (valid values: {}, {}, {})'.format(new_val, ansi.STYLE_TERMINAL, ansi.STYLE_ALWAYS, diff --git a/cmd2/utils.py b/cmd2/utils.py index a23826c3..2f2efefa 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -58,17 +58,17 @@ def strip_quotes(arg: str) -> str: def str_to_bool(val: str) -> bool: - """ - Converts a string to a boolean based on its value + """Converts a string to a boolean based on its value. + :param val: string being converted :return: boolean value expressed in the string :raises: ValueError if the string does not contain a value corresponding to a boolean value """ - if val.lower() == "true": + if val.capitalize() == str(True): return True - elif val.lower() == "false": + elif val.capitalize() == str(False): return False - raise ValueError("must be true or false") + raise ValueError("must be True or False") class Settable: @@ -143,38 +143,6 @@ def namedtuple_with_defaults(typename: str, field_names: Union[str, List[str]], return T -def cast(current: Any, new: str) -> Any: - """Tries to force a new value into the same type as the current when trying to set the value for a parameter. - - :param current: current value for the parameter, type varies - :param new: new value - :return: new value with same type as current, or the current value if there was an error casting - """ - typ = type(current) - orig_new = new - - if typ == bool: - try: - return bool(int(new)) - except (ValueError, TypeError): - pass - try: - new = new.lower() - if (new == 'on') or (new[0] in ('y', 't')): - return True - if (new == 'off') or (new[0] in ('n', 'f')): - return False - except AttributeError: - pass - else: - try: - return typ(new) - except (ValueError, TypeError): - pass - print("Problem setting parameter (now {}) to {}; incorrect type?".format(current, orig_new)) - return current - - def which(exe_name: str) -> Optional[str]: """Find the full path of a given executable on a Linux or Mac machine diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 8b14949c..671a6685 100755 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -108,45 +108,6 @@ def test_base_show_long(base_app): assert out == expected -def test_cast(): - # Boolean - assert utils.cast(True, True) == True - assert utils.cast(True, False) == False - assert utils.cast(True, 0) == False - assert utils.cast(True, 1) == True - assert utils.cast(True, 'on') == True - assert utils.cast(True, 'off') == False - assert utils.cast(True, 'ON') == True - assert utils.cast(True, 'OFF') == False - assert utils.cast(True, 'y') == True - assert utils.cast(True, 'n') == False - assert utils.cast(True, 't') == True - assert utils.cast(True, 'f') == False - - # Non-boolean same type - assert utils.cast(1, 5) == 5 - assert utils.cast(3.4, 2.7) == 2.7 - assert utils.cast('foo', 'bar') == 'bar' - assert utils.cast([1,2], [3,4]) == [3,4] - -def test_cast_problems(capsys): - expected = 'Problem setting parameter (now {}) to {}; incorrect type?\n' - - # Boolean current, with new value not convertible to bool - current = True - new = [True, True] - assert utils.cast(current, new) == current - out, err = capsys.readouterr() - assert out == expected.format(current, new) - - # Non-boolean current, with new value not convertible to current type - current = 1 - new = 'octopus' - assert utils.cast(current, new) == current - out, err = capsys.readouterr() - assert out == expected.format(current, new) - - def test_base_set(base_app): out, err = run_cmd(base_app, 'set quiet True') expected = normalize(""" |