Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard exit status code for usage errors #256

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func mustParse(config Config, dest ...interface{}) *Parser {
p, err := NewParser(config, dest...)
if err != nil {
fmt.Fprintln(config.Out, err)
config.Exit(-1)
config.Exit(2)
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func TestMustParseError(t *testing.T) {
os.Args = []string{"example"}
parser := MustParse(&args)
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")
}

Expand Down Expand Up @@ -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: "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 {
Expand Down Expand Up @@ -1571,7 +1571,7 @@ func TestMustParseInvalidParser(t *testing.T) {
}
parser := mustParse(Config{Out: &stdout, Exit: exit}, &args)
assert.Nil(t, parser)
assert.Equal(t, -1, exitCode)
assert.Equal(t, 2, exitCode)
}

func TestMustParsePrintsHelp(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
// the width of the left column
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) {
p.FailSubcommand(msg)
}

// FailSubcommand prints usage information for a specified subcommand to stderr,
// then exits with non-zero status. To write usage information for a top-level
// FailSubcommand prints usage information for a specified subcommand to p.Config.Out,
// 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
// information for a subcommand that is nested under another subcommand, provide
// a sequence of subcommand names starting with the top-level subcommand and so
Expand All @@ -27,7 +27,7 @@ func (p *Parser) FailSubcommand(msg string, subcommand ...string) error {
}

fmt.Fprintln(p.config.Out, "error:", msg)
p.config.Exit(-1)
p.config.Exit(2)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ error: something went wrong
p.Fail("something went wrong")

assert.Equal(t, expectedStdout[1:], stdout.String())
assert.Equal(t, -1, exitCode)
assert.Equal(t, 2, exitCode)
}

func TestFailSubcommand(t *testing.T) {
Expand All @@ -761,7 +761,7 @@ error: something went wrong
require.NoError(t, err)

assert.Equal(t, expectedStdout[1:], stdout.String())
assert.Equal(t, -1, exitCode)
assert.Equal(t, 2, exitCode)
}

type lengthOf struct {
Expand Down
Loading