Skip to content

Commit df6b29f

Browse files
authored
write usage errors to stderr (#431)
1 parent 0543ca8 commit df6b29f

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#### Breaking changes
66
- 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))
77
- Changed homebrew installation command on macOS. Users can install s5cmd via `brew install peak/tap/s5cmd`. ([#356](https://github.com/peak/s5cmd/issues/356))
8+
- 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))
89

910
#### Features
1011
- Added `sync` command to synchronize two given buckets, prefixes, or objects. ([#3](https://github.com/peak/s5cmd/issues/3))

command/app.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"context"
55
"fmt"
6+
"os"
67

78
cmpinstall "github.com/posener/complete/cmd/install"
89
"github.com/urfave/cli/v2"
@@ -111,6 +112,15 @@ var app = &cli.App{
111112
parallel.Close()
112113
log.Close()
113114
},
115+
OnUsageError: func(c *cli.Context, err error, isSubcommand bool) error {
116+
if err != nil {
117+
_, _ = fmt.Fprintf(os.Stderr, "%s %s\n", "Incorrect Usage:", err.Error())
118+
_, _ = fmt.Fprintf(os.Stderr, "See 's5cmd --help' for usage\n")
119+
return err
120+
}
121+
122+
return nil
123+
},
114124
Action: func(c *cli.Context) error {
115125
if c.Bool("install-completion") {
116126
if cmpinstall.IsInstalled(appName) {

e2e/app_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,21 @@ func TestAppUnknownCommand(t *testing.T) {
101101
0: equals(`ERROR "unknown-command": command not found`),
102102
})
103103
}
104+
105+
func TestUsageError(t *testing.T) {
106+
t.Parallel()
107+
108+
_, s5cmd, cleanup := setup(t)
109+
defer cleanup()
110+
111+
cmd := s5cmd("--recursive", "ls")
112+
result := icmd.RunCmd(cmd)
113+
114+
result.Assert(t, icmd.Expected{ExitCode: 1})
115+
116+
assertLines(t, result.Stdout(), map[int]compareFunc{})
117+
assertLines(t, result.Stderr(), map[int]compareFunc{
118+
0: equals("Incorrect Usage: flag provided but not defined: -recursive"),
119+
1: equals("See 's5cmd --help' for usage"),
120+
})
121+
}

0 commit comments

Comments
 (0)