parent
e17321aa24
commit
0a8762eb7b
3 changed files with 94 additions and 68 deletions
|
|
@ -30,54 +30,102 @@ 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
|
||||
|
||||
when defined(testament_tparseopt):
|
||||
import os
|
||||
proc main() =
|
||||
let args = commandLineParams()
|
||||
echo args
|
||||
for i, ai in args:
|
||||
echo "arg ", i, " ai.len:", ai.len, " :{", ai, "}"
|
||||
main()
|
||||
else:
|
||||
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
|
||||
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
|
||||
# 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
|
||||
|
||||
# bug #9619
|
||||
var x = parseopt.initOptParser(@["--foo:", "--path"], allowWhitespaceAfterColon = false)
|
||||
for kind, key, val in parseopt.getopt(x):
|
||||
echo kind, " ", key
|
||||
# bug #9619
|
||||
var x = parseopt.initOptParser(@["--foo:", "--path"],
|
||||
allowWhitespaceAfterColon = false)
|
||||
for kind, key, val in parseopt.getopt(x):
|
||||
echo kind, " ", key
|
||||
|
||||
block:
|
||||
echo "parseoptNoVal"
|
||||
# test NoVal mode with custom cmdline arguments
|
||||
var argv = "--left --debug:3 -l -r:2 --debug 2 --debug=1 -r1 -r=0 -lr4"
|
||||
var p = parseopt.initOptParser(argv,
|
||||
shortNoVal = {'l'}, longNoVal = @["left"])
|
||||
for kind, key, val in parseopt.getopt(p):
|
||||
echo "kind: ", kind, "\tkey:val -- ", key, ":", val
|
||||
block:
|
||||
echo "parseoptNoVal"
|
||||
# test NoVal mode with custom cmdline arguments
|
||||
var argv = "--left --debug:3 -l -r:2 --debug 2 --debug=1 -r1 -r=0 -lr4"
|
||||
var p = parseopt.initOptParser(argv,
|
||||
shortNoVal = {'l'}, longNoVal = @["left"])
|
||||
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
|
||||
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
|
||||
# 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
|
||||
|
||||
import osproc, os, strutils
|
||||
from stdtest/specialpaths import buildDir
|
||||
import "../.." / compiler/unittest_light
|
||||
|
||||
block: # fix #9951
|
||||
var p = parseopt.initOptParser(@["echo \"quoted\""])
|
||||
assertEquals parseopt.cmdLineRest(p), """'echo "quoted"'"""
|
||||
let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
|
||||
var p2 = parseopt.initOptParser(args)
|
||||
assertEquals parseopt.cmdLineRest(p2),
|
||||
"""a1b 'a2 b' '' 'a4"b' 'a5'"'"'b' 'a6\b' 'a7'"'"'b'"""
|
||||
doAssert "a5'b" == "a5\'b"
|
||||
|
||||
block: # fix #9842
|
||||
let exe = buildDir / "D20190112T145450".addFileExt(ExeExt)
|
||||
defer: removeFile exe
|
||||
let args = @["a1b", "a2 b", "", "a4\"b", "a5'b", r"a6\b", "a7\'b"]
|
||||
let cmd = "$# c -r --verbosity:0 -o:$# -d:testament_tparseopt $# $#" %
|
||||
[getCurrentCompilerExe(), exe, currentSourcePath(),
|
||||
args.quoteShellCommand]
|
||||
var ret = execCmdEx(cmd, options = {})
|
||||
if ret.exitCode != 0:
|
||||
# before bug fix, running cmd would show:
|
||||
# sh: -c: line 0: unexpected EOF while looking for matching `"'\n
|
||||
echo "exitCode: ", ret.exitCode, " cmd:", cmd
|
||||
doAssert false
|
||||
stripLineEnd(ret.output)
|
||||
assertEquals ret.output,
|
||||
"""
|
||||
@["a1b", "a2 b", "", "a4\"b", "a5\'b", "a6\\b", "a7\'b"]
|
||||
arg 0 ai.len:3 :{a1b}
|
||||
arg 1 ai.len:4 :{a2 b}
|
||||
arg 2 ai.len:0 :{}
|
||||
arg 3 ai.len:4 :{a4"b}
|
||||
arg 4 ai.len:4 :{a5'b}
|
||||
arg 5 ai.len:4 :{a6\b}
|
||||
arg 6 ai.len:4 :{a7'b}"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue