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:
Andreas Rumpf 2023-01-18 21:37:26 +01:00 • committed by GitHub
commit 00ef27f4d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 39 deletions

View file

@ -1360,6 +1360,7 @@ proc to*[T](node: JsonNode, t: typedesc[T]): T =
doAssert data.list == @[1, 2, 3, 4]
var jsonPath = ""
result = default(T)
initFromJson(result, node, jsonPath)
when false:

View file

@ -213,36 +213,6 @@ proc parseWord(s: string, i: int, w: var string,
add(w, s[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] = {},
longNoVal: seq[string] = @[];
allowWhitespaceAfterColon = true): OptParser =
@ -287,6 +257,32 @@ proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
result.key = ""
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) =
var i = p.pos
p.kind = cmdShortOption
@ -379,7 +375,7 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
handleShortOption(p, p.cmds[p.idx])
else:
p.kind = cmdArgument
p.key = p.cmds[p.idx]
p.key = p.cmds[p.idx]
inc p.idx
p.pos = 0
@ -398,7 +394,6 @@ when declared(quoteShellCommand):
## p.next()
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
## break
## else: continue
## doAssert p.cmdLineRest == "foo.txt bar.txt"
result = p.cmds[p.idx .. ^1].quoteShellCommand
@ -416,7 +411,6 @@ proc remainingArgs*(p: OptParser): seq[string] {.rtl, extern: "npo$1".} =
## p.next()
## if p.kind == cmdLongOption and p.key == "": # Look for "--"
## break
## else: continue
## doAssert p.remainingArgs == @["foo.txt", "bar.txt"]
result = @[]
for i in p.idx..<p.cmds.len: result.add p.cmds[i]