#@IgnoreInspection BashAddShebang # Boolean functionality (Koalephant Shell Script Library) # Version: 2.0.0 # Copyright: 2014-2017, Koalephant Co., Ltd # Author: Stephen Reay , Koalephant Packaging Team # Parse a string into 'true' or 'false' # 'true' values are 'on', 'true', 'yes', 'y' and '1', regardless of case # 'false' values are 'off', 'false' 'no', 'n' and '0', regardless of case # # Input: # $1 - the string to parse # $2 - the default value to return if neither true or false is matched # # Output: # either 'true' or 'false', or the default value if specified proc k_bool_parse { var value = '' var default = $(2-) if set value = $[k_bool_parse_error $1] { echo $(value) } else { echo $[k_bool_parse $(default) false] } } # Parse a string into 'true' or 'false' or error # 'true' values are 'on', 'true', 'yes', 'y' and '1', regardless of case # 'false' values are 'off', 'false' 'no', 'n' and '0', regardless of case # # Input: # $1 - the string to parse # # Output: # either 'true' or 'false' # # Return: # 0 if a valid bool value is given, 1 if not proc k_bool_parse_error { var value = $1 match $[k_string_lower $(value)] { with on|true|yes|y|1 echo true with off|false|no|n|0 echo false with * return 1 } } # Test a variable for truthiness. # Uses (#k_bool_parse) to parse input # # Input: # $1 - the variable to test # $2 - the default value to test if neither true or false is matched # # Return: # 0 if true, 1 if not # # Example: # if k_string_test_bool "${optionValue}"; then # do_something # fi proc k_bool_test { test $[k_bool_parse @Argv] = true } # Test a variable for booelan-ness # Uses (#k_bool_parse) to parse input # # Input: # $1 - the variable to test # # Return: # 0 if a valid boolean value, 1 if not proc k_bool_valid { k_bool_parse_error $1 > /dev/null }