success_with_args

This commit is contained in:
pvincent
2026-07-31 18:20:12 +04:00
parent 05fb3ac0db
commit 2e595ef217
3 changed files with 59 additions and 18 deletions
+30 -15
View File
@@ -581,33 +581,48 @@ function cleanup {
rm -f "$MIAOU_BASH_ERROR"
}
function usage {
echo "$(basename "$0") <SCRIPT> [...SCRIPT_ARGS] | --version | --help"
}
function parse_options {
[[ $# == 0 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 2
[[ $# == 1 ]] && case "$1" in
--help) usage && builtin exit ;;
--version) cat "$MIAOU_BASH_DIR/.semver_git_tag" && builtin exit ;;
-*) >&2 echo "ERROR: option '$1' unknown!" && usage && builtin exit 2 ;;
esac
[[ $1 =~ ^- ]] && >&2 echo "ERROR: too many args $#, either single option or SCRIPT + args accepted!" && builtin exit 2
SCRIPT="$1"
[[ ! -v SCRIPT ]] && >&2 echo 'ERROR: script expected!' && builtin exit 2
[[ ! -f "$SCRIPT" ]] && >&2 echo "ERROR: script '$SCRIPT' not found!" && builtin exit 2
true
}
## main
[[ $# -lt 1 ]] && >&2 echo 'ERROR: script expected!' && builtin exit 1
set -euo pipefail
set -E # HALT on subshell error
set -T # DEBUG RETURN
set -E # HALT on pipe error
set -T # RETURN
parse_options "$@"
build_miaou_bash_error
BASH_ARGV0="$1" && shift # to pretend script runs by itself
trap 'on_error $?' ERR
trap 'on_exit $?' EXIT
trap 'on_pipe' PIPE
trap 'on_return' RETURN
if ! is_true "$CHILD_PROCESS"; then
# main
[[ -e /proc/$$/fd/3 ]] && exec 4>&3 3>&- || exec 4>/dev/tty # swap fd 3 4 when fd 3 sent off
exec 3>/dev/stderr 2>"$MIAOU_BASH_ERROR"
else
# any children
exec 4>/dev/tty 2>"$MIAOU_BASH_ERROR"
fi
BASH_ARGV0="$SCRIPT" && shift
if is_true "$CHILD_PROCESS"; then
# any nested children
exec 4>/dev/tty 2>"$MIAOU_BASH_ERROR"
miaou_debug "<child file=$BASH_ARGV0 pid=${BASHPID} parent=${PPID}>"
else
# the MAIN Process
[[ -e /proc/$$/fd/3 ]] && exec 4>&3 3>&- || exec 4>/dev/tty # swap fd 3 4 when fd 3 sent off
exec 3>/dev/stderr 2>"$MIAOU_BASH_ERROR"
miaou_debug "<main file=$BASH_ARGV0 pid=${BASHPID}/>"
fi
# shellcheck source=/dev/null
source "$BASH_ARGV0"
source "$SCRIPT"