Skip to content

Commit

Permalink
Fix(cli): Show error message for invalid value
Browse files Browse the repository at this point in the history
For example, the command:
  `canonical_data_syncer --verbosity=quiett`

Now produces:
  Error: invalid value for '--verbosity': 'quiett'

And shows the help message.

Previously, the program silently ignored the invalid value, and operated
with the default value.

Fixes: exercism#36
  • Loading branch information
ee7 committed Oct 9, 2020
1 parent e0bbcb6 commit a0b5268
Showing 1 changed file with 23 additions and 15 deletions.
38 changes: 23 additions & 15 deletions src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,27 @@ proc prefix(kind: CmdLineKind): string =
of cmdLongOption: "--"
of cmdArgument, cmdEnd: ""

proc parseMode(mode: string): Mode =
case mode.toLowerAscii
of "c", "choose": modeChoose
of "i", "include": modeIncludeMissing
of "e", "exclude": modeExcludeMissing
else: modeChoose

proc parseVerbosity(verbosity: string): Verbosity =
case verbosity.toLowerAscii
of "q", "quiet": verQuiet
of "n", "normal": verNormal
of "d", "detailed": verDetailed
else: verNormal
proc parseMode(kind: CmdLineKind, key: string, val: string): Mode =
case val.toLowerAscii
of "c", "choose":
result = modeChoose
of "i", "include":
result = modeIncludeMissing
of "e", "exclude":
result = modeExcludeMissing
else:
showError(&"invalid value for '{kind.prefix}{key}': '{val}'")

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

proc initArguments: Arguments =
result = Arguments(
Expand All @@ -92,9 +100,9 @@ proc parseArguments*: Arguments =
of CheckArgument.short, CheckArgument.long:
result.action = actCheck
of ModeArgument.short, ModeArgument.long:
result.mode = parseMode(val)
result.mode = parseMode(kind, key, val)
of VerbosityArgument.short, VerbosityArgument.long:
result.verbosity = parseVerbosity(val)
result.verbosity = parseVerbosity(kind, key, val)
of HelpArgument.short, HelpArgument.long:
showHelp()
of VersionArgument.short, VersionArgument.long:
Expand Down

0 comments on commit a0b5268

Please sign in to comment.