Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 1 | # -*- sh -*- |
| 2 | ## ----------------------------------------------------------------------- |
| 3 | ## Intent: |
| 4 | ## o This is a helper module for common/common.sh to support passing |
| 5 | ## switches into the library as part of the source command. |
| 6 | ## o A delimiter must be inserted into @ARGV to prevent consumption |
| 7 | ## of command line arguments passed into the parent script. |
| 8 | ## ----------------------------------------------------------------------- |
| 9 | ## Usage: source preserve_argv.sh |
| 10 | ## ----------------------------------------------------------------------- |
| 11 | |
| 12 | # __DEBUG_COMMON__=1 |
| 13 | [[ -v __DEBUG_COMMON__ ]] && echo " ** ${BASH_SOURCE[0]}: BEGIN" |
| 14 | |
| 15 | ## ----------------------------------------------------------------------- |
| 16 | ## Intent: Preserve command line args allowing common.sh sourcing. |
| 17 | ## ----------------------------------------------------------------------- |
| 18 | ## Required: Caller is required to inline and shift the @ARGV {cab} marker |
| 19 | ## ----------------------------------------------------------------------- |
| 20 | ## Usage: source preserve_argv.sh |
| 21 | ## source "$(OPT_ROOT}"/common/common.sh --common-args-begin-- --stacktrace |
| 22 | ## ----------------------------------------------------------------------- |
| 23 | function __anon_func_preserve_argv__() |
| 24 | { |
| 25 | local key='__preserve_argv_stack__' |
| 26 | local cab='--common-args-begin--' |
| 27 | local iam="${BASH_SOURCE[0]%/*}" |
| 28 | |
| 29 | if [[ -v __preserve_argv_stack__ ]]; then |
| 30 | # echo " ** ${iam}: [POPD] environment: ${__preserve_argv_stack__[@]}" |
| 31 | set -- "${__preserve_argv_stack__[@]}" |
| 32 | unset __preserve_argv_stack__ |
| 33 | elif [ $# -eq 0 ] || [ "$1" != "$cab" ]; then |
| 34 | echo " ** ${iam} ERROR: ARGV marker not found: ${cab}" |
| 35 | echo " ** command: $0" |
| 36 | exit 1 |
| 37 | else |
| 38 | ## caller (common.sh) shift {cab} from argv |
| 39 | declare -g -a __preserve_argv_stack__=("$@") |
| 40 | # echo " ** ${iam}: [PUSHD] environment: ${__preserve_argv_stack__[@]}" |
| 41 | fi |
| 42 | |
| 43 | return |
| 44 | } |
| 45 | |
| 46 | ##----------------## |
| 47 | ##---] MAIN [---## |
| 48 | ##----------------## |
| 49 | __anon_func_preserve_argv__ "$@" |
| 50 | unset __anon_func_preserve_argv__ |
| 51 | |
| 52 | [[ -v __DEBUG_COMMON__ ]] && echo " ** ${BASH_SOURCE[0]}: CLOSE" |
| 53 | |
| 54 | : # NOP for set -e -vs- [[ -v __DEBUG_COMMON__ ]] |
| 55 | |
| 56 | # [EOF] |