Skip to content

Commit

Permalink
Prevent error if !OPTIND is unbound (#18)
Browse files Browse the repository at this point in the history
This PR adds a check that prevents an error when nounset is enabled (`set -u`) and `!OPTIND` is unbound, i.e. when there are no more arguments left to process and `getopts_long` encounters a final option that is missing a required argument. This matches the behavior of the builtin `getopts` command.
  • Loading branch information
UrsaDK authored Dec 17, 2024
2 parents 833b9f2 + ea81a25 commit cbe9795
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/getopts_long.bash
Original file line number Diff line number Diff line change
Expand Up @@ -53,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

0 comments on commit cbe9795

Please sign in to comment.