From ef01e299fe964a687a36d126d491dd303995f142 Mon Sep 17 00:00:00 2001 From: Anders Ingemann Date: Sat, 17 Aug 2024 19:35:52 +0200 Subject: [PATCH] Fix bug where choice() didn't consume params on successful matching --- docopt_sh/docopt.sh | 5 +++-- tests/docopt-sh-usecases.txt | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/docopt_sh/docopt.sh b/docopt_sh/docopt.sh index 3759906..fe75ed3 100644 --- a/docopt_sh/docopt.sh +++ b/docopt_sh/docopt.sh @@ -346,8 +346,9 @@ choice() { : $((testdepth--)) # Check if any subtree matched if [[ -n $best_match_idx ]]; then - # Let the best-matching subtree set the variables - [[ $testdepth -eq 0 ]] && "node_$best_match_idx" + # Let the best-matching subtree consume the params + # and potentially set the variables + "node_$best_match_idx" return 0 fi return 1 diff --git a/tests/docopt-sh-usecases.txt b/tests/docopt-sh-usecases.txt index b0e96f6..74d8c25 100644 --- a/tests/docopt-sh-usecases.txt +++ b/tests/docopt-sh-usecases.txt @@ -137,3 +137,14 @@ Ending with an escaped doublequote: \" """ $ prog {} + +# +# Sequence of choices works +# + +r"""Usage: + prog (a|b) (c|d) + +""" +$ prog a d +{"a": true, "b": false, "c": false, "d": true}