Skip to content

Commit

Permalink
cmd: Exit with status 0 when -h is used (#87)
Browse files Browse the repository at this point in the history
This is a pretty standard CLI behavior:
`cmd -h` should print the help message and exit with status 0.
This is the default behavior in the flag package under ExitOnError mode.
We neglected to replicate that in ContinueOnError mode.
  • Loading branch information
abhinav authored Feb 11, 2024
1 parent 30323a7 commit 57495cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Fixed
- cmd/errtrace: Don't exit with a non-zero status when `-h` is used.

## v0.3.0 - 2023-12-22

This release adds support to the CLI for using Go package patterns like `./...`
Expand Down
4 changes: 4 additions & 0 deletions cmd/errtrace/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"go/ast"
Expand Down Expand Up @@ -184,6 +185,9 @@ func (cmd *mainCmd) Run(args []string) (exitCode int) {

var p mainParams
if err := p.Parse(cmd.Stderr, args); err != nil {
if errors.Is(err, flag.ErrHelp) {
return 0
}
cmd.log.Printf("errtrace: %+v", err)
return 1
}
Expand Down
11 changes: 11 additions & 0 deletions cmd/errtrace/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ import (
"braces.dev/errtrace/internal/diff"
)

func TestErrHelp(t *testing.T) {
exitCode := (&mainCmd{
Stdin: strings.NewReader(""),
Stdout: testWriter{t},
Stderr: testWriter{t},
}).Run([]string{"-h"})
if want := 0; exitCode != want {
t.Errorf("exit code = %d, want %d", exitCode, want)
}
}

// TestGolden runs errtrace on all .go files inside testdata/golden,
// and compares the output to the corresponding .golden file.
// Files must match exactly.
Expand Down

0 comments on commit 57495cc

Please sign in to comment.