minor parseopt.nim improvements (#21256)
* minor parseopt.nim improvements * attempt to make CI happy Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
This commit is contained in:
parent
f04f3e8bf7
commit
00ef27f4d1
2 changed files with 34 additions and 39 deletions
|
|
@ -1360,6 +1360,7 @@ proc to*[T](node: JsonNode, t: typedesc[T]): T =
|
||||||
doAssert data.list == @[1, 2, 3, 4]
|
doAssert data.list == @[1, 2, 3, 4]
|
||||||
|
|
||||||
var jsonPath = ""
|
var jsonPath = ""
|
||||||
|
result = default(T)
|
||||||
initFromJson(result, node, jsonPath)
|
initFromJson(result, node, jsonPath)
|
||||||
|
|
||||||
when false:
|
when false:
|
||||||
|
|
|
||||||
|
|
@ -213,36 +213,6 @@ proc parseWord(s: string, i: int, w: var string,
|
||||||
add(w, s[result])
|
add(w, s[result])
|
||||||
inc(result)
|
inc(result)
|
||||||
|
|
||||||
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
|
||||||
longNoVal: seq[string] = @[];
|
|
||||||
allowWhitespaceAfterColon = true): OptParser
|
|
||||||
|
|
||||||
proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
|
|
||||||
longNoVal: seq[string] = @[];
|
|
||||||
allowWhitespaceAfterColon = true): OptParser =
|
|
||||||
## Initializes the command line parser.
|
|
||||||
##
|
|
||||||
## If `cmdline == ""`, the real command line as provided by the
|
|
||||||
## `os` module is retrieved instead if it is available. If the
|
|
||||||
## command line is not available, a `ValueError` will be raised.
|
|
||||||
##
|
|
||||||
## `shortNoVal` and `longNoVal` are used to specify which options
|
|
||||||
## do not take values. See the `documentation about these
|
|
||||||
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
|
||||||
## how this affects parsing.
|
|
||||||
##
|
|
||||||
## This does not provide a way of passing default values to arguments.
|
|
||||||
##
|
|
||||||
## See also:
|
|
||||||
## * `getopt iterator<#getopt.i,OptParser>`_
|
|
||||||
runnableExamples:
|
|
||||||
var p = initOptParser()
|
|
||||||
p = initOptParser("--left --debug:3 -l -r:2")
|
|
||||||
p = initOptParser("--left --debug:3 -l -r:2",
|
|
||||||
shortNoVal = {'l'}, longNoVal = @["left"])
|
|
||||||
|
|
||||||
initOptParser(parseCmdLine(cmdline), shortNoVal, longNoVal, allowWhitespaceAfterColon)
|
|
||||||
|
|
||||||
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
||||||
longNoVal: seq[string] = @[];
|
longNoVal: seq[string] = @[];
|
||||||
allowWhitespaceAfterColon = true): OptParser =
|
allowWhitespaceAfterColon = true): OptParser =
|
||||||
|
|
@ -287,6 +257,32 @@ proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
|
||||||
result.key = ""
|
result.key = ""
|
||||||
result.val = ""
|
result.val = ""
|
||||||
|
|
||||||
|
proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
|
||||||
|
longNoVal: seq[string] = @[];
|
||||||
|
allowWhitespaceAfterColon = true): OptParser =
|
||||||
|
## Initializes the command line parser.
|
||||||
|
##
|
||||||
|
## If `cmdline == ""`, the real command line as provided by the
|
||||||
|
## `os` module is retrieved instead if it is available. If the
|
||||||
|
## command line is not available, a `ValueError` will be raised.
|
||||||
|
##
|
||||||
|
## `shortNoVal` and `longNoVal` are used to specify which options
|
||||||
|
## do not take values. See the `documentation about these
|
||||||
|
## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on
|
||||||
|
## how this affects parsing.
|
||||||
|
##
|
||||||
|
## This does not provide a way of passing default values to arguments.
|
||||||
|
##
|
||||||
|
## See also:
|
||||||
|
## * `getopt iterator<#getopt.i,OptParser>`_
|
||||||
|
runnableExamples:
|
||||||
|
var p = initOptParser()
|
||||||
|
p = initOptParser("--left --debug:3 -l -r:2")
|
||||||
|
p = initOptParser("--left --debug:3 -l -r:2",
|
||||||
|
shortNoVal = {'l'}, longNoVal = @["left"])
|
||||||
|
|
||||||
|
initOptParser(parseCmdLine(cmdline), shortNoVal, longNoVal, allowWhitespaceAfterColon)
|
||||||
|
|
||||||
proc handleShortOption(p: var OptParser; cmd: string) =
|
proc handleShortOption(p: var OptParser; cmd: string) =
|
||||||
var i = p.pos
|
var i = p.pos
|
||||||
p.kind = cmdShortOption
|
p.kind = cmdShortOption
|
||||||
|
|
@ -398,7 +394,6 @@ when declared(quoteShellCommand):
|
||||||
## p.next()
|
## p.next()
|
||||||
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
||||||
## break
|
## break
|
||||||
## else: continue
|
|
||||||
## doAssert p.cmdLineRest == "foo.txt bar.txt"
|
## doAssert p.cmdLineRest == "foo.txt bar.txt"
|
||||||
result = p.cmds[p.idx .. ^1].quoteShellCommand
|
result = p.cmds[p.idx .. ^1].quoteShellCommand
|
||||||
|
|
||||||
|
|
@ -416,7 +411,6 @@ proc remainingArgs*(p: OptParser): seq[string] {.rtl, extern: "npo$1".} =
|
||||||
## p.next()
|
## p.next()
|
||||||
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
|
||||||
## break
|
## break
|
||||||
## else: continue
|
|
||||||
## doAssert p.remainingArgs == @["foo.txt", "bar.txt"]
|
## doAssert p.remainingArgs == @["foo.txt", "bar.txt"]
|
||||||
result = @[]
|
result = @[]
|
||||||
for i in p.idx..<p.cmds.len: result.add p.cmds[i]
|
for i in p.idx..<p.cmds.len: result.add p.cmds[i]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue