Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
main: Display more meaningful message on unknown command
Browse files Browse the repository at this point in the history
Introduced an explicity command-not-found handler to handle unknown
sub-commands. Previously, if the runtime was invoked with an unknown
command, it would error with:

    $ cc-runtime i-am-an-unknown-command
    No help topic for 'i-am-an-unknown-command'

It now errors with:

    $ cc-runtime i-am-an-unknown-command
    Invalid command "i-am-an-unknown-command"

Signed-off-by: James O. D. Hunt <[email protected]>
  • Loading branch information
jodh-intel committed Sep 7, 2017
1 parent 4018a0f commit f112d4a
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ var runtimeCommands = []cli.Command{
// parsing occurs.
var runtimeBeforeSubcommands = beforeSubcommands

// runtimeCommandNotFound is the function to handle an invalid sub-command.
var runtimeCommandNotFound = commandNotFound

// beforeSubcommands is the function to perform preliminary checks
// before command-line parsing occurs.
func beforeSubcommands(context *cli.Context) error {
Expand Down Expand Up @@ -173,10 +176,18 @@ func beforeSubcommands(context *cli.Context) error {
return nil
}

// function called when an invalid command is specified which causes the
// runtime to error.
func commandNotFound(c *cli.Context, command string) {
err := fmt.Errorf("Invalid command %q", command)
fatal(err)
}

func main() {
app := cli.NewApp()
app.Name = name
app.Usage = usage
app.CommandNotFound = runtimeCommandNotFound

cli.AppHelpTemplate = fmt.Sprintf(`%s%s`, cli.AppHelpTemplate, notes)

Expand Down

0 comments on commit f112d4a

Please sign in to comment.