Skip to content

Commit

Permalink
If no action is specified on the command or app, print the help docum…
Browse files Browse the repository at this point in the history
…entation

Rather than panic'ing or displaying an opaque error message about the
signature which is more confusing to the end user.

Fixes urfave#562
  • Loading branch information
jszwedko committed Nov 12, 2016
1 parent d86a009 commit 0113f56
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
4 changes: 4 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ func (a *App) Run(arguments []string) (err error) {
}
}

if a.Action == nil {
a.Action = helpCommand.Action
}

// Run default Action
err = HandleAction(a.Action, context)

Expand Down
43 changes: 43 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,49 @@ func ExampleApp_Run_commandHelp() {
// This is how we describe describeit the function
}

func ExampleApp_Run_noAction() {
app := App{}
app.Name = "greet"
app.Run([]string{"greet"})
// Output:
// NAME:
// greet
//
// USAGE:
// [global options] command [command options] [arguments...]
//
// COMMANDS:
// help, h Shows a list of commands or help for one command
//
// GLOBAL OPTIONS:
// --help, -h show help
// --version, -v print the version
}

func ExampleApp_Run_subcommandNoAction() {
app := App{}
app.Name = "greet"
app.Commands = []Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
},
}
app.Run([]string{"greet", "describeit"})
// Output:
// NAME:
// describeit - use it to see a description
//
// USAGE:
// describeit [arguments...]
//
// DESCRIPTION:
// This is how we describe describeit the function

}

func ExampleApp_Run_bashComplete() {
// set args for examples sake
os.Args = []string{"greet", "--generate-bash-completion"}
Expand Down
4 changes: 4 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func (c Command) Run(ctx *Context) (err error) {
}
}

if c.Action == nil {
c.Action = helpSubcommand.Action
}

context.Command = c
err = HandleAction(c.Action, context)

Expand Down
2 changes: 1 addition & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
var AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
{{.Name}}{{if .Usage}} - {{.Usage}}{{end}}
USAGE:
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} {{if .VisibleFlags}}[global options]{{end}}{{if .Commands}} command [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}{{if .Version}}{{if not .HideVersion}}
Expand Down

0 comments on commit 0113f56

Please sign in to comment.