Skip to content

Commit

Permalink
console: write more things to stderr
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 4, 2021
1 parent 4477f1b commit 5db7709
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 5 additions & 3 deletions src/cli.nim
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,11 @@ 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
f.flushFile()
quit(exitCode)

proc showVersion =
Expand Down
9 changes: 4 additions & 5 deletions tests/test_binary.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ template execAndCheck(expectedExitCode: int; body: untyped) {.dirty.} =
## `body` must have the same return type as `execCmdEx`.
let (outp, exitCode) = body
if exitCode != expectedExitCode:
echo outp
stderr.writeLine outp
fail()

func conciseDiff(s: string): string =
Expand Down Expand Up @@ -271,13 +271,12 @@ proc main =
const helpStart = &"Usage:\n {binaryName} [global-options] <command> [command-options]"

const cmd = "nimble --verbose build -d:release"
stdout.write(&"Running `{cmd}`... ")
stdout.flushFile()
stderr.write(&"Running `{cmd}`... ")
let (buildOutput, buildExitCode) = execCmdEx(cmd, workingDir = repoRootDir)
if buildExitCode == 0:
echo "success"
stderr.writeLine "success"
else:
echo "failure"
stderr.writeLine "failure"
raise newException(OSError, buildOutput)

suite "help as an argument":
Expand Down

0 comments on commit 5db7709

Please sign in to comment.