diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2023-01-18 21:37:26 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-18 21:37:26 +0100 |
commit | 00ef27f4d18bcd5f56132d9ac5dfab840ef4e6fe (patch) | |
tree | 2bb67372519fd57a97dc96f10e8538e8ee2c17f7 | |
parent | f04f3e8bf78ab4187a395a024c94cd545c275720 (diff) | |
download | Nim-00ef27f4d18bcd5f56132d9ac5dfab840ef4e6fe.tar.gz |
minor parseopt.nim improvements (#21256)
* minor parseopt.nim improvements * attempt to make CI happy Co-authored-by: Clay Sweetser <Varriount@users.noreply.github.com>
-rw-r--r-- | lib/pure/json.nim | 1 | ||||
-rw-r--r-- | lib/pure/parseopt.nim | 72 |
2 files changed, 34 insertions, 39 deletions
diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 78d6bd5bd..e881fbff7 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -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: diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim index 9052067db..059f8e0f5 100644 --- a/lib/pure/parseopt.nim +++ b/lib/pure/parseopt.nim @@ -74,9 +74,9 @@ ## ## The `getopt iterator<#getopt.i,OptParser>`_, which is provided for ## convenience, can be used to iterate through all command line options as well. -## +## ## To set a default value for a variable assigned through `getopt` and accept arguments from the cmd line. -## Assign the default value to a variable before parsing. +## Assign the default value to a variable before parsing. ## Then set the variable to the new value while parsing. ## ## Here is an example: @@ -84,7 +84,7 @@ ## import std/parseopt ## ## var varName: string = "defaultValue" -## +## ## for kind, key, val in getopt(): ## case kind ## of cmdArgument: @@ -95,7 +95,7 @@ ## varName = val # do input sanitization in production systems ## of cmdEnd: ## discard -## +## ## `shortNoVal` and `longNoVal` ## ============================ ## @@ -215,36 +215,6 @@ proc parseWord(s: string, i: int, w: var string, 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 = ## Initializes the command line parser. ## @@ -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] @@ -426,7 +420,7 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, ## Convenience iterator for iterating over the given ## `OptParser<#OptParser>`_. ## - ## There is no need to check for `cmdEnd` while iterating. If using `getopt` + ## There is no need to check for `cmdEnd` while iterating. If using `getopt` ## with case switching, checking for `cmdEnd` is required. ## ## See also: @@ -475,7 +469,7 @@ iterator getopt*(cmdline: seq[string] = @[], ## parameters<#nimshortnoval-and-nimlongnoval>`_ for more information on ## how this affects parsing. ## - ## There is no need to check for `cmdEnd` while iterating. If using `getopt` + ## There is no need to check for `cmdEnd` while iterating. If using `getopt` ## with case switching, checking for `cmdEnd` is required. ## ## See also: |