Skip to content

Commit

Permalink
[GH-18] Resolves "unbound variable" errors (#32)
Browse files Browse the repository at this point in the history
This PR adds checks to prevent getopts_long errors when `set -u` (shorthand for `set -o nounset`) is enabled. This setting ensures that accessing an unset or undefined variable triggers an error, which helps catch scripting mistakes and avoid unintended behaviour caused by typos or missing variables.


Co-authored-by: Will Allan <billyzkid@yahoo.com>
  • Loading branch information
UrsaDK and billyzkid authored Dec 17, 2024
1 parent 737a918 commit 936fdd5
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 107 deletions.
12 changes: 7 additions & 5 deletions lib/getopts_long.bash
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ getopts_long() {
# Sanitize and normalize short optspec
optspec_short="${optspec_short//-:}"
optspec_short="${optspec_short//-}"
[[ "${!OPTIND:0:2}" == "--" ]] && optspec_short+='-:'
if [[ -n "${!OPTIND:-}" && "${!OPTIND:0:2}" == "--" ]]; then
optspec_short+='-:'
fi


builtin getopts -- "${optspec_short}" "${optvar}" "${@}" || return ${?}
[[ "${!optvar}" == '-' ]] || return 0
Expand All @@ -50,10 +53,9 @@ getopts_long() {

# Missing argument
if [[ -z "${OPTARG}" ]]; then
OPTARG="${!OPTIND}" && OPTIND=$(( OPTIND + 1 ))
[[ -z "${OPTARG}" ]] || return 0

if [[ "${optspec_short:0:1}" == ':' ]]; then
if [[ -n "${!OPTIND:-}" ]]; then
OPTARG="${!OPTIND}" && OPTIND=$(( OPTIND + 1 ))
elif [[ "${optspec_short:0:1}" == ':' ]]; then
OPTARG="${!optvar}" && printf -v "${optvar}" ':'
else
[[ "${OPTERR}" == 0 ]] || \
Expand Down
42 changes: 42 additions & 0 deletions test/bats/github_18.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bats

load ../test_helper
export GETOPTS_LONG_TEST_BIN='getopts_long-nounset'

@test "${FEATURE}: toggles, silent" {
compare '-t -t -- user_arg' \
'-t --toggle -- user_arg'
}
@test "${FEATURE}: toggles, verbose" {
compare '-t -t -- user_arg' \
'-t --toggle -- user_arg'
}

@test "${FEATURE}: options, silent" {
compare '-o user_val1 -o user_val2 -- user_arg' \
'-o user_val1 --option user_val2 -- user_arg'
}
@test "${FEATURE}: options, verbose" {
compare '-o user_val1 -o user_val2 -- user_arg' \
'-o user_val1 --option user_val2 -- user_arg'
}

@test "${FEATURE}: variables, silent" {
compare '-vuser_val1 -vuser_val2 -- user_arg' \
'-vuser_val1 --variable=user_val2 -- user_arg'
}
@test "${FEATURE}: variables, verbose" {
compare '-vuser_val1 -vuser_val2 -- user_arg' \
'-vuser_val1 --variable=user_val2 -- user_arg'
}

@test "${FEATURE}: missing last argument, silent" {
compare '-vuser_val1 -vuser_val2 -- user_arg' \
'-vuser_val1 --variable=user_val2 -- user_arg'
}
@test "${FEATURE}: missing last argument, verbose" {
compare '-o' \
'--option' \
'1{s/getopts.+verbose:/getopts-NORMALIZED:/}' \
'1{s/(option requires an argument --) (o|option)$/\1 NORMALIZED/}'
}
28 changes: 11 additions & 17 deletions test/bin/getopts_long-explicit_args-silent
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,17 @@ variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

enable_extdebug='false'
if shopt -q extdebug; then
enable_extdebug='true'
(
shopt -u extdebug
fi

: "${1:?Missing required argument -- function name}"
function_name=${1}
shift
: "${1:?Missing required argument -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${enable_extdebug}; then
shopt -s extdebug
fi
if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi
)
28 changes: 11 additions & 17 deletions test/bin/getopts_long-explicit_args-verbose
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,17 @@ variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

enable_extdebug='false'
if shopt -q extdebug; then
enable_extdebug='true'
(
shopt -u extdebug
fi

: "${1:?Missing required parameter -- function name}"
function_name=${1}
shift
: "${1:?Missing required parameter -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${enable_extdebug}; then
shopt -s extdebug
fi
if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi
)
44 changes: 44 additions & 0 deletions test/bin/getopts_long-nounset-silent
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env bash

TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
# shellcheck disable=SC1090
source "${TOPDIR}/lib/getopts_long.bash"

(
set -o nounset

while getopts_long ':to:v: toggle option: variable:' OPTKEY; do
case ${OPTKEY} in
't'|'toggle')
printf 'toggle triggered'
;;
'o'|'option')
printf 'option supplied'
;;
'v'|'variable')
printf 'value supplied'
;;
'?')
printf "INVALID OPTION"
;;
':')
printf "MISSING ARGUMENT"
;;
*)
printf "NEVER REACHED"
;;
esac
printf ' -- '
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*'
done

shift $(( OPTIND - 1 ))

echo "OPTERR: ${OPTERR:-}"
echo "OPTKEY: ${OPTKEY:-}"
echo "OPTARG: ${OPTARG:-}"
echo "OPTIND: ${OPTIND:-}"

args=("$@")
declare -p args | sed -e 's/declare -a args=/$@: /'
)
41 changes: 41 additions & 0 deletions test/bin/getopts_long-nounset-verbose
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

TOPDIR="$(cd "$(dirname "${0}")"/../.. && pwd)"
# shellcheck disable=SC1090
source "${TOPDIR}/lib/getopts_long.bash"

(
set -o nounset

while getopts_long 'to:v: toggle option: variable:' OPTKEY; do
case ${OPTKEY} in
't'|'toggle')
printf 'toggle triggered'
;;
'o'|'option')
printf 'option supplied'
;;
'v'|'variable')
printf 'value supplied'
;;
'?')
printf "INVALID OPTION or MISSING ARGUMENT"
;;
*)
printf "NEVER REACHED"
;;
esac
printf ' -- '
declare -p OPTARG 2>&1 | grep -oe 'OPTARG.*'
done

shift $(( OPTIND - 1 ))

echo "OPTERR: ${OPTERR:-}"
echo "OPTKEY: ${OPTKEY:-}"
echo "OPTARG: ${OPTARG:-}"
echo "OPTIND: ${OPTIND:-}"

args=("$@")
declare -p args | sed -e 's/declare -a args=/$@: /'
)
28 changes: 11 additions & 17 deletions test/bin/getopts_long-with_extdebug-silent
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,17 @@ variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

disable_extdebug='false'
if ! shopt -q extdebug; then
disable_extdebug='true'
(
shopt -s extdebug
fi

: "${1:?Missing required argument -- function name}"
function_name=${1}
shift
: "${1:?Missing required argument -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${disable_extdebug}; then
shopt -u extdebug
fi
if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi
)
28 changes: 11 additions & 17 deletions test/bin/getopts_long-with_extdebug-verbose
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,17 @@ variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

disable_extdebug='false'
if ! shopt -q extdebug; then
disable_extdebug='true'
(
shopt -s extdebug
fi

: "${1:?Missing required parameter -- function name}"
function_name=${1}
shift
: "${1:?Missing required parameter -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${disable_extdebug}; then
shopt -u extdebug
fi
if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi
)
28 changes: 11 additions & 17 deletions test/bin/getopts_long-without_extdebug-silent
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,17 @@ variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

enable_extdebug='false'
if shopt -q extdebug; then
enable_extdebug='true'
(
shopt -u extdebug
fi

: "${1:?Missing required argument -- function name}"
function_name=${1}
shift
: "${1:?Missing required argument -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${enable_extdebug}; then
shopt -s extdebug
fi
if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi
)
28 changes: 11 additions & 17 deletions test/bin/getopts_long-without_extdebug-verbose
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,17 @@ variables() {
getopts_function -vuser_val1 --variable=user_val2 -- user_arg
}

enable_extdebug='false'
if shopt -q extdebug; then
enable_extdebug='true'
(
shopt -u extdebug
fi

: "${1:?Missing required parameter -- function name}"
function_name=${1}
shift
: "${1:?Missing required parameter -- function name}"
function_name=${1}
shift

if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi

if ${enable_extdebug}; then
shopt -s extdebug
fi
if declare -f "$function_name" > /dev/null; then
${function_name} "$@"
else
echo "Function not found -- ${function_name}"
exit 1
fi
)

0 comments on commit 936fdd5

Please sign in to comment.