changeset 493:fb2c018ef494

Implement fatal() and warn() in the common scripts as helpers for printing warnings and exiting with fatal errors
author Franz Glasner <fzglas.hg@dom66.de>
date Fri, 30 Aug 2024 15:36:41 +0200
parents 312aebce590c
children d10aad19eba3
files share/local-bsdtools/common.subr
diffstat 1 files changed, 43 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/share/local-bsdtools/common.subr	Fri Aug 30 14:17:45 2024 +0200
+++ b/share/local-bsdtools/common.subr	Fri Aug 30 15:36:41 2024 +0200
@@ -12,6 +12,49 @@
 
 
 #:
+#: Display an error message to stderr and exit with given exit code
+#:
+#: Args:
+#:   $1 (int): The exit code to exit with
+#:   $2 ...: The message to print to
+#:
+#: Exit:
+#:   Always exits with $1
+#:
+#: The name of the script is prepended to the error message always.
+#:
+fatal() {
+    local _ec
+
+    if [ $# -ge 1 ]; then
+        _ec=$1
+        shift
+    else
+        _ec=1
+    fi
+    printf "%s: ERROR: %s\\n" "$0" "$*"
+    exit ${_ec}
+}
+
+
+#:
+#: Display a warning to stderr
+#:
+#: Args:
+#:   $*: the fields to display
+#:
+#: Returns:
+#:   0
+#:
+#: The name of the script is prepended to the warning message always.
+#:
+warn() {
+    printf "$0: WARNING: %s\\n" "$0" "$*" 1>&2
+    return 0
+}
+
+
+#:
 #: Ensure that no command line options are given
 #:
 #: Args: