Merge pull request #5952 from Parashurama/fix_parseopt

Fix parseopt custom arguments
This commit is contained in:
Varriount 2017-06-13 04:11:58 -04:00 • committed by GitHub
commit a939a4b3a4
3 changed files with 117 additions and 30 deletions

View file

@ -149,15 +149,13 @@ proc cmdLineRest*(p: OptParser): TaintedString {.rtl, extern: "npo$1".} =
## retrieves the rest of the command line that has not been parsed yet. ## retrieves the rest of the command line that has not been parsed yet.
result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)).TaintedString result = strip(substr(p.cmd, p.pos, len(p.cmd) - 1)).TaintedString
when declared(initOptParser): iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedString] =
iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] = ## This is an convenience iterator for iterating over the given OptParser object.
## This is an convenience iterator for iterating over the command line. ## Example:
## This uses the OptParser object. Example:
## ##
## .. code-block:: nim ## .. code-block:: nim
## var ## var p = initOptParser("--left --debug:3 -l=4 -r:2")
## filename = "" ## for kind, key, val in p.getopt():
## for kind, key, val in getopt():
## case kind ## case kind
## of cmdArgument: ## of cmdArgument:
## filename = key ## filename = key
@ -169,6 +167,23 @@ when declared(initOptParser):
## if filename == "": ## if filename == "":
## # no filename has been given, so we show the help: ## # no filename has been given, so we show the help:
## writeHelp() ## writeHelp()
p.pos = 0
while true:
next(p)
if p.kind == cmdEnd: break
yield (p.kind, p.key, p.val)
when declared(initOptParser):
iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] =
## This is an convenience iterator for iterating over the command line arguments.
## This create a new OptParser object.
## See above for a more detailed example
##
## .. code-block:: nim
## for kind, key, val in getopt():
## # this will iterate over all arguments passed to the cmdline.
## continue
##
var p = initOptParser() var p = initOptParser()
while true: while true:
next(p) next(p)

View file

@ -123,15 +123,13 @@ type
{.deprecated: [TGetoptResult: GetoptResult].} {.deprecated: [TGetoptResult: GetoptResult].}
when declared(paramCount): iterator getopt*(p: var OptParser): GetoptResult =
iterator getopt*(): GetoptResult = ## This is an convenience iterator for iterating over the given OptParser object.
## This is an convenience iterator for iterating over the command line. ## Example:
## This uses the OptParser object. Example:
## ##
## .. code-block:: nim ## .. code-block:: nim
## var ## var p = initOptParser("--left --debug:3 -l=4 -r:2")
## filename = "" ## for kind, key, val in p.getopt():
## for kind, key, val in getopt():
## case kind ## case kind
## of cmdArgument: ## of cmdArgument:
## filename = key ## filename = key
@ -143,6 +141,23 @@ when declared(paramCount):
## if filename == "": ## if filename == "":
## # no filename has been given, so we show the help: ## # no filename has been given, so we show the help:
## writeHelp() ## writeHelp()
p.pos = 0
while true:
next(p)
if p.kind == cmdEnd: break
yield (p.kind, p.key, p.val)
when declared(paramCount):
iterator getopt*(): GetoptResult =
## This is an convenience iterator for iterating over the command line arguments.
## This create a new OptParser object.
## See above for a more detailed example
##
## .. code-block:: nim
## for kind, key, val in getopt():
## # this will iterate over all arguments passed to the cmdline.
## continue
##
var p = initOptParser() var p = initOptParser()
while true: while true:
next(p) next(p)

57
tests/misc/tparseopt.nim Normal file
View file

@ -0,0 +1,57 @@
discard """
file: "tparseopt.nim"
output: '''
parseopt
first round
kind: cmdLongOption key:val -- left:
second round
kind: cmdLongOption key:val -- left:
kind: cmdLongOption key:val -- debug:3
kind: cmdShortOption key:val -- l:4
kind: cmdShortOption key:val -- r:2
parseopt2
first round
kind: cmdLongOption key:val -- left:
second round
kind: cmdLongOption key:val -- left:
kind: cmdLongOption key:val -- debug:3
kind: cmdShortOption key:val -- l:4
kind: cmdShortOption key:val -- r:2'''
"""
from parseopt import nil
from parseopt2 import nil
block:
echo "parseopt"
for kind, key, val in parseopt.getopt():
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
# pass custom cmdline arguments
echo "first round"
var argv = "--left --debug:3 -l=4 -r:2"
var p = parseopt.initOptParser(argv)
for kind, key, val in parseopt.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
break
# reset getopt iterator and check arguments are returned correctly.
echo "second round"
for kind, key, val in parseopt.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
block:
echo "parseopt2"
for kind, key, val in parseopt2.getopt():
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
# pass custom cmdline arguments
echo "first round"
var argv: seq[string] = @["--left", "--debug:3", "-l=4", "-r:2"]
var p = parseopt2.initOptParser(argv)
for kind, key, val in parseopt2.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
break
# reset getopt iterator and check arguments are returned correctly.
echo "second round"
for kind, key, val in parseopt2.getopt(p):
echo "kind: ", kind, "\tkey:val -- ", key, ":", val