Skip to content

Commit

Permalink
Fix bug where testdepth would not be decreased on failed match
Browse files Browse the repository at this point in the history
  • Loading branch information
andsens committed May 11, 2024
1 parent 248ff39 commit bd2958f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
10 changes: 5 additions & 5 deletions docopt_sh/docopt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ sequence() {

choice() {
local initial_params=("${params[@]}") best_match_idx match_count node_idx
# Increase testdepth, so that we can test all subtrees without setting variables
# Increase testdepth so that we can test all subtrees without setting variables
: $((testdepth++))
# Determine the best subtree match
for node_idx in "$@"; do
Expand All @@ -342,12 +342,12 @@ choice() {
fi
params=("${initial_params[@]}")
done
# Done checking, decrease the testdepth again
: $((testdepth--))
# Check if any subtree matched
if [[ -n $best_match_idx ]]; then
# Decrease testdepth and let the best-matching subtree set the variables
if [[ $((--testdepth)) -eq 0 ]]; then
"node_$best_match_idx"
fi
# Let the best-matching subtree set the variables
[[ $testdepth -eq 0 ]] && "node_$best_match_idx"
return 0
fi
# No subtree matched, reset the remaining params
Expand Down
11 changes: 11 additions & 0 deletions tests/docopt-sh-usecases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,14 @@ r"""Usage: prog ARG (-a|-b)
"""
$ prog -a A
{"ARG": "A", "-a": true, "-b": false}

#
# choice not reducing testdepth on failed match
#

r"""Usage:
prog (a|b) [OPT]
prog ARG
"""
$ prog test
{"a": false, "b": false, "ARG": "test", "OPT": null}

0 comments on commit bd2958f

Please sign in to comment.