Skip to content

Commit

Permalink
write usage errors to stderr (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonmezonur authored Apr 8, 2022
1 parent 0543ca8 commit df6b29f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#### Breaking changes
- Dropped inline comment feature for `run` command. Previously s5cmd supported a command with an inline comment like `ls s3://bucket/object.gz # inline comment`. ([#309](https://github.com/peak/s5cmd/issues/309))
- Changed homebrew installation command on macOS. Users can install s5cmd via `brew install peak/tap/s5cmd`. ([#356](https://github.com/peak/s5cmd/issues/356))
- Print usage errors to stderr instead of stdout and do not show help text on usage error. ([#399](https://github.com/peak/s5cmd/issues/399))

#### Features
- Added `sync` command to synchronize two given buckets, prefixes, or objects. ([#3](https://github.com/peak/s5cmd/issues/3))
Expand Down
10 changes: 10 additions & 0 deletions command/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"context"
"fmt"
"os"

cmpinstall "github.com/posener/complete/cmd/install"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -111,6 +112,15 @@ var app = &cli.App{
parallel.Close()
log.Close()
},
OnUsageError: func(c *cli.Context, err error, isSubcommand bool) error {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s %s\n", "Incorrect Usage:", err.Error())
_, _ = fmt.Fprintf(os.Stderr, "See 's5cmd --help' for usage\n")
return err
}

return nil
},
Action: func(c *cli.Context) error {
if c.Bool("install-completion") {
if cmpinstall.IsInstalled(appName) {
Expand Down
18 changes: 18 additions & 0 deletions e2e/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,21 @@ func TestAppUnknownCommand(t *testing.T) {
0: equals(`ERROR "unknown-command": command not found`),
})
}

func TestUsageError(t *testing.T) {
t.Parallel()

_, s5cmd, cleanup := setup(t)
defer cleanup()

cmd := s5cmd("--recursive", "ls")
result := icmd.RunCmd(cmd)

result.Assert(t, icmd.Expected{ExitCode: 1})

assertLines(t, result.Stdout(), map[int]compareFunc{})
assertLines(t, result.Stderr(), map[int]compareFunc{
0: equals("Incorrect Usage: flag provided but not defined: -recursive"),
1: equals("See 's5cmd --help' for usage"),
})
}

0 comments on commit df6b29f

Please sign in to comment.