summaryrefslogtreecommitdiff
path: root/cmd2/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r--cmd2/utils.py12
1 files changed, 8 insertions, 4 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