fixes #9619
This commit is contained in:
parent
fdd09ad090
commit
c0fc2f5726
3 changed files with 55 additions and 38 deletions
|
|
@ -72,6 +72,11 @@ proc enumToString*(enums: openArray[enum]): string =
|
||||||
|
|
||||||
- Complex type is now generic and not a tuple anymore.
|
- Complex type is now generic and not a tuple anymore.
|
||||||
|
|
||||||
|
- The `parseopt` module now supports a new flag `allowWhitespaceAfterColon`
|
||||||
|
(default value: true) that can be set to `false` for better Posix
|
||||||
|
interoperability. (Bug #9619.)
|
||||||
|
|
||||||
|
|
||||||
### Language additions
|
### Language additions
|
||||||
|
|
||||||
- Vm suport for float32<->int32 and float64<->int64 casts was added.
|
- Vm suport for float32<->int32 and float64<->int64 casts was added.
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,7 @@ type
|
||||||
cmd*: string # cmd,pos exported so caller can catch "--" as..
|
cmd*: string # cmd,pos exported so caller can catch "--" as..
|
||||||
pos*: int # ..empty key or subcmd cmdArg & handle specially
|
pos*: int # ..empty key or subcmd cmdArg & handle specially
|
||||||
inShortState: bool
|
inShortState: bool
|
||||||
|
allowWhitespaceAfterColon: bool
|
||||||
shortNoVal: set[char]
|
shortNoVal: set[char]
|
||||||
longNoVal: seq[string]
|
longNoVal: seq[string]
|
||||||
cmds: seq[string]
|
cmds: seq[string]
|
||||||
|
|
@ -95,7 +96,8 @@ when declared(os.paramCount):
|
||||||
# access the command line arguments then!
|
# access the command line arguments then!
|
||||||
|
|
||||||
proc initOptParser*(cmdline = "", shortNoVal: set[char]={},
|
proc initOptParser*(cmdline = "", shortNoVal: set[char]={},
|
||||||
longNoVal: seq[string] = @[]): OptParser =
|
longNoVal: seq[string] = @[];
|
||||||
|
allowWhitespaceAfterColon = true): OptParser =
|
||||||
## inits the option parser. If ``cmdline == ""``, the real command line
|
## inits the option parser. If ``cmdline == ""``, the real command line
|
||||||
## (as provided by the ``OS`` module) is taken. If ``shortNoVal`` is
|
## (as provided by the ``OS`` module) is taken. If ``shortNoVal`` is
|
||||||
## provided command users do not need to delimit short option keys and
|
## provided command users do not need to delimit short option keys and
|
||||||
|
|
@ -108,6 +110,7 @@ when declared(os.paramCount):
|
||||||
result.inShortState = false
|
result.inShortState = false
|
||||||
result.shortNoVal = shortNoVal
|
result.shortNoVal = shortNoVal
|
||||||
result.longNoVal = longNoVal
|
result.longNoVal = longNoVal
|
||||||
|
result.allowWhitespaceAfterColon = allowWhitespaceAfterColon
|
||||||
if cmdline != "":
|
if cmdline != "":
|
||||||
result.cmd = cmdline
|
result.cmd = cmdline
|
||||||
result.cmds = parseCmdLine(cmdline)
|
result.cmds = parseCmdLine(cmdline)
|
||||||
|
|
@ -124,7 +127,8 @@ when declared(os.paramCount):
|
||||||
result.val = TaintedString""
|
result.val = TaintedString""
|
||||||
|
|
||||||
proc initOptParser*(cmdline: seq[TaintedString], shortNoVal: set[char]={},
|
proc initOptParser*(cmdline: seq[TaintedString], shortNoVal: set[char]={},
|
||||||
longNoVal: seq[string] = @[]): OptParser =
|
longNoVal: seq[string] = @[];
|
||||||
|
allowWhitespaceAfterColon = true): OptParser =
|
||||||
## inits the option parser. If ``cmdline.len == 0``, the real command line
|
## inits the option parser. If ``cmdline.len == 0``, the real command line
|
||||||
## (as provided by the ``OS`` module) is taken. ``shortNoVal`` and
|
## (as provided by the ``OS`` module) is taken. ``shortNoVal`` and
|
||||||
## ``longNoVal`` behavior is the same as for ``initOptParser(string,...)``.
|
## ``longNoVal`` behavior is the same as for ``initOptParser(string,...)``.
|
||||||
|
|
@ -133,6 +137,7 @@ when declared(os.paramCount):
|
||||||
result.inShortState = false
|
result.inShortState = false
|
||||||
result.shortNoVal = shortNoVal
|
result.shortNoVal = shortNoVal
|
||||||
result.longNoVal = longNoVal
|
result.longNoVal = longNoVal
|
||||||
|
result.allowWhitespaceAfterColon = allowWhitespaceAfterColon
|
||||||
result.cmd = ""
|
result.cmd = ""
|
||||||
if cmdline.len != 0:
|
if cmdline.len != 0:
|
||||||
result.cmds = newSeq[string](cmdline.len)
|
result.cmds = newSeq[string](cmdline.len)
|
||||||
|
|
@ -210,7 +215,7 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
|
||||||
inc(i)
|
inc(i)
|
||||||
while i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {'\t', ' '}: inc(i)
|
while i < p.cmds[p.idx].len and p.cmds[p.idx][i] in {'\t', ' '}: inc(i)
|
||||||
# if we're at the end, use the next command line option:
|
# if we're at the end, use the next command line option:
|
||||||
if i >= p.cmds[p.idx].len and p.idx < p.cmds.len:
|
if i >= p.cmds[p.idx].len and p.idx < p.cmds.len and p.allowWhitespaceAfterColon:
|
||||||
inc p.idx
|
inc p.idx
|
||||||
i = 0
|
i = 0
|
||||||
p.val = TaintedString p.cmds[p.idx].substr(i)
|
p.val = TaintedString p.cmds[p.idx].substr(i)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ kind: cmdLongOption key:val -- left:
|
||||||
kind: cmdLongOption key:val -- debug:3
|
kind: cmdLongOption key:val -- debug:3
|
||||||
kind: cmdShortOption key:val -- l:4
|
kind: cmdShortOption key:val -- l:4
|
||||||
kind: cmdShortOption key:val -- r:2
|
kind: cmdShortOption key:val -- r:2
|
||||||
|
cmdLongOption foo
|
||||||
|
cmdLongOption path
|
||||||
parseoptNoVal
|
parseoptNoVal
|
||||||
kind: cmdLongOption key:val -- left:
|
kind: cmdLongOption key:val -- left:
|
||||||
kind: cmdLongOption key:val -- debug:3
|
kind: cmdLongOption key:val -- debug:3
|
||||||
|
|
@ -50,6 +52,11 @@ block:
|
||||||
for kind, key, val in parseopt.getopt(p):
|
for kind, key, val in parseopt.getopt(p):
|
||||||
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
|
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
|
||||||
|
|
||||||
|
# bug #9619
|
||||||
|
var x = parseopt.initOptParser(@["--foo:", "--path"], allowWhitespaceAfterColon = false)
|
||||||
|
for kind, key, val in parseopt.getopt(x):
|
||||||
|
echo kind, " ", key
|
||||||
|
|
||||||
block:
|
block:
|
||||||
echo "parseoptNoVal"
|
echo "parseoptNoVal"
|
||||||
# test NoVal mode with custom cmdline arguments
|
# test NoVal mode with custom cmdline arguments
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue