diff options
author | kotfu <kotfu@kotfu.net> | 2022-11-07 15:05:51 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-07 15:05:51 -0700 |
commit | 23a7d22cb3d2261e77bed3480f33f45cafd21cc2 (patch) | |
tree | 78b135bfd3b6c1f5c60e12c2cc2d81e5bbab724f /cmd2/utils.py | |
parent | e95af81acb9c6829c969f76e40658356a4808edb (diff) | |
parent | 860403e4c46e3f8b64efcac2bb08ef2754ebf698 (diff) | |
download | cmd2-git-23a7d22cb3d2261e77bed3480f33f45cafd21cc2.tar.gz |
Merge pull request #1253 from python-cmd2/1252-str-to-bool
Enhance str_to_bool() to accept other types
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 295edb2f..f5b91b99 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -89,11 +89,14 @@ def strip_quotes(arg: str) -> str: return arg -def str_to_bool(val: str) -> bool: - """Converts a string to a boolean based on its value. +def to_bool(val: Any) -> bool: + """Converts anything to a boolean based on its value. - :param val: string being converted - :return: boolean value expressed in the string + Strings like "True", "true", "False", and "false" return True, True, False, and False + respectively. All other values are converted using bool() + + :param val: value being converted + :return: boolean value expressed in the passed in value :raises: ValueError if the string does not contain a value corresponding to a boolean value """ if isinstance(val, str): @@ -101,7 +104,11 @@ def str_to_bool(val: str) -> bool: return True elif val.capitalize() == str(False): return False - raise ValueError("must be True or False (case-insensitive)") + raise ValueError("must be True or False (case-insensitive)") + elif isinstance(val, bool): + return val + else: + return bool(val) class Settable: @@ -126,7 +133,7 @@ class Settable: :param name: name of the instance attribute being made settable :param val_type: callable used to cast the string value from the command line into its proper type and even validate its value. Setting this to bool provides tab completion for true/false and - validation using str_to_bool(). The val_type function should raise an exception if it fails. + validation using to_bool(). The val_type function should raise an exception if it fails. This exception will be caught and printed by Cmd.do_set(). :param description: string describing this setting :param settable_object: object to which the instance attribute belongs (e.g. self) @@ -153,7 +160,7 @@ class Settable: """Used to tab complete lowercase boolean values""" return ['true', 'false'] - val_type = str_to_bool + val_type = to_bool choices_provider = cast(ChoicesProviderFunc, get_bool_choices) self.name = name |