fixes parseopt/parseopt2 custom cmdline args.
This commit is contained in:
parent
c59bc0cc18
commit
27e2a34340
3 changed files with 117 additions and 30 deletions
|
|
@ -149,26 +149,41 @@ 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
|
||||||
|
|
||||||
|
iterator getopt*(p: var OptParser): tuple[kind: CmdLineKind, key, val: TaintedString] =
|
||||||
|
## This is an convenience iterator for iterating over the given OptParser object.
|
||||||
|
## Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: nim
|
||||||
|
## var p = initOptParser("--left --debug:3 -l=4 -r:2")
|
||||||
|
## for kind, key, val in p.getopt():
|
||||||
|
## case kind
|
||||||
|
## of cmdArgument:
|
||||||
|
## filename = key
|
||||||
|
## of cmdLongOption, cmdShortOption:
|
||||||
|
## case key
|
||||||
|
## of "help", "h": writeHelp()
|
||||||
|
## of "version", "v": writeVersion()
|
||||||
|
## of cmdEnd: assert(false) # cannot happen
|
||||||
|
## if filename == "":
|
||||||
|
## # no filename has been given, so we show the help:
|
||||||
|
## writeHelp()
|
||||||
|
p.pos = 0
|
||||||
|
while true:
|
||||||
|
next(p)
|
||||||
|
if p.kind == cmdEnd: break
|
||||||
|
yield (p.kind, p.key, p.val)
|
||||||
|
|
||||||
when declared(initOptParser):
|
when declared(initOptParser):
|
||||||
iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] =
|
iterator getopt*(): tuple[kind: CmdLineKind, key, val: TaintedString] =
|
||||||
## This is an convenience iterator for iterating over the command line.
|
## This is an convenience iterator for iterating over the command line arguments.
|
||||||
## This uses the OptParser object. Example:
|
## This create a new OptParser object.
|
||||||
|
## See above for a more detailed example
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var
|
|
||||||
## filename = ""
|
|
||||||
## for kind, key, val in getopt():
|
## for kind, key, val in getopt():
|
||||||
## case kind
|
## # this will iterate over all arguments passed to the cmdline.
|
||||||
## of cmdArgument:
|
## continue
|
||||||
## filename = key
|
##
|
||||||
## of cmdLongOption, cmdShortOption:
|
|
||||||
## case key
|
|
||||||
## of "help", "h": writeHelp()
|
|
||||||
## of "version", "v": writeVersion()
|
|
||||||
## of cmdEnd: assert(false) # cannot happen
|
|
||||||
## if filename == "":
|
|
||||||
## # no filename has been given, so we show the help:
|
|
||||||
## writeHelp()
|
|
||||||
var p = initOptParser()
|
var p = initOptParser()
|
||||||
while true:
|
while true:
|
||||||
next(p)
|
next(p)
|
||||||
|
|
|
||||||
|
|
@ -123,26 +123,41 @@ type
|
||||||
|
|
||||||
{.deprecated: [TGetoptResult: GetoptResult].}
|
{.deprecated: [TGetoptResult: GetoptResult].}
|
||||||
|
|
||||||
|
iterator getopt*(p: var OptParser): GetoptResult =
|
||||||
|
## This is an convenience iterator for iterating over the given OptParser object.
|
||||||
|
## Example:
|
||||||
|
##
|
||||||
|
## .. code-block:: nim
|
||||||
|
## var p = initOptParser("--left --debug:3 -l=4 -r:2")
|
||||||
|
## for kind, key, val in p.getopt():
|
||||||
|
## case kind
|
||||||
|
## of cmdArgument:
|
||||||
|
## filename = key
|
||||||
|
## of cmdLongOption, cmdShortOption:
|
||||||
|
## case key
|
||||||
|
## of "help", "h": writeHelp()
|
||||||
|
## of "version", "v": writeVersion()
|
||||||
|
## of cmdEnd: assert(false) # cannot happen
|
||||||
|
## if filename == "":
|
||||||
|
## # no filename has been given, so we show the help:
|
||||||
|
## writeHelp()
|
||||||
|
p.pos = 0
|
||||||
|
while true:
|
||||||
|
next(p)
|
||||||
|
if p.kind == cmdEnd: break
|
||||||
|
yield (p.kind, p.key, p.val)
|
||||||
|
|
||||||
when declared(paramCount):
|
when declared(paramCount):
|
||||||
iterator getopt*(): GetoptResult =
|
iterator getopt*(): GetoptResult =
|
||||||
## This is an convenience iterator for iterating over the command line.
|
## This is an convenience iterator for iterating over the command line arguments.
|
||||||
## This uses the OptParser object. Example:
|
## This create a new OptParser object.
|
||||||
|
## See above for a more detailed example
|
||||||
##
|
##
|
||||||
## .. code-block:: nim
|
## .. code-block:: nim
|
||||||
## var
|
|
||||||
## filename = ""
|
|
||||||
## for kind, key, val in getopt():
|
## for kind, key, val in getopt():
|
||||||
## case kind
|
## # this will iterate over all arguments passed to the cmdline.
|
||||||
## of cmdArgument:
|
## continue
|
||||||
## filename = key
|
##
|
||||||
## of cmdLongOption, cmdShortOption:
|
|
||||||
## case key
|
|
||||||
## of "help", "h": writeHelp()
|
|
||||||
## of "version", "v": writeVersion()
|
|
||||||
## of cmdEnd: assert(false) # cannot happen
|
|
||||||
## if filename == "":
|
|
||||||
## # no filename has been given, so we show the help:
|
|
||||||
## writeHelp()
|
|
||||||
var p = initOptParser()
|
var p = initOptParser()
|
||||||
while true:
|
while true:
|
||||||
next(p)
|
next(p)
|
||||||
|
|
|
||||||
57
tests/misc/tparseopt.nim
Normal file
57
tests/misc/tparseopt.nim
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue