changeset 795:77da00746485

common.subr: Implement checkyesnovalue() and checkyesvalue() to check values instead of variables. These are variants of checkyes() and checkyesno().
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 30 Oct 2024 15:29:54 +0100
parents 3e775a9390af
children e48d173534ec
files share/local-bsdtools/common.subr
diffstat 1 files changed, 56 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/common.subr	Wed Oct 30 14:17:15 2024 +0100
+++ b/share/local-bsdtools/common.subr	Wed Oct 30 15:29:54 2024 +0100
@@ -115,7 +115,8 @@
 #:   int: 0 (truthy) iff ``yes`` (et al),
 #:        1 (falsy) otherwise.
 #:
-#: This function warns if other than proper YES/NO values are found.
+#: This function warns if other than proper YES/NO or their alias
+#: values are found.
 #:
 checkyesno() {
     # $1
@@ -123,6 +124,7 @@
     local _value
 
     eval _value=\"\$\{"${1}"\}\"
+    # do not delegate to checkyesnovalue() because of the error message proper
     case "${_value}" in
         # "yes", "true", "on", or "1"
         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
@@ -141,6 +143,38 @@
 
 
 #:
+#: Test $1 variable, and warn if not set to YES or NO.
+#:
+#: Args:
+#:   $1 (str): The value to test to.
+#:
+#: Returns:
+#:   int: 0 (truthy) iff ``yes`` (et al),
+#:        1 (falsy) otherwise.
+#:
+#: This function warns if other than proper YES/NO values are found.
+#:
+checkyesnovalue() {
+    # $1
+
+    case "${1}" 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 "expected YES/NO but got \`${1}'"
+            return 1
+            ;;
+    esac
+}
+
+
+#:
 #: Test $1 variable whether it is set to YES.
 #:
 #: Args:
@@ -159,7 +193,27 @@
     local _value
 
     eval _value=\"\$\{"${1}"\}\"
-    case "${_value}" in
+    checkyesvalue "${_value}"
+}
+
+
+#:
+#: Test a given value whether it is set to YES.
+#:
+#: Args:
+#:   $1 (str): The value to test to.
+#:
+#: Returns:
+#:   int: 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.
+#:
+checkyesvalue() {
+    # $1
+
+    case "${1}" in
         # "yes", "true", "on", or "1"
         [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
             return 0