Add support for sending expected output to stdout, and errors to stderr by dnephin · Pull Request #81 · mitchellh/cli · GitHub
Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Add support for sending expected output to stdout, and errors to stderr#81

Merged
mitchellh merged 2 commits into
mitchellh:masterfrom
dnephin:help-output-stream
Mar 26, 2020
Merged

Add support for sending expected output to stdout, and errors to stderr#81
mitchellh merged 2 commits into
mitchellh:masterfrom
dnephin:help-output-stream

Conversation

@dnephin

@dnephin dnephin commented Mar 20, 2020

Copy link
Copy Markdown
Contributor

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 nomad sets HelpWriter to os.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 on stdout instead of stderr. 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.

$ git --help | wc -l
45
$ cat --help | wc -l
25
$ git bogus 2> /dev/null
$ git branch --bogus 2> /dev/null
$ cat --bogus 2> /dev/null

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 existing Writer.

I've seen this problem solved this way in another cli library so I thought it may be applicable here as well.

Comment thread .travis.yml

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like CI is failing on 13 and 14 while trying to update dependencies, so I'll need to take another look.

Comment thread go.mod Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI was failing on Go1.11 due to go modules being enabled, change to go1.11

@dnephin dnephin force-pushed the help-output-stream branch from 06adfaf to cc78ad0 Compare March 23, 2020 16:13
So that requested help and version text can go to stdout, but errors
due to invalid flags or commands can go to stderr.
@dnephin dnephin force-pushed the help-output-stream branch from cc78ad0 to a8f5ed9 Compare March 23, 2020 16:16
@dnephin dnephin force-pushed the help-output-stream branch from a8f5ed9 to 7d5906d Compare March 23, 2020 16:19
@mitchellh mitchellh merged commit c0e9b05 into mitchellh:master Mar 26, 2020
@dnephin dnephin deleted the help-output-stream branch March 26, 2020 15:59
alisdair added a commit to hashicorp/terraform that referenced this pull request Sep 10, 2020
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.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants