summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorNan Xiao <nan@chinadtrace.org>2022-09-28 03:00:57 +0800
committerGitHub <noreply@github.com>2022-09-27 15:00:57 -0400
commitfd257a9a0f363bd31cb0702c729193ee7f8fe8ef (patch)
tree7df631817e2372fb0207523f2f508c2176b64086
parent80e739f2bf5a3e40df7000566a9cbb9cfab29ac4 (diff)
downloadNim-fd257a9a0f363bd31cb0702c729193ee7f8fe8ef.tar.gz
Refactor initOptParser (#19656)
Co-authored-by: flywind <xzsflywind@gmail.com>
-rw-r--r--lib/pure/parseopt.nim27
1 files changed, 5 insertions, 22 deletions
diff --git a/lib/pure/parseopt.nim b/lib/pure/parseopt.nim
index fef354733..f8d03e092 100644
--- a/lib/pure/parseopt.nim
+++ b/lib/pure/parseopt.nim
@@ -192,6 +192,10 @@ proc parseWord(s: string, i: int, w: var string,
       add(w, s[result])
       inc(result)
 
+proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
+                    longNoVal: seq[string] = @[];
+                    allowWhitespaceAfterColon = true): OptParser
+
 proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
                     longNoVal: seq[string] = @[];
                     allowWhitespaceAfterColon = true): OptParser =
@@ -214,28 +218,7 @@ proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
     p = initOptParser("--left --debug:3 -l -r:2",
                       shortNoVal = {'l'}, longNoVal = @["left"])
 
-  result.pos = 0
-  result.idx = 0
-  result.inShortState = false
-  result.shortNoVal = shortNoVal
-  result.longNoVal = longNoVal
-  result.allowWhitespaceAfterColon = allowWhitespaceAfterColon
-  if cmdline != "":
-    result.cmds = parseCmdLine(cmdline)
-  else:
-    when declared(paramCount):
-      result.cmds = newSeq[string](paramCount())
-      for i in countup(1, paramCount()):
-        result.cmds[i-1] = paramStr(i)
-    else:
-      # we cannot provide this for NimRtl creation on Posix, because we can't
-      # access the command line arguments then!
-      doAssert false, "empty command line given but" &
-        " real command line is not accessible"
-
-  result.kind = cmdEnd
-  result.key = ""
-  result.val = ""
+  initOptParser(parseCmdLine(cmdline), shortNoVal, longNoVal, allowWhitespaceAfterColon)
 
 proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
                     longNoVal: seq[string] = @[];