Skip to content

Commit

Permalink
cli: improve argument validation output
Browse files Browse the repository at this point in the history
Improve the output for these validation errors:

- Removes the short command description from the output. This information
  does not provide much useful help, and distracts from the error message.
- Reduces punctuation, and
- Prefixes the error message with the binary / root-command name
  (usually `docker:`) to be consistent with other similar errors.
- Adds an empty line between the error-message and the "call to action"
  (`Run 'docker volume --help'...` in the example below). This helps
  separating the error message and "usage" from the call-to-action.

Before this patch:

    $ docker volume ls one two three
    "docker volume ls" accepts no arguments.
    See 'docker volume ls --help'.

    Usage:  docker volume ls [OPTIONS]

    List volumes

    $ docker volume create one two three
    "docker volume create" requires at most 1 argument.
    See 'docker volume create --help'.

    Usage:  docker volume create [OPTIONS] [VOLUME]

    Create a volume

With this patch:

    $ docker volume ls one two three
    docker: 'docker volume ls' accepts no arguments

    Usage:  docker volume ls [OPTIONS]

    Run 'docker volume ls --help' for more information

    $ docker voludocker volume create one two three
    docker: 'docker volume create' requires at most 1 argument

    Usage:  docker volume create [OPTIONS] [VOLUME]

    SRun 'docker volume create --help' for more information

Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
thaJeztah committed Jul 5, 2024
1 parent a6e96c7 commit 346dac7
Show file tree
Hide file tree
Showing 37 changed files with 71 additions and 75 deletions.
4 changes: 2 additions & 2 deletions cli/command/checkpoint/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func TestCheckpointCreateErrors(t *testing.T) {
}{
{
args: []string{"too-few-arguments"},
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"too", "many", "arguments"},
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"foo", "bar"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/checkpoint/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func TestCheckpointListErrors(t *testing.T) {
}{
{
args: []string{},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"too", "many", "arguments"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"foo"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/checkpoint/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestCheckpointRemoveErrors(t *testing.T) {
}{
{
args: []string{"too-few-arguments"},
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"too", "many", "arguments"},
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"foo", "bar"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/config/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ func TestConfigCreateErrors(t *testing.T) {
}{
{
args: []string{"too_few"},
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"too", "many", "arguments"},
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"name", filepath.Join("testdata", configDataFile)},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/config/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestConfigRemoveErrors(t *testing.T) {
}{
{
args: []string{},
expectedError: "requires at least 1 argument.",
expectedError: "requires at least 1 argument",
},
{
args: []string{"foo"},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewHistoryCommandErrors(t *testing.T) {
{
name: "wrong-args",
args: []string{},
expectedError: "requires exactly 1 argument.",
expectedError: "requires 1 argument.",
},
{
name: "client-error",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestNewImportCommandErrors(t *testing.T) {
{
name: "wrong-args",
args: []string{},
expectedError: "requires at least 1 argument.",
expectedError: "requires at least 1 argument",
},
{
name: "import-failed",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNewInspectCommandErrors(t *testing.T) {
{
name: "wrong-args",
args: []string{},
expectedError: "requires at least 1 argument.",
expectedError: "requires at least 1 argument",
},
}
for _, tc := range testCases {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNewImagesCommandErrors(t *testing.T) {
{
name: "wrong-args",
args: []string{"arg1", "arg2"},
expectedError: "requires at most 1 argument.",
expectedError: "requires at most 1 argument",
},
{
name: "failed-list",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestNewLoadCommandErrors(t *testing.T) {
{
name: "wrong-args",
args: []string{"arg"},
expectedError: "accepts no arguments.",
expectedError: "accepts no arguments",
},
{
name: "input-to-terminal",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestNewPruneCommandErrors(t *testing.T) {
{
name: "wrong-args",
args: []string{"something"},
expectedError: "accepts no arguments.",
expectedError: "accepts no arguments",
},
{
name: "prune-error",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewPullCommandErrors(t *testing.T) {
}{
{
name: "wrong-args",
expectedError: "requires exactly 1 argument.",
expectedError: "requires 1 argument.",
args: []string{},
},
{
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNewPushCommandErrors(t *testing.T) {
{
name: "wrong-args",
args: []string{},
expectedError: "requires exactly 1 argument.",
expectedError: "requires 1 argument.",
},
{
name: "invalid-name",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestNewRemoveCommandErrors(t *testing.T) {
}{
{
name: "wrong args",
expectedError: "requires at least 1 argument.",
expectedError: "requires at least 1 argument",
},
{
name: "ImageRemove fail with force option",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestNewSaveCommandErrors(t *testing.T) {
{
name: "wrong args",
args: []string{},
expectedError: "requires at least 1 argument.",
expectedError: "requires at least 1 argument",
},
{
name: "output to terminal",
Expand Down
2 changes: 1 addition & 1 deletion cli/command/image/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestCliNewTagCommandErrors(t *testing.T) {
{"image1"},
{"image1", "image2", "image3"},
}
expectedError := "\"tag\" requires exactly 2 arguments."
expectedError := "\"tag\" requires 2 arguments"
for _, args := range testCases {
cmd := NewTagCommand(test.NewFakeCli(&fakeClient{}))
cmd.SetArgs(args)
Expand Down
2 changes: 1 addition & 1 deletion cli/command/manifest/annotate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestManifestAnnotateError(t *testing.T) {
}{
{
args: []string{"too-few-arguments"},
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"th!si'sa/fa!ke/li$t/name", "example.com/alpine:3.0"},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/manifest/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestManifestPushErrors(t *testing.T) {
}{
{
args: []string{"one-arg", "extra-arg"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"th!si'sa/fa!ke/li$t/-name"},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestNetworkConnectErrors(t *testing.T) {
expectedError string
}{
{
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"toto", "titi"},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNetworkCreateErrors(t *testing.T) {
expectedError string
}{
{
expectedError: "exactly 1 argument",
expectedError: "1 argument",
},
{
args: []string{"toto"},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/network/disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestNetworkDisconnectErrors(t *testing.T) {
expectedError string
}{
{
expectedError: "requires exactly 2 arguments",
expectedError: "requires 2 arguments",
},
{
args: []string{"toto", "titi"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/node/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func TestNodeUpdateErrors(t *testing.T) {
expectedError string
}{
{
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"node1", "node2"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"nodeID"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/plugin/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ func TestPluginDisableErrors(t *testing.T) {
}{
{
args: []string{},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"too", "many", "arguments"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"plugin-foo"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/plugin/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func TestPluginEnableErrors(t *testing.T) {
}{
{
args: []string{},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"too-many", "arguments"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"plugin-foo"},
Expand Down
2 changes: 1 addition & 1 deletion cli/command/secret/remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestSecretRemoveErrors(t *testing.T) {
}{
{
args: []string{},
expectedError: "requires at least 1 argument.",
expectedError: "requires at least 1 argument",
},
{
args: []string{"foo"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/service/rollback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ func TestRollbackWithErrors(t *testing.T) {
}{
{
name: "not-enough-args",
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "too-many-args",
args: []string{"service-id-1", "service-id-2"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "service-does-not-exists",
Expand Down
4 changes: 2 additions & 2 deletions cli/command/stack/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func TestStackPsErrors(t *testing.T) {
}{
{
args: []string{},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"foo", "bar"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
args: []string{"foo"},
Expand Down
4 changes: 2 additions & 2 deletions cli/command/swarm/join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func TestSwarmJoinErrors(t *testing.T) {
}{
{
name: "not-enough-args",
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "too-many-args",
args: []string{"remote1", "remote2"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "join-failed",
Expand Down
4 changes: 2 additions & 2 deletions cli/command/swarm/join_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ func TestSwarmJoinTokenErrors(t *testing.T) {
}{
{
name: "not-enough-args",
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "too-many-args",
args: []string{"worker", "manager"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "invalid-args",
Expand Down
4 changes: 2 additions & 2 deletions cli/command/trust/key_generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestTrustKeyGenerateErrors(t *testing.T) {
}{
{
name: "not-enough-args",
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "too-many-args",
args: []string{"key-1", "key-2"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
}

Expand Down
5 changes: 3 additions & 2 deletions cli/command/trust/key_load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ func TestTrustKeyLoadErrors(t *testing.T) {
}{
{
name: "not-enough-args",
expectedError: "exactly 1 argument",
expectedError: "1 argument",
args: []string{},
expectedOutput: "",
},
{
name: "too-many-args",
args: []string{"iamnotakey", "alsonotakey"},
expectedError: "exactly 1 argument",
expectedError: "1 argument",
expectedOutput: "",
},
{
Expand Down
4 changes: 2 additions & 2 deletions cli/command/trust/revoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func TestTrustRevokeCommandErrors(t *testing.T) {
}{
{
name: "not-enough-args",
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "too-many-args",
args: []string{"remote1", "remote2"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "sha-reference",
Expand Down
4 changes: 2 additions & 2 deletions cli/command/trust/sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func TestTrustSignCommandErrors(t *testing.T) {
}{
{
name: "not-enough-args",
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "too-many-args",
args: []string{"image", "tag"},
expectedError: "requires exactly 1 argument",
expectedError: "requires 1 argument",
},
{
name: "sha-reference",
Expand Down
Loading

0 comments on commit 346dac7

Please sign in to comment.