Values of arguments with ellipsis get doubled if other optional elements are present
See original GitHub issueFirst I’d like to say: docopt is great Python stuff! Thank you Vladimir!
Meanwhile I use docopt for quite complex CLIs, and I think a found a minor, well-hidden bug in docopt 0.6.1.
To trigger the bug there have to be at least two different usage lines, with at least one of them containing an optional element and an argument with ellipses. Ordering does not matter, nor if the ellipses are inside or after () or []. All but the first argument value get duplicated. With three usage lines these values get trippled and so on…
import sys
from docopt import docopt
USAGE = """\
Usage:
counted_example2.py [-a] [-x VAL]...
counted_example2.py [-b] [-x VAL]...
Options:
-a optional a option
-b optional b option
-x VAL multiple x arguments
"""
# these work as expected:
sys.argv = 'counted_example2.py -b'.split()
print str(docopt(USAGE)) + '\n'
sys.argv = 'counted_example2.py -a -x 3'.split()
print str(docopt(USAGE)) + '\n'
# with multiple -x all but first value are duplicated:
sys.argv = 'counted_example2.py -x 1 -x 2'.split()
print str(docopt(USAGE)) + '\n'
sys.argv = 'counted_example2.py -b -x 4 -x 5 -x 6'.split()
print str(docopt(USAGE)) + '\n'
# e.g. last print shows:
# {'-a': False,
# '-b': True,
# '-x': ['4', '5', '6', '5', '6']}
Issue Analytics
- State:
- Created 10 years ago
- Reactions:3
- Comments:9 (1 by maintainers)
Top Results From Across the Web
Python 3: Ellipsis in function parameters? - Stack Overflow
So I assume that the use of ellipsis here is a "cute" way to check that the argument wasn't passed on this function's...
Read more >unnecessary-ellipsis / W2301 - Pylint 2.16.0-dev documentation
A line of code consisting of an ellipsis is unnecessary if there is a docstring on the preceding line or if there is...
Read more >What do the three dots (…) mean in JavaScript? - Adrian Oprea
This means that the code below will result in you having an array with duplicate elements. Think of it as a replacement for...
Read more >12.6 — Ellipsis (and why to avoid them) - Learn C++
In this case, the problem is that the double we passed in as the first ellipsis argument is 8 bytes, whereas va_arg(list, int)...
Read more >Lodash Documentation
If array can't be split evenly, the final chunk will be the remaining elements. ... Creates an array of array values not included...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found this one particularly tricky to deal with. Appears that there’s actually two separate issues at play.
If one demonstrates both short and long-hand option in the usage list, this duplicates each usage as well.
Assuming a user is consistent with their usage, I have the following code that appears to resolve the issue
Details
Another way to trigger the bug:
http://try.docopt.org/?doc=Usage%3A ++naval_fate.py+<name>…±-speed%3D%3Ckn%3E…%0D%0A++naval_fate.py+%3Cname%3E…±-speed%3D%3Ckn%3E…%0D%0A%0D%0AOptions%3A%0D%0A+±-speed%3D%3Ckn%3E++Speed%0D%0A%0D%0A&argv=name1+name2±-speed%3D20±-speed%3D30