summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkotfu <kotfu@kotfu.net>2022-11-07 15:05:51 -0700
committerGitHub <noreply@github.com>2022-11-07 15:05:51 -0700
commit23a7d22cb3d2261e77bed3480f33f45cafd21cc2 (patch)
tree78b135bfd3b6c1f5c60e12c2cc2d81e5bbab724f
parente95af81acb9c6829c969f76e40658356a4808edb (diff)
parent860403e4c46e3f8b64efcac2bb08ef2754ebf698 (diff)
downloadcmd2-git-23a7d22cb3d2261e77bed3480f33f45cafd21cc2.tar.gz
Merge pull request #1253 from python-cmd2/1252-str-to-bool
Enhance str_to_bool() to accept other types
-rw-r--r--cmd2/utils.py21
-rw-r--r--docs/api/utils.rst2
-rw-r--r--tests/test_utils.py43
3 files changed, 43 insertions, 23 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
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..cfdf07b0 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -824,28 +824,41 @@ 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_bool():
+ assert cu.to_bool(True)
+ assert not cu.to_bool(False)
+
+
+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():