docker / cli

The Docker CLI
Apache License 2.0
4.75k stars 1.88k forks source link

cli: FlagErrorFunc: don't print long usage output for invalid flags #5233

Open thaJeztah opened 6 days ago

thaJeztah commented 6 days ago

cli: FlagErrorFunc: don't print usage output for invalid flags

When trying to use an invalid flag, the CLI currently prints the a short error message, instructions to use the --help flag to learn about the correct usage, followed by the command's usage output.

While this is a common convention, and may have been a nice gesture when docker was still young and only had a few commands and options ("you did something wrong, but here's an overview of what you can use"), that's no longer the case, and many commands have a very long output.

The result of this is that the error message, which is the relevant information in this case - "You mis-typed something" - is lost in the output, and hard to find (sometimes even requiring scrolling back).

The output is also confusing, because it looks like something ran successfully (most of the output is not about the error!).

Even further; the suggested resolution (try --help to see the correct options) is rather redundant, because running teh command with --help produces exactly the same output as was just showh, baring the error message. As a fun fact, due to the usage output being printed, the output even contains not one, but two "call to actions";

In short; the output is too verbose, confusing, and doesn't provide a good UX. Let's reduce the output produced so that the focus is on the important information.

This patch:

Before this patch:

docker volume --no-such-flag
unknown flag: --no-such-flag
See 'docker volume --help'.

Usage:  docker volume COMMAND

Manage volumes

Commands:
  create      Create a volume
  inspect     Display detailed information on one or more volumes
  ls          List volumes
  prune       Remove unused local volumes
  rm          Remove one or more volumes
  update      Update a volume (cluster volumes only)

Run 'docker volume COMMAND --help' for more information on a command.

With this patch:

docker volume --no-such-flag
docker: unknown flag: --no-such-flag

Usage:  docker volume COMMAND

Run 'docker volume --help' for more information

- Description for the changelog

Improve error-output for invalid flags on the command-line.

- A picture of a cute animal (not mandatory but encouraged)

codecov-commenter commented 6 days ago

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 61.48%. Comparing base (cad08ff) to head (5f0f0dd).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #5233 +/- ## ======================================= Coverage 61.47% 61.48% ======================================= Files 298 298 Lines 20815 20812 -3 ======================================= Hits 12797 12797 + Misses 7106 7103 -3 Partials 912 912 ```