Fix : and = precedence in parseopt.

This commit is contained in:
Michał Zieliński 2013-12-10 19:10:06 +01:00
commit 896766ae2c
2 changed files with 6 additions and 5 deletions

View file

@ -71,7 +71,7 @@ when not defined(createNimRtl):
proc next*(p: var TOptParser) {.rtl, extern: "npo$1".} proc next*(p: var TOptParser) {.rtl, extern: "npo$1".}
proc nextOption(p: var TOptParser, token: string, allowEmpty: bool) = proc nextOption(p: var TOptParser, token: string, allowEmpty: bool) =
for splitchar in ['=', ':']: for splitchar in [':', '=']:
if splitchar in token: if splitchar in token:
let pos = token.find(splitchar) let pos = token.find(splitchar)
p.key = token[0..pos-1] p.key = token[0..pos-1]

View file

@ -7,11 +7,12 @@ let argv = commandLineParams()
if argv == @[]: if argv == @[]:
# this won't work with spaces # this won't work with spaces
assert execShellCmd(getAppFilename() & " \"foo bar\" --aa:bar --ab -c --a[baz]:doo") == 0 assert execShellCmd(getAppFilename() & " \"foo bar\" --aa:bar=a --a=c:d --ab -c --a[baz]:doo") == 0
else: else:
let f = toSeq(getopt()) let f = toSeq(getopt())
echo f.repr echo f.repr
assert f[0].kind == cmdArgument and f[0].key == "foo bar" and f[0].val == "" assert f[0].kind == cmdArgument and f[0].key == "foo bar" and f[0].val == ""
assert f[1].kind == cmdLongOption and f[1].key == "aa" and f[1].val == "bar" assert f[1].kind == cmdLongOption and f[1].key == "aa" and f[1].val == "bar=a"
assert f[2].kind == cmdLongOption and f[2].key == "ab" and f[2].val == "" assert f[2].kind == cmdLongOption and f[2].key == "a=c" and f[2].val == "d"
assert f[3].kind == cmdShortOption and f[3].key == "c" and f[3].val == "" assert f[3].kind == cmdLongOption and f[3].key == "ab" and f[3].val == ""
assert f[4].kind == cmdShortOption and f[4].key == "c" and f[4].val == ""