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 =
options.config.nimbleDir / "bin"
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:
proc parseCommand*(key: string, result: var Options) =
setNimScriptCommand(key)
result.action.typ = parseActionType(key)
initAction(result, key)
else:
proc parseArgument*(key: string, result: var Options) =
case result.action.typ
of actionNil:
assert false
@ -229,15 +224,16 @@ proc parseCmdLine*(): Options =
writeHelp()
else:
discard
of cmdLongOption, cmdShortOption:
proc parseFlag*(flag, val: string, result: var Options) =
case result.action.typ
of actionCompile:
if val == "":
result.action.compileOptions.add("--" & key)
result.action.compileOptions.add("--" & flag)
else:
result.action.compileOptions.add("--" & key & ":" & val)
result.action.compileOptions.add("--" & flag & ":" & val)
else:
case key.normalize()
case flag.normalize()
of "help", "h": writeHelp()
of "version", "v":
assert result.action.typ == actionNil
@ -248,7 +244,20 @@ proc parseCmdLine*(): Options =
of "nimbledir": result.config.nimbleDir = val # overrides option from file
of "installed", "i": result.queryInstalled = true
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
if result.action.typ == actionNil:
writeHelp()