This commit is contained in:
Araq 2015-07-01 16:44:16 +02:00
commit 30829ffec4

View file

@ -42,25 +42,6 @@ type
{.deprecated: [TCmdLineKind: CmdLineKind, TOptParser: OptParser].}
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 = ""): OptParser =
## inits the option parser. If ``cmdline == ""``, the real command line
## (as provided by the ``OS`` module) is taken.
result.pos = 0
result.inShortState = false
if cmdline != "":
result.cmd = cmdline
else:
result.cmd = ""
for i in countup(1, paramCount()):
result.cmd = result.cmd & quoteIfContainsWhite(paramStr(i).string) & ' '
result.kind = cmdEnd
result.key = TaintedString""
result.val = TaintedString""
proc parseWord(s: string, i: int, w: var string,
delim: set[char] = {'\x09', ' ', '\0'}): int =
result = i
@ -75,6 +56,44 @@ proc parseWord(s: string, i: int, w: var string,
add(w, s[result])
inc(result)
when declared(os.paramCount):
proc quote(s: string): string =
if find(s, {' ', '\t'}) >= 0 and s[0] != '"':
if s[0] == '-':
result = newStringOfCap(s.len)
var i = parseWord(s, 0, result, {'\0', ' ', '\x09', ':', '='})
if s[i] in {':','='}:
result.add s[i]
inc i
result.add '"'
while i < s.len:
result.add s[i]
inc i
result.add '"'
else:
result = '"' & s & '"'
else:
result = s
# we cannot provide this for NimRtl creation on Posix, because we can't
# access the command line arguments then!
proc initOptParser*(cmdline = ""): OptParser =
## inits the option parser. If ``cmdline == ""``, the real command line
## (as provided by the ``OS`` module) is taken.
result.pos = 0
result.inShortState = false
if cmdline != "":
result.cmd = cmdline
else:
result.cmd = ""
for i in countup(1, paramCount()):
result.cmd.add quote(paramStr(i).string)
result.cmd.add ' '
result.kind = cmdEnd
result.key = TaintedString""
result.val = TaintedString""
proc handleShortOption(p: var OptParser) =
var i = p.pos
p.kind = cmdShortOption