[backport] run nimpretty on parsers

This commit is contained in:
narimiran 2019-09-27 11:01:51 +02:00
commit b17ed2ca9c
11 changed files with 207 additions and 193 deletions

View file

@ -155,11 +155,11 @@ import
os, strutils
type
CmdLineKind* = enum ## The detected command line token.
cmdEnd, ## End of command line reached
cmdArgument, ## An argument such as a filename
cmdLongOption, ## A long option such as --option
cmdShortOption ## A short option such as -c
CmdLineKind* = enum ## The detected command line token.
cmdEnd, ## End of command line reached
cmdArgument, ## An argument such as a filename
cmdLongOption, ## A long option such as --option
cmdShortOption ## A short option such as -c
OptParser* = object of RootObj ## \
## Implementation of the command line parser.
##
@ -172,10 +172,10 @@ type
longNoVal: seq[string]
cmds: seq[string]
idx: int
kind*: CmdLineKind ## The detected command line token
key*, val*: TaintedString ## Key and value pair; the key is the option
## or the argument, and the value is not "" if
## the option was given a value
kind*: CmdLineKind ## The detected command line token
key*, val*: TaintedString ## Key and value pair; the key is the option
## or the argument, and the value is not "" if
## the option was given a value
proc parseWord(s: string, i: int, w: var string,
delim: set[char] = {'\t', ' '}): int =
@ -197,7 +197,7 @@ when declared(os.paramCount):
# we cannot provide this for NimRtl creation on Posix, because we can't
# access the command line arguments then!
proc initOptParser*(cmdline = "", shortNoVal: set[char]={},
proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
longNoVal: seq[string] = @[];
allowWhitespaceAfterColon = true): OptParser =
## Initializes the command line parser.
@ -235,7 +235,7 @@ when declared(os.paramCount):
result.key = TaintedString""
result.val = TaintedString""
proc initOptParser*(cmdline: seq[TaintedString], shortNoVal: set[char]={},
proc initOptParser*(cmdline: seq[TaintedString], shortNoVal: set[char] = {},
longNoVal: seq[string] = @[];
allowWhitespaceAfterColon = true): OptParser =
## Initializes the command line parser.
@ -345,7 +345,8 @@ proc next*(p: var OptParser) {.rtl, extern: "npo$1".} =
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 i >= p.cmds[p.idx].len and p.idx < p.cmds.len and p.allowWhitespaceAfterColon:
if i >= p.cmds[p.idx].len and p.idx < p.cmds.len and
p.allowWhitespaceAfterColon:
inc p.idx
i = 0
if p.idx < p.cmds.len:
@ -403,7 +404,8 @@ proc remainingArgs*(p: OptParser): seq[TaintedString] {.rtl, extern: "npo$1".} =
result = @[]
for i in p.idx..<p.cmds.len: result.add TaintedString(p.cmds[i])
iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedString] =
iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key,
val: TaintedString] =
## Convenience iterator for iterating over the given
## `OptParser<#OptParser>`_.
##
@ -443,7 +445,7 @@ iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedSt
when declared(initOptParser):
iterator getopt*(cmdline: seq[TaintedString] = commandLineParams(),
shortNoVal: set[char]={}, longNoVal: seq[string] = @[]):
shortNoVal: set[char] = {}, longNoVal: seq[string] = @[]):
tuple[kind: CmdLineKind, key, val: TaintedString] =
## Convenience iterator for iterating over command line arguments.
##
@ -484,7 +486,8 @@ when declared(initOptParser):
## if filename == "":
## # no filename has been written, so we show the help
## writeHelp()
var p = initOptParser(cmdline, shortNoVal=shortNoVal, longNoVal=longNoVal)
var p = initOptParser(cmdline, shortNoVal = shortNoVal,
longNoVal = longNoVal)
while true:
next(p)
if p.kind == cmdEnd: break