Skip to content
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

Fix for multiple argument values #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions docopt.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Pattern
fix: ->
@fix_identities()
@fix_list_arguments()
@fix_list_options()

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

fix_list_options: ->
"""Find options that should accumulate values and fix them."""
either = (c.children for c in @either().children)
for child in either
counts = {}
for c in child
counts[c] = (counts[c] ? 0) + 1
e.value = [] for e in child \
when counts[e] > 1 and e.constructor is Option
@

either: ->
if not @hasOwnProperty 'children'
return new Either [new Required [@]]
Expand Down Expand Up @@ -182,8 +194,16 @@ class Option extends Pattern
new Option short, long, argcount, value

match: (left, collected=[]) ->
left_ = (l for l in left when (l.constructor isnt Option \
or @short isnt l.short or @long isnt l.long))
left_ = []
values = []
for l in left
if l.constructor is Option and @short is l.short and @long is l.long
values.push l.value
else
left_.push l
if @value.constructor is Array
@value = @value.concat(values)
collected.push @
[left.join(', ') isnt left_.join(', '), left_, collected]

class AnyOptions extends Pattern
Expand Down
75 changes: 47 additions & 28 deletions docopt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion test_docopt.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ setup()

#{new Option, TokenStream, parse_shorts, parse_long,
# parse_args, printable_usage, docopt} = module
`with (require('./docopt')) { //`
`with (require('./docopt.coffee')) { //`

test "Option.parse", ->
eq(
Expand Down Expand Up @@ -394,6 +394,16 @@ test "parse_pattern", ->
]
]
)
eq(
parse_pattern '(--file <val>)...', o
new Required [
new OneOrMore [
new Required [
new Option '-f', '--file', 1, '<val>'
]
]
]
)
eq(
parse_pattern('[ -h | -v ]', o)
new Required [
Expand Down Expand Up @@ -1027,6 +1037,10 @@ test "options_without_description", ->
new Dict([['-v', true], ['--verbose', false]]))
eq(docopt('usage: git remote [-v | --verbose]', argv: 'remote -v'),
new Dict([['remote', true], ['-v', true], ['--verbose', false]]))
eq(docopt('usage: cmd (--file=<val>)... --l=<x>', argv: '--file=first --file=second --file=third --l=1'),
new Dict([['--file', ['first', 'second', 'third']], ['--l', 1]]))
eq(docopt('usage: cmd (--file=<val>)... --l=<x>...', argv: '--file=first --l=1 --file=second --file=third --l=2'),
new Dict([['--file', ['first', 'second', 'third']], ['--l', [1, 2]]]))


test 'allow_single_underscore', ->
Expand Down