From 6d094981690fd1cff857a94bd1f3d169ecf2ca90 Mon Sep 17 00:00:00 2001 From: Irioth Date: Thu, 18 Jun 2020 00:24:04 +0300 Subject: [PATCH 1/4] hide version flag for subcommands --- command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command.go b/command.go index daa58c85e3..dda2f49a0a 100644 --- a/command.go +++ b/command.go @@ -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 From 44371a2ac6c3bf1372ca23ff24d16038590c6c7c Mon Sep 17 00:00:00 2001 From: Irioth Date: Thu, 18 Jun 2020 00:46:15 +0300 Subject: [PATCH 2/4] test for version flag on commands --- command_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/command_test.go b/command_test.go index 765081ea1f..c1d106dfb9 100644 --- a/command_test.go +++ b/command_test.go @@ -377,3 +377,27 @@ 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 + Action: func(c *Context) error { + for _, f := range c.App.VisibleFlags() { + if f == VersionFlag { + t.Fatalf("unexpected version flag") + } + } + return nil + }, + }, + }, + } + + err := app.Run([]string{"foo", "bar"}) + expect(t, err, nil) +} From 1f3e0b52339258611596d10319d35d1e9f38bc55 Mon Sep 17 00:00:00 2001 From: Irioth Date: Thu, 18 Jun 2020 01:02:47 +0300 Subject: [PATCH 3/4] make test more general and stable --- command_test.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/command_test.go b/command_test.go index c1d106dfb9..a3c3b84e9a 100644 --- a/command_test.go +++ b/command_test.go @@ -386,11 +386,10 @@ func TestCommand_NoVersionFlagOnCommands(t *testing.T) { Name: "bar", Usage: "this is for testing", Subcommands: []*Command{{}}, // some subcommand + HideHelp: true, Action: func(c *Context) error { - for _, f := range c.App.VisibleFlags() { - if f == VersionFlag { - t.Fatalf("unexpected version flag") - } + if len(c.App.VisibleFlags()) != 0 { + t.Fatalf("unexpected flag on command") } return nil }, From ef2d047c454173cb2956531d8a45997edcf7fbb8 Mon Sep 17 00:00:00 2001 From: Irioth Date: Thu, 18 Jun 2020 09:34:54 +0300 Subject: [PATCH 4/4] added test for successfully used -v flag on command with subcommands --- command_test.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/command_test.go b/command_test.go index a3c3b84e9a..6add442865 100644 --- a/command_test.go +++ b/command_test.go @@ -389,7 +389,7 @@ func TestCommand_NoVersionFlagOnCommands(t *testing.T) { HideHelp: true, Action: func(c *Context) error { if len(c.App.VisibleFlags()) != 0 { - t.Fatalf("unexpected flag on command") + t.Fatal("unexpected flag on command") } return nil }, @@ -400,3 +400,25 @@ func TestCommand_NoVersionFlagOnCommands(t *testing.T) { 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) +}