Closed DanaLacoste closed 2 years ago
Hi @DanaLacoste
You're right:
# len of array in bash is ${#var_name[@]}
$ NAME=()
$ echo ${#NAME[@]}
0
$ NAME2=('')
$ echo ${#NAME2[@]}
1
lets try python's version 0.6.1
docker run -ti -v $PWD:/data debian-docpots docopts -h 'Usage: prog [NAME...]' :
NAME=()
Which was in the code too:
https://github.com/docopt/docopts/blob/4c8b652998a5ffda5068197f002b5ab5735761d6/docopts#L66
So I fixed it. Tanks for reporting.
https://github.com/docopt/docopts/blob/e7d60ea17b98db2b1314d7eee7bdf3a3b80722d2/docopts.go#L185-L187
It wont fail with test in: testcases.docopt
as test are run through testee.sh
which in turn uses -A
mode which uses a different code path.
So, I added a functional test case for that + unit test_case in go:
Also test are now more documented:
Let me know it it works as expected for you.
Yep, that looks perfect, thanks!
Description
docopts.go is expanding empty arrays to be a 1-element array with a single null/empty string element in https://github.com/docopt/docopts/blob/c21151315bea2852ef3250c70a52352c9cd0d6eb/docopts.go#L185
Details
OK, this one is a bit edge-case-y, but it causes an issue with the way bash expansion works. Specifically, docopts is not creating an empty array when a
...
element is defined, but no parameters are given. Instead, it creates an array containing a singlenull
element.This gets confusing, because bash considers it an empty array (if you query the length of the array) as the element is
null
, but if you pass it using standard bash expansion, it will be expanded to an empty string which will confuse any downstream commands.Test Case 1
Here is the test script (I tried to use the use case defined in testcases.docopt, but I could not trace down exactly why that specific test case is not failing here)
NOTE: You need two scripts to see the problem clearly :)
test1.sh: this just runs the basic test case which has the bug:
and test2.sh:
And the results:
As you can see:
Test Case 2: Seeing this in pure bash, without docopts
So, let's find the root cause: what does docopts output that bash doesn't handle well?
test3.sh:
And the result:
Conclusion:
Somehow, this is expanding the empty array once: can we change it so that it just emits a
name=()
instead ofname=('')
for this use case? (I need to learn more go :) ) https://github.com/docopt/docopts/blob/c21151315bea2852ef3250c70a52352c9cd0d6eb/docopts.go#L185Perhaps we could (maybe) move the
'
into theJoin
rather than outside?