mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-29 17:34:43 +00:00
* Add ability for users to elide ':' or '=' when CLI authors pass a
non-empty partial symbol table. Behavior should be identical to the
old behavior if empty partial symbol tables are passed. "Partialness"
of the symbol table refers to the fact that one need only specify
option keys that are toggles/booleans/do not take arguments, hence
the "NoArg" suffixes in shortNoArg and longNoArg.
commandLineParams() returns seq[TaintedString], so use that consistently
in getopt() and initOptParser(seq[TaintedString]) dropping the taint at
the quoting stage just as with the paramStr() logic.
Fix capitalization inconsistency of cmdLongOption.
Export OptParser.cmd and OptParser.pos so that, at least *in principle*,
users of this API can handle "--" option processing termination or some
"git-like" sub-command stop word with a separate option sub-syntax.
{ Eg., ``case p.key of "": echo "trailing non-option args: ", p.cmd[p.pos..^1]``
or ``case p.kind of cmdArgument: if p.key == "mysubcmd": ...``. } Really,
searching for the last delimiter before p.pos is probably needed to frame
the trailing text..Not the nicest API, but still possible with effort.
* Make requested changes from string to seq[char]
(see https://github.com/nim-lang/Nim/pull/7297)
* Document new behavior and elaborate on some special cases.
* NoArg => NoVal to be less ambiguous/more clear.
* Add more documentation and an example snippet.
* Tweak language. Clarify still using ':'/'=' is ok.
* Add a test case for new NoVal behavior.
78 lines
2.5 KiB
Nim
78 lines
2.5 KiB
Nim
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
|
|
parseoptNoVal
|
|
kind: cmdLongOption key:val -- left:
|
|
kind: cmdLongOption key:val -- debug:3
|
|
kind: cmdShortOption key:val -- l:
|
|
kind: cmdShortOption key:val -- r:2
|
|
kind: cmdLongOption key:val -- debug:2
|
|
kind: cmdLongOption key:val -- debug:1
|
|
kind: cmdShortOption key:val -- r:1
|
|
kind: cmdShortOption key:val -- r:0
|
|
kind: cmdShortOption key:val -- l:
|
|
kind: cmdShortOption key:val -- r:4
|
|
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 "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
|
|
|
|
# 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
|