Skip to content

Commit c101655

Browse files
committed
Fix for multiple argument values
1 parent 4308f90 commit c101655

File tree

3 files changed

+84
-31
lines changed

3 files changed

+84
-31
lines changed

docopt.coffee

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Pattern
3535
fix: ->
3636
@fix_identities()
3737
@fix_list_arguments()
38+
@fix_list_options()
3839

3940
fix_identities: (uniq=null) ->
4041
"""Make pattern-tree tips point to same object if they are equal."""
@@ -64,6 +65,17 @@ class Pattern
6465
when counts[e] > 1 and e.constructor is Argument
6566
@
6667

68+
fix_list_options: ->
69+
"""Find options that should accumulate values and fix them."""
70+
either = (c.children for c in @either().children)
71+
for child in either
72+
counts = {}
73+
for c in child
74+
counts[c] = (counts[c] ? 0) + 1
75+
e.value = [] for e in child \
76+
when counts[e] > 1 and e.constructor is Option
77+
@
78+
6779
either: ->
6880
if not @hasOwnProperty 'children'
6981
return new Either [new Required [@]]
@@ -182,8 +194,16 @@ class Option extends Pattern
182194
new Option short, long, argcount, value
183195

184196
match: (left, collected=[]) ->
185-
left_ = (l for l in left when (l.constructor isnt Option \
186-
or @short isnt l.short or @long isnt l.long))
197+
left_ = []
198+
values = []
199+
for l in left
200+
if l.constructor is Option and @short is l.short and @long is l.long
201+
values.push l.value
202+
else
203+
left_.push l
204+
if @value.constructor is Array
205+
@value = @value.concat(values)
206+
collected.push @
187207
[left.join(', ') isnt left_.join(', '), left_, collected]
188208

189209
class AnyOptions extends Pattern

docopt.js

Lines changed: 47 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test_docopt.coffee

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ setup()
7676

7777
#{new Option, TokenStream, parse_shorts, parse_long,
7878
# parse_args, printable_usage, docopt} = module
79-
`with (require('./docopt')) { //`
79+
`with (require('./docopt.coffee')) { //`
8080

8181
test "Option.parse", ->
8282
eq(
@@ -394,6 +394,16 @@ test "parse_pattern", ->
394394
]
395395
]
396396
)
397+
eq(
398+
parse_pattern '(--file <val>)...', o
399+
new Required [
400+
new OneOrMore [
401+
new Required [
402+
new Option '-f', '--file', 1, '<val>'
403+
]
404+
]
405+
]
406+
)
397407
eq(
398408
parse_pattern('[ -h | -v ]', o)
399409
new Required [
@@ -1027,6 +1037,10 @@ test "options_without_description", ->
10271037
new Dict([['-v', true], ['--verbose', false]]))
10281038
eq(docopt('usage: git remote [-v | --verbose]', argv: 'remote -v'),
10291039
new Dict([['remote', true], ['-v', true], ['--verbose', false]]))
1040+
eq(docopt('usage: cmd (--file=<val>)... --l=<x>', argv: '--file=first --file=second --file=third --l=1'),
1041+
new Dict([['--file', ['first', 'second', 'third']], ['--l', 1]]))
1042+
eq(docopt('usage: cmd (--file=<val>)... --l=<x>...', argv: '--file=first --l=1 --file=second --file=third --l=2'),
1043+
new Dict([['--file', ['first', 'second', 'third']], ['--l', [1, 2]]]))
10301044

10311045

10321046
test 'allow_single_underscore', ->

0 commit comments

Comments
 (0)