Skip to content

Commit

Permalink
display help page on unknown commands (#176)
Browse files Browse the repository at this point in the history
* display help page on unknown commands

* point user to --help page instead of printing the whole page

* make grammar make sense

* linting
  • Loading branch information
stephchen-stripe committed Sep 23, 2019
1 parent 8c3ed9c commit 6c85da7
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/cmd/root.go
Expand Up @@ -5,6 +5,7 @@ package cmd
import (
"fmt"
"os"
"strings"

"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,7 +54,18 @@ func Execute() {
rootCmd.SetUsageTemplate(getUsageTemplate())
rootCmd.SetVersionTemplate(version.Template)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
if strings.Contains(err.Error(), "unknown command") {
suggestions := rootCmd.SuggestionsFor(os.Args[1])
suggStr := "\nS"
if len(suggestions) > 0 {
suggStr = fmt.Sprintf(" Did you mean \"%s\"?\nIf not, s", suggestions[0])
}
fmt.Println(fmt.Sprintf("Unknown command \"%s\" for \"%s\".%s"+
"ee \"stripe --help\" for a list of available commands.",
os.Args[1], rootCmd.CommandPath(), suggStr))
} else {
fmt.Println(err)
}
os.Exit(1)
}
}
Expand Down

0 comments on commit 6c85da7

Please sign in to comment.