From a0b5268de235ef27b4b57614413aff1873a662a5 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Fri, 9 Oct 2020 14:15:11 +0200 Subject: [PATCH] Fix(cli): Show error message for invalid value 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: #36 --- src/cli.nim | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/cli.nim b/src/cli.nim index 8f387093..de559d76 100644 --- a/src/cli.nim +++ b/src/cli.nim @@ -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( @@ -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: