Skip to content

Commit 4ea11af

Browse files
committed
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
1 parent e95af81 commit 4ea11af

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

cmd2/utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def strip_quotes(arg: str) -> str:
8989
return arg
9090

9191

92-
def str_to_bool(val: str) -> bool:
92+
def to_bool(val: str) -> bool:
9393
"""Converts a string to a boolean based on its value.
9494
9595
:param val: string being converted
@@ -101,7 +101,11 @@ def str_to_bool(val: str) -> bool:
101101
return True
102102
elif val.capitalize() == str(False):
103103
return False
104-
raise ValueError("must be True or False (case-insensitive)")
104+
raise ValueError("must be True or False (case-insensitive)")
105+
elif isinstance(val, bool):
106+
return val
107+
else:
108+
return bool(val)
105109

106110

107111
class Settable:
@@ -126,7 +130,7 @@ def __init__(
126130
:param name: name of the instance attribute being made settable
127131
:param val_type: callable used to cast the string value from the command line into its proper type and
128132
even validate its value. Setting this to bool provides tab completion for true/false and
129-
validation using str_to_bool(). The val_type function should raise an exception if it fails.
133+
validation using to_bool(). The val_type function should raise an exception if it fails.
130134
This exception will be caught and printed by Cmd.do_set().
131135
:param description: string describing this setting
132136
:param settable_object: object to which the instance attribute belongs (e.g. self)
@@ -153,7 +157,7 @@ def get_bool_choices(_) -> List[str]: # type: ignore[no-untyped-def]
153157
"""Used to tab complete lowercase boolean values"""
154158
return ['true', 'false']
155159

156-
val_type = str_to_bool
160+
val_type = to_bool
157161
choices_provider = cast(ChoicesProviderFunc, get_bool_choices)
158162

159163
self.name = name

docs/api/utils.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Text Alignment
8888
Miscellaneous
8989
-------------
9090

91-
.. autofunction:: cmd2.utils.str_to_bool
91+
.. autofunction:: cmd2.utils.to_bool
9292

9393
.. autofunction:: cmd2.utils.categorize
9494

tests/test_utils.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -824,28 +824,36 @@ def test_align_right_wide_fill_needs_padding():
824824
assert aligned == fill_char + ' ' + text
825825

826826

827-
def test_str_to_bool_true():
828-
assert cu.str_to_bool('true')
829-
assert cu.str_to_bool('True')
830-
assert cu.str_to_bool('TRUE')
831-
assert cu.str_to_bool('tRuE')
827+
def test_to_bool_str_true():
828+
assert cu.to_bool('true')
829+
assert cu.to_bool('True')
830+
assert cu.to_bool('TRUE')
831+
assert cu.to_bool('tRuE')
832832

833833

834-
def test_str_to_bool_false():
835-
assert not cu.str_to_bool('false')
836-
assert not cu.str_to_bool('False')
837-
assert not cu.str_to_bool('FALSE')
838-
assert not cu.str_to_bool('fAlSe')
834+
def test_to_bool_str_false():
835+
assert not cu.to_bool('false')
836+
assert not cu.to_bool('False')
837+
assert not cu.to_bool('FALSE')
838+
assert not cu.to_bool('fAlSe')
839839

840840

841-
def test_str_to_bool_invalid():
841+
def test_to_bool_str_invalid():
842842
with pytest.raises(ValueError):
843-
cu.str_to_bool('other')
843+
cu.to_bool('other')
844844

845845

846-
def test_str_to_bool_bad_input():
847-
with pytest.raises(ValueError):
848-
cu.str_to_bool(1)
846+
def test_to_bool_int():
847+
assert cu.to_bool(1)
848+
assert cu.to_bool(-1)
849+
assert not cu.to_bool(0)
850+
851+
852+
def test_to_bool_float():
853+
assert cu.to_bool(2.35)
854+
assert cu.to_bool(0.25)
855+
assert cu.to_bool(-3.1415)
856+
assert not cu.to_bool(0)
849857

850858

851859
def test_find_editor_specified():

0 commit comments

Comments
 (0)