Skip to content

Commit

Permalink
Print usage instead of panic for no arguments (#24)
Browse files Browse the repository at this point in the history
* Print usage instead of panic for no arguments

Previously, if no arguments were passed to the
root command, a panic was triggered which in turn
bypassed the error handling strategy. Also the
provided message was not really helpful to the
user.

With this commit the usage is printed instead.
Additionally, an error is returned which is
correctly handled by the error handling strategy.

* Revert to old behavior

* Print usage if no subcommand was given

When no subcommand is passed to the application, only a short error text was printed to the console. Now the usage text is printed as well.

* Cleanup

* Add test for call without sub commands

* Remove explicit error discarding
  • Loading branch information
Jan authored Sep 26, 2020
1 parent 769c8c8 commit 8cff9ee
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,10 @@ func (c *SubCmd) parse(args []string) ([]string, error) {
// First argument is the command name.
args = args[1:]

/// If command has sub commands, find it and parse the sub command.
// If command has sub commands, find it and parse the sub command.
if len(c.sub) > 0 {
if len(args) == 0 {
c.Usage()
return nil, fmt.Errorf("must provide sub command")
}
name := args[0]
Expand Down
12 changes: 11 additions & 1 deletion cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"flag"
"io/ioutil"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -289,7 +290,7 @@ func TestCmd_valueCheck(t *testing.T) {

assert.NoError(t, root.ParseArgs("cmd", "-file", "cmd.go"))
assert.NoError(t, root.ParseArgs("cmd", "-file", "./cmd.go"))
assert.NoError(t, root.ParseArgs("cmd", "-file", "example/main.go"))
assert.NoError(t, root.ParseArgs("cmd", "-file", filepath.Join("example", "main.go")))
assert.Error(t, root.ParseArgs("cmd", "-file", "no-such-file.go"))
assert.Error(t, root.ParseArgs("cmd", "-file", "README.md"))

Expand Down Expand Up @@ -393,4 +394,13 @@ func TestCmd_failures(t *testing.T) {

assert.Panics(t, func() { root.Args("", "") })
})

t.Run("calling without sub commands fails with usage", func(t *testing.T) {
root := New(OptOutput(ioutil.Discard), OptErrorHandling(flag.ContinueOnError))
root.SubCommand("sub1", "")

if err := root.ParseArgs("cmd"); assert.Error(t, err) {
assert.EqualError(t, err, "must provide sub command")
}
})
}
4 changes: 2 additions & 2 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ var (
)

func main() {
root.ParseArgs()
root.Parse()

// Check which sub command was choses by the user.
// Check which sub command was chosen by the user.
switch {
case sub1.Parsed():
fmt.Printf("Called sub1 with flag: %s", *flag1)
Expand Down

0 comments on commit 8cff9ee

Please sign in to comment.