diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-02-04 21:57:05 -0500 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2020-02-04 21:57:05 -0500 |
commit | 457123d3a1376a2ab713f0ff638313b0eacfcf3e (patch) | |
tree | ecffdeb48fdac5fe7a17c38c555ae932c9b89f64 /cmd2 | |
parent | a5d3f7959c252ee23cf6360b81292d376b8c6fcc (diff) | |
download | cmd2-git-457123d3a1376a2ab713f0ff638313b0eacfcf3e.tar.gz |
Updated CHANGELOG and made a few minor tweaks
Diffstat (limited to 'cmd2')
-rw-r--r-- | cmd2/cmd2.py | 10 | ||||
-rw-r--r-- | cmd2/utils.py | 42 |
2 files changed, 8 insertions, 44 deletions
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 |