Add support for sending expected output to stdout, and errors to stderr#81
Conversation
There was a problem hiding this comment.
I noticed the versions of Go being tested were past their EOL so I updated this list in a separate commit. I would be happy to revert this change if it is undesirable.
There was a problem hiding this comment.
It looks like CI is failing on 13 and 14 while trying to update dependencies, so I'll need to take another look.
There was a problem hiding this comment.
I believe the last 2 or 3 versions of Go will automatically add this directive to the go.mod file. Adding it will make it slightly more convenient for contributors so they do not have to revert the changes made by the toolchain every time they run tests.
I can change this to an older version if that is desirable.
There was a problem hiding this comment.
CI was failing on Go1.11 due to go modules being enabled, change to go1.11
06adfaf to
cc78ad0
Compare
So that requested help and version text can go to stdout, but errors due to invalid flags or commands can go to stderr.
cc78ad0 to
a8f5ed9
Compare
a8f5ed9 to
7d5906d
Compare
Update tests to match the fix in mitchellh/cli#71, which aligns MockUi with BasicUi and allows newlines in user input. We are not using the new ErrorWriter, added in mitchellh/cli#81, as it does not appear to interact correctly with panicwrap. All error output from CLI parsing will continue to appear on stdout, not stderr.

Problem
I encountered this problem when trying to view the Consul help with
consul agent --help. Any time a command has a lot of help text I send it to a pager (ex:consul agent --help | less). I will also quite frequently pipe the help output to grep to quickly find the help text for a flag that I know exists, but I may not remember exactly what it expects as a value.To my surprise, this did not work! I did some digging and found that all help text (and --version output), even when requested by the user, is always sent to
stderr.I looked to see how other users of this library have handled the problem and I found that
nomadsetsHelpWritertoos.Stdout. This fixes the problem, but introduces a new problem. If a user runs a command with an invalid flag the error message ends up onstdoutinstead ofstderr. This can be very confusing to users when they are trying to run a command with output piped somewhere else, because the error will be hidden.I believe it is best practice for a CLI to write requested help and version output to stdout, but to send that same output to stderr when it is being printed as a result of an invalid flag or subcommand.
Solution
This PR addresses the problem by introducing a second
Writer. I believe it is backwards compatible as the new writer is initialized to the value of the existingWriter.I've seen this problem solved this way in another cli library so I thought it may be applicable here as well.