Skip to content

Commit

Permalink
Rename Command.Subcommands ➡️ Command.Commands
Browse files Browse the repository at this point in the history
Closes #1600
  • Loading branch information
meatballhat committed Nov 29, 2022
1 parent 26b286a commit a22c6de
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 68 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (a *App) newRootCommand() *Command {
After: a.After,
Action: a.Action,
OnUsageError: a.OnUsageError,
Subcommands: a.Commands,
Commands: a.Commands,
Flags: a.Flags,
flagCategories: a.flagCategories,
HideHelp: a.HideHelp,
Expand Down
36 changes: 18 additions & 18 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func ExampleApp_Run_subcommand() {
Aliases: []string{"hi"},
Usage: "use it to see a description",
Description: "This is how we describe hello the function",
Subcommands: []*Command{
Commands: []*Command{
{
Name: "english",
Aliases: []string{"en"},
Expand Down Expand Up @@ -548,7 +548,7 @@ func TestApp_RunDefaultCommandWithSubCommand(t *testing.T) {
{
Name: "foobar",
Aliases: []string{"f"},
Subcommands: []*Command{
Commands: []*Command{
{Name: "jimbob", Aliases: []string{"j"}},
{Name: "carly"},
},
Expand Down Expand Up @@ -985,7 +985,7 @@ func TestApp_UseShortOptionHandlingSubCommand(t *testing.T) {
return nil
},
}
command.Subcommands = []*Command{subCommand}
command.Commands = []*Command{subCommand}
app.Commands = []*Command{command}

err := app.Run([]string{"", "cmd", "sub", "-on", expected})
Expand All @@ -1007,7 +1007,7 @@ func TestApp_UseShortOptionHandlingSubCommand_missing_value(t *testing.T) {
&StringFlag{Name: "name", Aliases: []string{"n"}},
},
}
command.Subcommands = []*Command{subCommand}
command.Commands = []*Command{subCommand}
app.Commands = []*Command{command}

err := app.Run([]string{"", "cmd", "sub", "-n"})
Expand Down Expand Up @@ -1204,7 +1204,7 @@ func TestApp_SetStdin_Subcommand(t *testing.T) {
Commands: []*Command{
{
Name: "command",
Subcommands: []*Command{
Commands: []*Command{
{
Name: "subcommand",
Action: func(c *Context) error {
Expand Down Expand Up @@ -1524,7 +1524,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
appRunInput: []string{"myCLI", "myCommand", "mySubCommand"},
appCommands: []*Command{{
Name: "myCommand",
Subcommands: []*Command{{
Commands: []*Command{{
Name: "mySubCommand",
Flags: []Flag{&StringFlag{Name: "requiredFlag", Required: true}},
}},
Expand All @@ -1550,7 +1550,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--help"},
appCommands: []*Command{{
Name: "myCommand",
Subcommands: []*Command{{
Commands: []*Command{{
Name: "mySubCommand",
Flags: []Flag{&StringFlag{Name: "requiredFlag", Required: true}},
}},
Expand All @@ -1577,7 +1577,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--optional", "cats"},
appCommands: []*Command{{
Name: "myCommand",
Subcommands: []*Command{{
Commands: []*Command{{
Name: "mySubCommand",
Flags: []Flag{&StringFlag{Name: "requiredFlag", Required: true}, &StringFlag{Name: "optional"}},
}},
Expand All @@ -1603,7 +1603,7 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) {
appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--requiredFlag", "cats"},
appCommands: []*Command{{
Name: "myCommand",
Subcommands: []*Command{{
Commands: []*Command{{
Name: "mySubCommand",
Flags: []Flag{&StringFlag{Name: "requiredFlag", Required: true}},
Action: func(c *Context) error {
Expand Down Expand Up @@ -1868,7 +1868,7 @@ func TestApp_Run_CommandWithSubcommandHasHelpTopic(t *testing.T) {
cmd := &Command{
Name: "foo",
Description: "descriptive wall of text about how it does foo things",
Subcommands: []*Command{subCmdBar, subCmdBaz},
Commands: []*Command{subCmdBar, subCmdBaz},
Action: func(c *Context) error { return nil },
}

Expand Down Expand Up @@ -1909,7 +1909,7 @@ func TestApp_Run_SubcommandFullPath(t *testing.T) {
cmd := &Command{
Name: "foo",
Description: "foo commands",
Subcommands: []*Command{subCmd},
Commands: []*Command{subCmd},
}
app.Commands = []*Command{cmd}

Expand Down Expand Up @@ -1943,7 +1943,7 @@ func TestApp_Run_SubcommandHelpName(t *testing.T) {
cmd := &Command{
Name: "foo",
Description: "foo commands",
Subcommands: []*Command{subCmd},
Commands: []*Command{subCmd},
}
app.Commands = []*Command{cmd}

Expand Down Expand Up @@ -1978,7 +1978,7 @@ func TestApp_Run_CommandHelpName(t *testing.T) {
Name: "foo",
HelpName: "custom",
Description: "foo commands",
Subcommands: []*Command{subCmd},
Commands: []*Command{subCmd},
}
app.Commands = []*Command{cmd}

Expand Down Expand Up @@ -2014,7 +2014,7 @@ func TestApp_Run_CommandSubcommandHelpName(t *testing.T) {
Name: "foo",
Usage: "foo commands",
Description: "This is a description",
Subcommands: []*Command{subCmd},
Commands: []*Command{subCmd},
}
app.Commands = []*Command{cmd}

Expand Down Expand Up @@ -2356,7 +2356,7 @@ func TestApp_Run_SubcommandDoesNotOverwriteErrorFromBefore(t *testing.T) {
app := &App{
Commands: []*Command{
{
Subcommands: []*Command{
Commands: []*Command{
{
Name: "sub",
},
Expand Down Expand Up @@ -2636,7 +2636,7 @@ func TestWhenExitSubCommandWithCodeThenAppQuitUnexpectedly(t *testing.T) {
app.Commands = []*Command{
{
Name: "cmd",
Subcommands: []*Command{
Commands: []*Command{
{
Name: "subcmd",
Action: func(c *Context) error {
Expand Down Expand Up @@ -2737,7 +2737,7 @@ func TestFlagAction(t *testing.T) {
Name: "c1",
Flags: []Flag{stringFlag},
Action: func(ctx *Context) error { return nil },
Subcommands: []*Command{
Commands: []*Command{
{
Name: "sub1",
Action: func(ctx *Context) error { return nil },
Expand Down Expand Up @@ -3101,7 +3101,7 @@ func TestPersistentFlag(t *testing.T) {
Destination: &appOverrideCmdInt,
},
},
Subcommands: []*Command{
Commands: []*Command{
{
Name: "subcmd",
Flags: []Flag{
Expand Down
18 changes: 9 additions & 9 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Command struct {
// Execute this function if a usage error occurs.
OnUsageError OnUsageErrorFunc
// List of child commands
Subcommands []*Command
Commands []*Command
// List of flags to parse
Flags []Flag
flagCategories FlagCategories
Expand Down Expand Up @@ -97,7 +97,7 @@ func (c *Command) FullName() string {
}

func (cmd *Command) Command(name string) *Command {
for _, c := range cmd.Subcommands {
for _, c := range cmd.Commands {
if c.HasName(name) {
return c
}
Expand All @@ -110,7 +110,7 @@ func (c *Command) setup(ctx *Context) {
if c.Command(helpCommand.Name) == nil && !c.HideHelp {
if !c.HideHelpCommand {
helpCommand.HelpName = fmt.Sprintf("%s %s", c.HelpName, helpName)
c.Subcommands = append(c.Subcommands, helpCommand)
c.Commands = append(c.Commands, helpCommand)
}
}

Expand All @@ -124,19 +124,19 @@ func (c *Command) setup(ctx *Context) {
}

c.categories = newCommandCategories()
for _, command := range c.Subcommands {
for _, command := range c.Commands {
c.categories.AddCommand(command.Category, command)
}
sort.Sort(c.categories.(*commandCategories))

var newCmds []*Command
for _, scmd := range c.Subcommands {
for _, scmd := range c.Commands {
if scmd.HelpName == "" {
scmd.HelpName = fmt.Sprintf("%s %s", c.HelpName, scmd.Name)
}
newCmds = append(newCmds, scmd)
}
c.Subcommands = newCmds
c.Commands = newCmds
}

func (c *Command) Run(cCtx *Context, arguments ...string) (err error) {
Expand Down Expand Up @@ -237,8 +237,8 @@ func (c *Command) Run(cCtx *Context, arguments ...string) (err error) {

if hasDefault {
dc := cCtx.App.Command(cCtx.App.DefaultCommand)
defaultHasSubcommands = len(dc.Subcommands) > 0
for _, dcSub := range dc.Subcommands {
defaultHasSubcommands = len(dc.Commands) > 0
for _, dcSub := range dc.Commands {
if checkStringSliceIncludes(name, dcSub.Names()) {
isDefaultSubcommand = true
break
Expand Down Expand Up @@ -395,7 +395,7 @@ func (c *Command) VisibleCategories() []CommandCategory {
// VisibleCommands returns a slice of the Commands with Hidden=false
func (c *Command) VisibleCommands() []*Command {
var ret []*Command
for _, command := range c.Subcommands {
for _, command := range c.Commands {
if !command.Hidden {
ret = append(ret, command)
}
Expand Down
26 changes: 13 additions & 13 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ func TestCommand_OnUsageError_WithSubcommand(t *testing.T) {
Commands: []*Command{
{
Name: "bar",
Subcommands: []*Command{
Commands: []*Command{
{
Name: "baz",
},
Expand Down Expand Up @@ -276,7 +276,7 @@ func TestCommand_Run_SubcommandsCanUseErrWriter(t *testing.T) {
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{
Commands: []*Command{
{
Name: "baz",
Usage: "this is for testing",
Expand Down Expand Up @@ -384,10 +384,10 @@ func TestCommand_NoVersionFlagOnCommands(t *testing.T) {
Version: "some version",
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
HideHelp: true,
Name: "bar",
Usage: "this is for testing",
Commands: []*Command{{}}, // some subcommand
HideHelp: true,
Action: func(c *Context) error {
if len(c.Command.VisibleFlags()) != 0 {
t.Fatal("unexpected flag on command")
Expand All @@ -408,9 +408,9 @@ func TestCommand_CanAddVFlagOnCommands(t *testing.T) {
Writer: io.Discard,
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
Name: "bar",
Usage: "this is for testing",
Commands: []*Command{{}}, // some subcommand
Flags: []Flag{
&BoolFlag{
Name: "v",
Expand All @@ -436,7 +436,7 @@ func TestCommand_VisibleSubcCommands(t *testing.T) {
c := &Command{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{
Commands: []*Command{
subc1,
{
Name: "subc2",
Expand Down Expand Up @@ -496,9 +496,9 @@ func TestCommand_RunSubcommandWithDefault(t *testing.T) {
},
},
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
Name: "bar",
Usage: "this is for testing",
Commands: []*Command{{}}, // some subcommand
Action: func(*Context) error {
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestContext_Value_InvalidFlagAccessHandler(t *testing.T) {
Commands: []*Command{
{
Name: "command",
Subcommands: []*Command{
Commands: []*Command{
{
Name: "subcommand",
Action: func(ctx *Context) error {
Expand Down
4 changes: 2 additions & 2 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ func prepareCommands(commands []*Command, level int) []string {
coms = append(coms, prepared)

// recursively iterate subcommands
if len(command.Subcommands) > 0 {
if len(command.Commands) > 0 {
coms = append(
coms,
prepareCommands(command.Subcommands, level+1)...,
prepareCommands(command.Commands, level+1)...,
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func (a *App) prepareFishCommands(commands []*Command, allCommands *[]string, pr
)

// recursively iterate subcommands
if len(command.Subcommands) > 0 {
if len(command.Commands) > 0 {
completions = append(
completions,
a.prepareFishCommands(
command.Subcommands, allCommands, command.Names(),
command.Commands, allCommands, command.Names(),
)...,
)
}
Expand Down
4 changes: 2 additions & 2 deletions fish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func testApp() *App {
},
Name: "config",
Usage: "another usage test",
Subcommands: []*Command{{
Commands: []*Command{{
Aliases: []string{"s", "ss"},
Flags: []Flag{
&StringFlag{Name: "sub-flag", Aliases: []string{"sub-fl", "s"}},
Expand Down Expand Up @@ -110,7 +110,7 @@ func() { ... }
Should be a part of the same code block
`,
Subcommands: []*Command{{
Commands: []*Command{{
Aliases: []string{"su"},
Flags: []Flag{
&BoolFlag{
Expand Down
2 changes: 1 addition & 1 deletion godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ type Command struct {
// Execute this function if a usage error occurs.
OnUsageError OnUsageErrorFunc
// List of child commands
Subcommands []*Command
Commands []*Command
// List of flags to parse
Flags []Flag

Expand Down
Loading

0 comments on commit a22c6de

Please sign in to comment.