Skip to content

Commit

Permalink
cli: write help to stderr for invalid CLI usage
Browse files Browse the repository at this point in the history
Rationale:
- When the user writes `--help`, the help message should go to stdout.
- When the user writes some incorrect options/arguments, the help
  message should go to stderr.
  • Loading branch information
ee7 committed Mar 11, 2021
1 parent ab14efe commit b7f808d
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,12 @@ proc showHelp(exitCode: range[0..255] = 0) =
const helpText = genHelpText()
let appName = extractFilename(getAppFilename())
let usage = "Usage:\n" &
&" {appName} [global-options] <command> [command-options]\n\n"
stdout.write usage
echo helpText
&" {appName} [global-options] <command> [command-options]\n"
let f = if exitCode == 0: stdout else: stderr
f.writeLine usage
f.writeLine helpText
if f == stdout:
f.flushFile()
quit(exitCode)

proc showVersion =
Expand Down

0 comments on commit b7f808d

Please sign in to comment.