diff options
author | kotfu <kotfu@kotfu.net> | 2022-11-05 20:13:37 -0600 |
---|---|---|
committer | kotfu <kotfu@kotfu.net> | 2022-11-05 20:13:37 -0600 |
commit | 4ea11af39e78caa0a722791e408336395a113497 (patch) | |
tree | 87d8fe481ba13e3f2f76048961e28432dba1b43b | |
parent | e95af81acb9c6829c969f76e40658356a4808edb (diff) | |
download | cmd2-git-4ea11af39e78caa0a722791e408336395a113497.tar.gz |
Enhance str_to_bool() to accept other types
- Rename str_to_bool() -> to_bool()
- Enhance to_bool() so that it accepts and converts bool, int, and float in addition to str
-rw-r--r-- | cmd2/utils.py | 12 | ||||
-rw-r--r-- | docs/api/utils.rst | 2 | ||||
-rw-r--r-- | tests/test_utils.py | 38 |
3 files changed, 32 insertions, 20 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 295edb2f..e193b4db 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -89,7 +89,7 @@ def strip_quotes(arg: str) -> str: return arg -def str_to_bool(val: str) -> bool: +def to_bool(val: str) -> bool: """Converts a string to a boolean based on its value. :param val: string being converted @@ -101,7 +101,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 +130,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 +157,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 diff --git a/docs/api/utils.rst b/docs/api/utils.rst index f9092d8d..0fc11b50 100644 --- a/docs/api/utils.rst +++ b/docs/api/utils.rst @@ -88,7 +88,7 @@ Text Alignment Miscellaneous ------------- -.. autofunction:: cmd2.utils.str_to_bool +.. autofunction:: cmd2.utils.to_bool .. autofunction:: cmd2.utils.categorize diff --git a/tests/test_utils.py b/tests/test_utils.py index 72d9176e..cf2b9810 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -824,28 +824,36 @@ def test_align_right_wide_fill_needs_padding(): 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') - assert cu.str_to_bool('tRuE') +def test_to_bool_str_true(): + assert cu.to_bool('true') + assert cu.to_bool('True') + assert cu.to_bool('TRUE') + assert cu.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') - assert not cu.str_to_bool('fAlSe') +def test_to_bool_str_false(): + assert not cu.to_bool('false') + assert not cu.to_bool('False') + assert not cu.to_bool('FALSE') + assert not cu.to_bool('fAlSe') -def test_str_to_bool_invalid(): +def test_to_bool_str_invalid(): with pytest.raises(ValueError): - cu.str_to_bool('other') + cu.to_bool('other') -def test_str_to_bool_bad_input(): - with pytest.raises(ValueError): - cu.str_to_bool(1) +def test_to_bool_int(): + assert cu.to_bool(1) + assert cu.to_bool(-1) + assert not cu.to_bool(0) + + +def test_to_bool_float(): + assert cu.to_bool(2.35) + assert cu.to_bool(0.25) + assert cu.to_bool(-3.1415) + assert not cu.to_bool(0) def test_find_editor_specified(): |