changeset 495:fb362bcab722

Implement "checkyesno()" and "checkyes()" in the common shell subroutines
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 30 Aug 2024 16:42:04 +0200
parents d10aad19eba3
children 8af33277a2dd
files share/local-bsdtools/common.subr
diffstat 1 files changed, 60 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/common.subr	Fri Aug 30 15:51:26 2024 +0200
+++ b/share/local-bsdtools/common.subr	Fri Aug 30 16:42:04 2024 +0200
@@ -55,6 +55,66 @@
 
 
 #:
+#: Test $1 variable, and warn if not set to YES or NO.
+#:
+#: Args:
+#:   $1 (str): the name of the variable to test to
+#:
+#: Returns:
+#:   0 (truthy) iff ``yes`` (et al), 1 (falsy) otherwise.
+#:
+#: This function warns if other than proper YES/NO values are found.
+#:
+checkyesno() {
+    local _value
+
+    eval _value=\${${1}}
+    case $_value in
+        # "yes", "true", "on", or "1"
+        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
+            return 0
+            ;;
+        # "no", "false", "off", or "0"
+        [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0)
+            return 1
+            ;;
+        *)
+            warn "\${${1}} is not set properly"
+            return 1
+            ;;
+    esac
+}
+
+
+#:
+#: Test $1 variable whether it is set to YES.
+#:
+#: Args:
+#:   $1 (str): the name of the variable to test to
+#:
+#: Returns:
+#:   0 (truthy) iff ``yes`` (et al), 1 (falsy) otherwise.
+#:
+#: Contrary to `checkyesno` this function does not warn if the variable
+#: contains other values as YES.
+#:
+checkyes() {
+    local _value
+
+    eval _value=\${${1}}
+    case $_value in
+        # "yes", "true", "on", or "1"
+        [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
+            return 0
+            ;;
+        *)
+            return 1
+            ;;
+    esac
+}
+
+
+#:
 #: Ensure that no command line options are given
 #:
 #: Args: