~siegfriedehret/brrr

4bc84c42aff26686f12663a59d43b644c3d69dff — Siegfried Ehret 2 years ago 942b6e6
🎁 refactor options
1 files changed, 19 insertions(+), 19 deletions(-)

M src/options.nim
M src/options.nim => src/options.nim +19 -19
@@ 5,31 5,39 @@ import std/[parseopt, strutils]

type
  ActionType* = enum
    actionNil, actionInstall
    actionNil, actionHelp, actionInstall, actionVersion
  Action* = object
    case typ*: ActionType
    of actionNil: nil
    of actionHelp: nil
    of actionInstall:
      packages*: seq[PkgTuple]
    of actionVersion: nil
  Options* = object
    action*: Action
    showHelp*: bool
    showVersion*: bool

proc parseActionType*(action: string): ActionType =
  case action.normalize()
  of "help":
    result = actionHelp
  of "install":
    result = actionInstall
  of "version":
    result = actionVersion
  else:
    result = actionNil
    result = actionHelp

proc parseCommand*(key: string, result: var Options) =
  echo "parseCommand ", key
  result.action = Action(typ: parseActionType(key))

proc parseArgument*(key: string, result: var Options) =
  echo "parseArgument ", key
  case result.action.typ
  of actionNil:
    assert false
  of actionHelp:
    discard
  of actionInstall:
    if '@' in key:
      let i = find(key, '@')


@@ 37,15 45,17 @@ proc parseArgument*(key: string, result: var Options) =
      result.action.packages.add((pkgName, pkgVer))
    else:
      result.action.packages.add((key, "*"))
  of actionVersion:
    discard

proc doAction*(options: var Options) =
  if options.showHelp:
    writeHelp()
  if options.showVersion:
    writeVersion()
  case options.action.typ
    of actionHelp:
      writeHelp()
    of actionInstall:
      install(options.action.packages)
    of actionVersion:
      writeVersion()
    of actionNil:
      writeHelp()



@@ 68,14 78,4 @@ proc parseCmdLine*(): Options =
        parseArgument(key, result)
    of cmdLongOption, cmdShortOption:
        discard
    of cmdEnd: assert(false) # cannot happen

  # result.config = parseConfig()

  if result.action.typ == actionNil and not result.showVersion:
    result.showHelp = true

  if result.action.typ != actionNil and result.showVersion:
    # We've got another command that should be handled. For example:
    # nimble run foobar -v
    result.showVersion = false
\ No newline at end of file
    of cmdEnd: assert(false) # cannot happen
\ No newline at end of file