Use standard exit status code for usage errors

* The stdlib `flags` package and most command line utilities use status code `2`.
This commit is contained in:
Hugo Hromic 2024-06-29 15:42:05 +01:00
parent bee5cf5d7c
commit a7c40c36a3
No known key found for this signature in database
GPG Key ID: 5CAFA8074D3F443B
4 changed files with 10 additions and 10 deletions

View File

@ -90,7 +90,7 @@ func mustParse(config Config, dest ...interface{}) *Parser {
p, err := NewParser(config, dest...) p, err := NewParser(config, dest...)
if err != nil { if err != nil {
fmt.Fprintln(config.Out, err) fmt.Fprintln(config.Out, err)
config.Exit(-1) config.Exit(2)
return nil return nil
} }

View File

@ -703,7 +703,7 @@ func TestMustParseError(t *testing.T) {
os.Args = []string{"example"} os.Args = []string{"example"}
parser := MustParse(&args) parser := MustParse(&args)
assert.Nil(t, parser) assert.Nil(t, parser)
assert.Equal(t, -1, exitCode) assert.Equal(t, 2, exitCode)
assert.Contains(t, stdout.String(), "default values are not supported for slice or map fields") assert.Contains(t, stdout.String(), "default values are not supported for slice or map fields")
} }
@ -921,7 +921,7 @@ func TestParserMustParse(t *testing.T) {
}{ }{
{name: "help", args: struct{}{}, cmdLine: []string{"--help"}, code: 0, output: "display this help and exit"}, {name: "help", args: struct{}{}, cmdLine: []string{"--help"}, code: 0, output: "display this help and exit"},
{name: "version", args: versioned{}, cmdLine: []string{"--version"}, code: 0, output: "example 3.2.1"}, {name: "version", args: versioned{}, cmdLine: []string{"--version"}, code: 0, output: "example 3.2.1"},
{name: "invalid", args: struct{}{}, cmdLine: []string{"invalid"}, code: -1, output: ""}, {name: "invalid", args: struct{}{}, cmdLine: []string{"invalid"}, code: 2, output: ""},
} }
for _, tt := range tests { for _, tt := range tests {
@ -1571,7 +1571,7 @@ func TestMustParseInvalidParser(t *testing.T) {
} }
parser := mustParse(Config{Out: &stdout, Exit: exit}, &args) parser := mustParse(Config{Out: &stdout, Exit: exit}, &args)
assert.Nil(t, parser) assert.Nil(t, parser)
assert.Equal(t, -1, exitCode) assert.Equal(t, 2, exitCode)
} }
func TestMustParsePrintsHelp(t *testing.T) { func TestMustParsePrintsHelp(t *testing.T) {

View File

@ -9,13 +9,13 @@ import (
// the width of the left column // the width of the left column
const colWidth = 25 const colWidth = 25
// Fail prints usage information to stderr and exits with non-zero status // Fail prints usage information to p.Config.Out and exits with status code 2.
func (p *Parser) Fail(msg string) { func (p *Parser) Fail(msg string) {
p.FailSubcommand(msg) p.FailSubcommand(msg)
} }
// FailSubcommand prints usage information for a specified subcommand to stderr, // FailSubcommand prints usage information for a specified subcommand to p.Config.Out,
// then exits with non-zero status. To write usage information for a top-level // then exits with status code 2. To write usage information for a top-level
// subcommand, provide just the name of that subcommand. To write usage // subcommand, provide just the name of that subcommand. To write usage
// information for a subcommand that is nested under another subcommand, provide // information for a subcommand that is nested under another subcommand, provide
// a sequence of subcommand names starting with the top-level subcommand and so // a sequence of subcommand names starting with the top-level subcommand and so
@ -27,7 +27,7 @@ func (p *Parser) FailSubcommand(msg string, subcommand ...string) error {
} }
fmt.Fprintln(p.config.Out, "error:", msg) fmt.Fprintln(p.config.Out, "error:", msg)
p.config.Exit(-1) p.config.Exit(2)
return nil return nil
} }

View File

@ -738,7 +738,7 @@ error: something went wrong
p.Fail("something went wrong") p.Fail("something went wrong")
assert.Equal(t, expectedStdout[1:], stdout.String()) assert.Equal(t, expectedStdout[1:], stdout.String())
assert.Equal(t, -1, exitCode) assert.Equal(t, 2, exitCode)
} }
func TestFailSubcommand(t *testing.T) { func TestFailSubcommand(t *testing.T) {
@ -761,7 +761,7 @@ error: something went wrong
require.NoError(t, err) require.NoError(t, err)
assert.Equal(t, expectedStdout[1:], stdout.String()) assert.Equal(t, expectedStdout[1:], stdout.String())
assert.Equal(t, -1, exitCode) assert.Equal(t, 2, exitCode)
} }
type lengthOf struct { type lengthOf struct {