# HG changeset patch # User Franz Glasner # Date 1725025001 -7200 # Node ID fb2c018ef4949eec468ad99a105aa8dbc3e39164 # Parent 312aebce590ce3ed30540fb09bafd6a8ce83a2fe Implement fatal() and warn() in the common scripts as helpers for printing warnings and exiting with fatal errors diff -r 312aebce590c -r fb2c018ef494 share/local-bsdtools/common.subr --- 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: