From 758ad1e83684a8fd03447c32bbbea763f5e400c5 Mon Sep 17 00:00:00 2001 From: Brian Goff Date: Sat, 20 Jun 2015 19:59:54 -0400 Subject: [PATCH] Sets a subcommand's parent cmd This allows the help output to show the correct/full command path to the subcommand. Signed-off-by: Brian Goff --- app_test.go | 30 ++++++++++++++++++++++++++++++ command.go | 19 ++++++++++++++++++- help.go | 6 +++--- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app_test.go b/app_test.go index b224d63..2d52e88 100644 --- a/app_test.go +++ b/app_test.go @@ -735,6 +735,36 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) { } } +func TestApp_Run_SubcommandFullPath(t *testing.T) { + app := cli.NewApp() + buf := new(bytes.Buffer) + app.Writer = buf + + subCmd := cli.Command{ + Name: "bar", + Usage: "does bar things", + } + cmd := cli.Command{ + Name: "foo", + Description: "foo commands", + Subcommands: []cli.Command{subCmd}, + } + app.Commands = []cli.Command{cmd} + + err := app.Run([]string{"command", "foo", "bar", "--help"}) + if err != nil { + t.Error(err) + } + + output := buf.String() + if !strings.Contains(output, "foo bar - does bar things") { + t.Errorf("expected full path to subcommand: %s", output) + } + if !strings.Contains(output, "command foo bar [arguments...]") { + t.Errorf("expected full path to subcommand: %s", output) + } +} + func TestApp_Run_Help(t *testing.T) { var helpArguments = [][]string{{"boom", "--help"}, {"boom", "-h"}, {"boom", "help"}} diff --git a/command.go b/command.go index 212f128..54617af 100644 --- a/command.go +++ b/command.go @@ -36,11 +36,21 @@ type Command struct { SkipFlagParsing bool // Boolean to hide built-in help command HideHelp bool + + commandNamePath []string +} + +// Returns the full name of the command. +// For subcommands this ensures that parent commands are part of the command path +func (c Command) FullName() string { + if c.commandNamePath == nil { + return c.Name + } + return strings.Join(c.commandNamePath, " ") } // Invokes the command given the context, parses ctx.Args() to generate command-specific flags func (c Command) Run(ctx *Context) error { - if len(c.Subcommands) > 0 || c.Before != nil || c.After != nil { return c.startApp(ctx) } @@ -179,5 +189,12 @@ func (c Command) startApp(ctx *Context) error { app.Action = helpSubcommand.Action } + var newCmds []Command + for _, cc := range app.Commands { + cc.commandNamePath = []string{c.Name, cc.Name} + newCmds = append(newCmds, cc) + } + app.Commands = newCmds + return app.RunAsSubcommand(ctx) } diff --git a/help.go b/help.go index e2cf82e..66ef2fb 100644 --- a/help.go +++ b/help.go @@ -20,7 +20,7 @@ USAGE: VERSION: {{.Version}} {{end}}{{if len .Authors}} -AUTHOR(S): +AUTHOR(S): {{range .Authors}}{{ . }}{{end}} {{end}}{{if .Commands}} COMMANDS: @@ -38,10 +38,10 @@ COPYRIGHT: // cli.go uses text/template to render templates. You can // render custom help text by setting this variable. var CommandHelpTemplate = `NAME: - {{.Name}} - {{.Usage}} + {{.FullName}} - {{.Usage}} USAGE: - command {{.Name}}{{if .Flags}} [command options]{{end}} [arguments...]{{if .Description}} + command {{.FullName}}{{if .Flags}} [command options]{{end}} [arguments...]{{if .Description}} DESCRIPTION: {{.Description}}{{end}}{{if .Flags}}