diff --git a/cobra_test.go b/cobra_test.go index 3aed7dd6..da4b50a2 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -18,8 +18,8 @@ var _ = os.Stderr var tp, te, tt, t1, tr []string var rootPersPre, echoPre, echoPersPre, timesPersPre []string -var flagb1, flagb2, flagb3, flagbr, flagbp bool -var flags1, flags2a, flags2b, flags3 string +var flagb1, flagb2, flagb3, flagbr, flagbp, flagbh, flagbph bool +var flags1, flags2a, flags2b, flags3, outs string var flagi1, flagi2, flagi3, flagir int var globalFlag1 bool var flagEcho, rootcalled bool @@ -28,6 +28,18 @@ var versionUsed int const strtwoParentHelp = "help message for parent flag strtwo" const strtwoChildHelp = "help message for child flag strtwo" +var cmdHiddenFlags = &Command{ + Use: "hidden [string to set]", + Short: "set a string value to string var", + Long: `an utterly useless command for testing.`, + Run: func(cmd *Command, args []string) { + outs = "visible" + if flagbh { + outs = "hidden" + } + }, +} + var cmdPrint = &Command{ Use: "print [string to print]", Short: "Print anything to the screen", @@ -976,15 +988,15 @@ func TestFlagOnPflagCommandLine(t *testing.T) { func TestAddTemplateFunctions(t *testing.T) { AddTemplateFunc("t", func() bool { return true }) AddTemplateFuncs(template.FuncMap{ - "f": func() bool { return false }, - "h": func() string { return "Hello," }, + "f": func() bool { return false }, + "h": func() string { return "Hello," }, "w": func() string { return "world." }}) const usage = "Hello, world." - + c := &Command{} c.SetUsageTemplate(`{{if t}}{{h}}{{end}}{{if f}}{{h}}{{end}} {{w}}`) - + if us := c.UsageString(); us != usage { t.Errorf("c.UsageString() != \"%s\", is \"%s\"", usage, us) } diff --git a/command.go b/command.go index bf642b53..5decbe2d 100644 --- a/command.go +++ b/command.go @@ -917,12 +917,16 @@ func (c *Command) LocalFlags() *flag.FlagSet { local := flag.NewFlagSet(c.Name(), flag.ContinueOnError) c.lflags.VisitAll(func(f *flag.Flag) { - local.AddFlag(f) + if !f.Hidden { + local.AddFlag(f) + } }) if !c.HasParent() { flag.CommandLine.VisitAll(func(f *flag.Flag) { if local.Lookup(f.Name) == nil { - local.AddFlag(f) + if !f.Hidden { + local.AddFlag(f) + } } }) } @@ -942,7 +946,9 @@ func (c *Command) InheritedFlags() *flag.FlagSet { if x.HasPersistentFlags() { x.PersistentFlags().VisitAll(func(f *flag.Flag) { if inherited.Lookup(f.Name) == nil && local.Lookup(f.Name) == nil { - inherited.AddFlag(f) + if !f.Hidden { + inherited.AddFlag(f) + } } }) } diff --git a/command_test.go b/command_test.go index 477d84e7..adb6fcf1 100644 --- a/command_test.go +++ b/command_test.go @@ -5,6 +5,36 @@ import ( "testing" ) +func init() { + cmdHiddenFlags.Flags().BoolVarP(&flagbh, "boolh", "", false, "") + cmdHiddenFlags.Flags().MarkHidden("boolh") + + cmdHiddenFlags.PersistentFlags().BoolVarP(&flagbph, "boolph", "", false, "") + cmdHiddenFlags.PersistentFlags().MarkHidden("boolph") +} + +// test to ensure hidden flags run as intended; if the the hidden flag fails to +// run, the output will be incorrect +func TestHiddenFlagExecutes(t *testing.T) { + cmdHiddenFlags.execute([]string{"--boolh"}) + if outs != "hidden" { + t.Errorf("Hidden flag failed to run!") + } +} + +// test to ensure hidden flags do not show up in usage/help text; if a flag is +// found by Lookup() it will be visible in usage/help text +func TestHiddenFlagsAreHidden(t *testing.T) { + + if cmdHiddenFlags.LocalFlags().Lookup("boolh") != nil { + t.Errorf("unexpected flag 'boolh'") + } + + if cmdHiddenFlags.InheritedFlags().Lookup("boolph") != nil { + t.Errorf("unexpected flag 'boolph'") + } +} + func TestStripFlags(t *testing.T) { tests := []struct { input []string