Skip to content

Commit e877758

Browse files
committed
Also print cmd usage string on "unknown command"
1 parent faa96be commit e877758

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

command/root.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package command
22

33
import (
4-
"errors"
54
"fmt"
65
"os"
76

@@ -19,13 +18,14 @@ func init() {
1918
// RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output")
2019

2120
RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
22-
cmd.Println(err)
23-
cmd.Println(cmd.UsageString())
24-
return SilentErr
21+
return FlagError{err}
2522
})
2623
}
2724

28-
var SilentErr = errors.New("SilentErr")
25+
// FlagError is the kind of error raised in flag processing
26+
type FlagError struct {
27+
error
28+
}
2929

3030
// RootCmd is the entry point of command-line execution
3131
var RootCmd = &cobra.Command{

main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/github/gh-cli/command"
89
)
910

1011
func main() {
11-
if err := command.RootCmd.Execute(); err != nil {
12-
if err != command.SilentErr {
13-
fmt.Fprintln(os.Stderr, err)
12+
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
13+
fmt.Fprintln(os.Stderr, err)
14+
_, isFlagError := err.(command.FlagError)
15+
if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") {
16+
fmt.Fprintln(os.Stderr, cmd.UsageString())
1417
}
1518
os.Exit(1)
1619
}

0 commit comments

Comments
 (0)