Merge pull request #1153 from Irioth/master

Bugfix: Remove version flag from commands with subcommands
This commit is contained in:
lynn (they) 2020-06-23 09:23:47 -07:00 committed by GitHub
commit 9047f3b42d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 1 deletions

View File

@ -241,7 +241,7 @@ func (c *Command) startApp(ctx *Context) error {
app.HideHelpCommand = c.HideHelpCommand
app.Version = ctx.App.Version
app.HideVersion = ctx.App.HideVersion
app.HideVersion = true
app.Compiled = ctx.App.Compiled
app.Writer = ctx.App.Writer
app.ErrWriter = ctx.App.ErrWriter

View File

@ -377,3 +377,48 @@ func TestCommand_Run_CustomShellCompleteAcceptsMalformedFlags(t *testing.T) {
}
}
func TestCommand_NoVersionFlagOnCommands(t *testing.T) {
app := &App{
Version: "some version",
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
HideHelp: true,
Action: func(c *Context) error {
if len(c.App.VisibleFlags()) != 0 {
t.Fatal("unexpected flag on command")
}
return nil
},
},
},
}
err := app.Run([]string{"foo", "bar"})
expect(t, err, nil)
}
func TestCommand_CanAddVFlagOnCommands(t *testing.T) {
app := &App{
Version: "some version",
Writer: ioutil.Discard,
Commands: []*Command{
{
Name: "bar",
Usage: "this is for testing",
Subcommands: []*Command{{}}, // some subcommand
Flags: []Flag{
&BoolFlag{
Name: "v",
},
},
},
},
}
err := app.Run([]string{"foo", "bar"})
expect(t, err, nil)
}