Refactored command line parsing further.

This commit is contained in:
Dominik Picheta 2015-12-25 15:04:55 +00:00
commit 5eaee85825

View file

@ -191,17 +191,12 @@ proc getPkgsDir*(options: Options): string =
proc getBinDir*(options: Options): string = proc getBinDir*(options: Options): string =
options.config.nimbleDir / "bin" options.config.nimbleDir / "bin"
proc parseCmdLine*(): Options = proc parseCommand*(key: string, result: var Options) =
result.action.typ = actionNil
result.config = parseConfig()
for kind, key, val in getOpt():
case kind
of cmdArgument:
if result.action.typ == actionNil:
setNimScriptCommand(key) setNimScriptCommand(key)
result.action.typ = parseActionType(key) result.action.typ = parseActionType(key)
initAction(result, key) initAction(result, key)
else:
proc parseArgument*(key: string, result: var Options) =
case result.action.typ case result.action.typ
of actionNil: of actionNil:
assert false assert false
@ -229,15 +224,16 @@ proc parseCmdLine*(): Options =
writeHelp() writeHelp()
else: else:
discard discard
of cmdLongOption, cmdShortOption:
proc parseFlag*(flag, val: string, result: var Options) =
case result.action.typ case result.action.typ
of actionCompile: of actionCompile:
if val == "": if val == "":
result.action.compileOptions.add("--" & key) result.action.compileOptions.add("--" & flag)
else: else:
result.action.compileOptions.add("--" & key & ":" & val) result.action.compileOptions.add("--" & flag & ":" & val)
else: else:
case key.normalize() case flag.normalize()
of "help", "h": writeHelp() of "help", "h": writeHelp()
of "version", "v": of "version", "v":
assert result.action.typ == actionNil assert result.action.typ == actionNil
@ -248,7 +244,20 @@ proc parseCmdLine*(): Options =
of "nimbledir": result.config.nimbleDir = val # overrides option from file of "nimbledir": result.config.nimbleDir = val # overrides option from file
of "installed", "i": result.queryInstalled = true of "installed", "i": result.queryInstalled = true
else: else:
raise newException(NimbleError, "Unknown option: --" & key) raise newException(NimbleError, "Unknown option: --" & flag)
proc parseCmdLine*(): Options =
result.action.typ = actionNil
result.config = parseConfig()
for kind, key, val in getOpt():
case kind
of cmdArgument:
if result.action.typ == actionNil:
parseCommand(key, result)
else:
parseArgument(key, result)
of cmdLongOption, cmdShortOption:
parseFlag(key, val, result)
of cmdEnd: assert(false) # cannot happen of cmdEnd: assert(false) # cannot happen
if result.action.typ == actionNil: if result.action.typ == actionNil:
writeHelp() writeHelp()