Merge pull request #5233 from thaJeztah/nicer_errors

cli: FlagErrorFunc: don't print long usage output for invalid flags
This commit is contained in:
Sebastiaan van Stijn 2024-07-17 14:00:52 +02:00 committed by GitHub
commit 05a808166b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 6 deletions

View File

@ -92,12 +92,8 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
return nil
}
usage := ""
if cmd.HasSubCommands() {
usage = "\n\n" + cmd.UsageString()
}
return StatusError{
Status: fmt.Sprintf("%s\nSee '%s --help'.%s", err, cmd.CommandPath(), usage),
Status: fmt.Sprintf("%s\n\nUsage: %s\n\nRun '%s --help' for more information", err, cmd.UseLine(), cmd.CommandPath()),
StatusCode: 125,
}
}

View File

@ -78,6 +78,6 @@ func TestGlobalHelp(t *testing.T) {
})
assert.Assert(t, is.Equal(res2.Stdout(), ""))
assert.Assert(t, is.Contains(res2.Stderr(), "unknown flag: --badopt"))
assert.Assert(t, is.Contains(res2.Stderr(), "See 'docker --help"))
assert.Assert(t, is.Contains(res2.Stderr(), "Run 'docker --help' for more information"))
})
}