From 8f24dcbbf29bfc3cc8425bbec938dcfd26ca9650 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Sat, 27 Feb 2021 15:09:51 +0100 Subject: [PATCH 1/3] cli: make showError write to stderr This proc is currently called when: - the user passes invalid options/arguments to configlet - there was an error when trying to clone the problem-specifications repo --- src/cli.nim | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cli.nim b/src/cli.nim index 0c9bdfc1..a6222662 100644 --- a/src/cli.nim +++ b/src/cli.nim @@ -183,9 +183,9 @@ proc showVersion = quit(0) proc showError*(s: string) = - stdout.styledWrite(fgRed, "Error: ") - stdout.write(s) - stdout.write("\n\n") + stderr.styledWrite(fgRed, "Error: ") + stderr.write(s) + stderr.write("\n\n") showHelp(exitCode = 1) func formatOpt(kind: CmdLineKind, key: string, val = ""): string = From 2984d0cfd36648ba9eda9a112a6bef59bf5087f8 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Tue, 2 Mar 2021 20:36:08 +0100 Subject: [PATCH 2/3] cli: write help to stderr for invalid CLI usage 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. --- src/cli.nim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cli.nim b/src/cli.nim index a6222662..9493ba9d 100644 --- a/src/cli.nim +++ b/src/cli.nim @@ -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-options]\n\n" - stdout.write usage - echo helpText + &" {appName} [global-options] [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 = From c98dbe4fbafc7c750afbf85fcf3cec2a83fdf544 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Thu, 11 Mar 2021 08:12:11 +0100 Subject: [PATCH 3/3] tests: write progress message to stderr, not stdout We can argue that this message isn't really the "output" of the tests, but is just a temporary communication to the user that the program hasn't hung unexpectedly. `unittest` writes everything to stdout, and so with this commit, the user who compiles the tests and runs ``` $ ./tests/all_tests > /tmp/configlet_test_results.txt ``` will no longer see the message in that file - they see it in their terminal instead. --- tests/test_binary.nim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/test_binary.nim b/tests/test_binary.nim index 48093dd3..eabb8f12 100644 --- a/tests/test_binary.nim +++ b/tests/test_binary.nim @@ -271,13 +271,12 @@ proc main = const helpStart = &"Usage:\n {binaryName} [global-options] [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":