This commit is contained in:
Steve Domino 2015-09-08 18:57:38 +00:00
commit b5cd2088e1
3 changed files with 57 additions and 9 deletions

View file

@ -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",

View file

@ -917,13 +917,17 @@ func (c *Command) LocalFlags() *flag.FlagSet {
local := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
c.lflags.VisitAll(func(f *flag.Flag) {
if !f.Hidden {
local.AddFlag(f)
}
})
if !c.HasParent() {
flag.CommandLine.VisitAll(func(f *flag.Flag) {
if local.Lookup(f.Name) == nil {
if !f.Hidden {
local.AddFlag(f)
}
}
})
}
return local
@ -942,8 +946,10 @@ 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 {
if !f.Hidden {
inherited.AddFlag(f)
}
}
})
}
if x.HasParent() {

View file

@ -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