-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug in expansion of variable length parameters (when count of parameters == 0) #53
Comments
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
Which was in the code too: Line 66 in 4c8b652
So I fixed it. Tanks for reporting. Lines 185 to 187 in e7d60ea
It wont fail with test in: So, I added a functional test case for that + unit test_case in go: docopts/tests/functional_tests_docopts.bats Lines 70 to 76 in e7d60ea
docopts/common_input_test.json Line 21 in e7d60ea
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
docopts/docopts.go
Line 185 in c211513
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 :) )docopts/docopts.go
Line 185 in c211513
Perhaps we could (maybe) move the
'
into theJoin
rather than outside?The text was updated successfully, but these errors were encountered: