Skip to content

Commit 6b76c2b

Browse files
NanXiaoringabout
authored andcommitted
Refactor initOptParser (nim-lang#19656)
Co-authored-by: flywind <[email protected]>
1 parent 18eeafa commit 6b76c2b

File tree

1 file changed

+5
-22
lines changed

1 file changed

+5
-22
lines changed

lib/pure/parseopt.nim

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ proc parseWord(s: string, i: int, w: var string,
192192
add(w, s[result])
193193
inc(result)
194194

195+
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
196+
longNoVal: seq[string] = @[];
197+
allowWhitespaceAfterColon = true): OptParser
198+
195199
proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
196200
longNoVal: seq[string] = @[];
197201
allowWhitespaceAfterColon = true): OptParser =
@@ -214,28 +218,7 @@ proc initOptParser*(cmdline = "", shortNoVal: set[char] = {},
214218
p = initOptParser("--left --debug:3 -l -r:2",
215219
shortNoVal = {'l'}, longNoVal = @["left"])
216220

217-
result.pos = 0
218-
result.idx = 0
219-
result.inShortState = false
220-
result.shortNoVal = shortNoVal
221-
result.longNoVal = longNoVal
222-
result.allowWhitespaceAfterColon = allowWhitespaceAfterColon
223-
if cmdline != "":
224-
result.cmds = parseCmdLine(cmdline)
225-
else:
226-
when declared(paramCount):
227-
result.cmds = newSeq[string](paramCount())
228-
for i in countup(1, paramCount()):
229-
result.cmds[i-1] = paramStr(i)
230-
else:
231-
# we cannot provide this for NimRtl creation on Posix, because we can't
232-
# access the command line arguments then!
233-
doAssert false, "empty command line given but" &
234-
" real command line is not accessible"
235-
236-
result.kind = cmdEnd
237-
result.key = ""
238-
result.val = ""
221+
initOptParser(parseCmdLine(cmdline), shortNoVal, longNoVal, allowWhitespaceAfterColon)
239222

240223
proc initOptParser*(cmdline: seq[string], shortNoVal: set[char] = {},
241224
longNoVal: seq[string] = @[];

0 commit comments

Comments
 (0)