Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Name for Application from inside Subcommands #974

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ var (
type App struct {
// The name of the program. Defaults to path.Base(os.Args[0])
Name string
// The name of the program. Defaults to path.base(os.Args[0])
ProgramName string
// The name of the current running command. In case of a sub-command, this variable would also contain
// the name of its parents.
CommandName string
// Full name of command for help, defaults to Name
HelpName string
// Description of the program.
Expand Down Expand Up @@ -139,6 +144,10 @@ func (a *App) Setup() {
a.Name = filepath.Base(os.Args[0])
}

if a.Name != "" && a.ProgramName == "" {
a.ProgramName = a.Name
}

if a.HelpName == "" {
a.HelpName = a.Name
}
Expand Down
10 changes: 8 additions & 2 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func (c *Command) Run(ctx *Context) (err error) {
return c.startApp(ctx)
}

ctx.App.CommandName = fmt.Sprintf("%s %s", ctx.App.CommandName, c.Name)

if !c.HideHelp && HelpFlag != nil {
// append help to flags
c.appendFlag(HelpFlag)
Expand Down Expand Up @@ -216,10 +218,14 @@ func (c *Command) HasName(name string) bool {

func (c *Command) startApp(ctx *Context) error {
app := &App{
Metadata: ctx.App.Metadata,
Name: fmt.Sprintf("%s %s", ctx.App.Name, c.Name),
Metadata: ctx.App.Metadata,
Name: fmt.Sprintf("%s %s", ctx.App.Name, c.Name),
CommandName: fmt.Sprintf("%s %s", ctx.App.Name, c.Name),
}

lineage := ctx.Lineage()
app.ProgramName = lineage[len(lineage)-1].App.Name

if c.HelpName == "" {
app.HelpName = c.HelpName
} else {
Expand Down
81 changes: 81 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"strings"
"testing"
)
Expand Down Expand Up @@ -375,7 +376,87 @@ func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) {
expect(t, err, nil)
expect(t, stdout, c.expectedOut)
}
}

func TestSubCommand_Name_ProgramName_CommandName(t *testing.T) {
cases := []struct {
testArgs []string
expectedOutput []string
}{
{
testArgs: []string{"cmd"},
expectedOutput: []string{"myprogramname", "myprogramname", ""},
},
{
testArgs: []string{"cmd", "foo"},
expectedOutput: []string{"myprogramname foo", "myprogramname", "myprogramname foo"},
},
{
testArgs: []string{"cmd", "foo", "bar"},
expectedOutput: []string{"myprogramname foo bar", "myprogramname", "myprogramname foo bar"},
},
{
testArgs: []string{"cmd", "foo", "bar", "baz"},
expectedOutput: []string{"myprogramname foo bar", "myprogramname", "myprogramname foo bar baz"},
},
}

for _, c := range cases {
var output []string

app := &App{
Name: "myprogramname",
Action: func(c *Context) error {
output = append(output, c.App.Name)
output = append(output, c.App.ProgramName)
output = append(output, c.App.CommandName)
return nil
},
Commands: []*Command{
{
Name: "foo",
Action: func(c *Context) error {
output = append(output, c.App.Name)
output = append(output, c.App.ProgramName)
output = append(output, c.App.CommandName)
return nil
},
Subcommands: []*Command{
{
Name: "bar",
Action: func(c *Context) error {
output = append(output, c.App.Name)
output = append(output, c.App.ProgramName)
output = append(output, c.App.CommandName)
return nil
},
Subcommands: []*Command{
{
Name: "baz",
Action: func(c *Context) error {
output = append(output, c.App.Name)
output = append(output, c.App.ProgramName)
output = append(output, c.App.CommandName)
return nil
},
},
},
},
},
},
},
}

err := app.Run(c.testArgs)
if err != nil {
log.Fatal(err)
}

if !equal(output, c.expectedOutput) {
t.Errorf("want %v, got %v", c.expectedOutput, output)
}

}
}

func TestCommand_NoVersionFlagOnCommands(t *testing.T) {
Expand Down
12 changes: 12 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,15 @@ func expect(t *testing.T, a interface{}, b interface{}) {
t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}

func equal(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}