# HG changeset patch # User Franz Glasner # Date 1730298594 -3600 # Node ID 77da0074648568ec1e8d00b0a2f9679e86caed53 # Parent 3e775a9390afeed8d1fe5bed625a08098954cf55 common.subr: Implement checkyesnovalue() and checkyesvalue() to check values instead of variables. These are variants of checkyes() and checkyesno(). diff -r 3e775a9390af -r 77da00746485 share/local-bsdtools/common.subr --- 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