Skip to content

Commit

Permalink
Refactor(cli): Reduce duplication in value parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ee7 committed Oct 27, 2020
1 parent b8d2d12 commit 5dd0b64
Showing 1 changed file with 19 additions and 29 deletions.
48 changes: 19 additions & 29 deletions src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ func list(opt: Opt): string =
else:
&"-{short[opt]}, --{$opt}"

func first(s: string): string =
# Returns the first character of `s` as a string.
result = newString(1)
result[0] = s[0]

func allowedValues(T: typedesc[enum]): string =
## Returns a string that describes the allowed values for an enum `T`.
result = "Allowed values: "
Expand Down Expand Up @@ -100,28 +95,6 @@ proc prefix(kind: CmdLineKind): string =
of cmdLongOption: "--"
of cmdArgument, cmdEnd, cmdError: ""

proc parseMode(kind: CmdLineKind, key: string, val: string): Mode =
case val.toLowerAscii
of first($modeChoose), $modeChoose:
result = modeChoose
of first($modeInclude), $modeInclude:
result = modeInclude
of first($modeExclude), $modeExclude:
result = modeExclude
else:
showError(&"invalid value for '{kind.prefix}{key}': '{val}'")

proc parseVerbosity(kind: CmdLineKind, key: string, val: string): Verbosity =
case val.toLowerAscii
of first($verQuiet), $verQuiet:
result = verQuiet
of first($verNormal), $verNormal:
result = verNormal
of first($verDetailed), $verDetailed:
result = verDetailed
else:
showError(&"invalid value for '{kind.prefix}{key}': '{val}'")

proc initConf: Conf =
result = Conf(
action: actSync,
Expand Down Expand Up @@ -164,6 +137,23 @@ proc parseOption(kind: CmdLineKind, key: string, val: string): Opt =
except ValueError:
showError(&"invalid option: '{prefix(kind)}{key}'")

proc parseVal[T: enum](kind: CmdLineKind, key: string, val: string): T =
## Parses `val` as a value of the enum `T`, using a case-insensitive
## comparsion.
##
## Exits with an error if `key` cannot be parsed as a value of `T`.
var valNormalized = toLowerAscii(val)
# Convert a valid single-letter abbreviation to the string value of the enum.
if valNormalized.len == 1:
for e in T:
if valNormalized[0] == ($e)[0]:
valNormalized = $e
break
try:
result = parseEnum[T](valNormalized)
except ValueError:
showError(&"invalid value for '{prefix(kind)}{key}': '{val}'")

proc processCmdLine*: Conf =
result = initConf()

Expand All @@ -182,9 +172,9 @@ proc processCmdLine*: Conf =
of optCheck:
result.action = actCheck
of optMode:
result.mode = parseMode(kind, key, val)
result.mode = parseVal[Mode](kind, key, val)
of optVerbosity:
result.verbosity = parseVerbosity(kind, key, val)
result.verbosity = parseVal[Verbosity](kind, key, val)
of optProbSpecsDir:
result.probSpecsDir = some(val)
of optOffline:
Expand Down

0 comments on commit 5dd0b64

Please sign in to comment.