forked from mirror/cobra
Merge pull request #127 from eparis/show-commandline-on-parent
Display pflag.CommandLine flags as if they were declared on the parent
This commit is contained in:
commit
db05184446
|
@ -963,3 +963,11 @@ func TestGlobalNormFuncPropagation(t *testing.T) {
|
||||||
t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")
|
t.Error("cmdPrint and cmdEchoSub should had the normalization function of rootCmd")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFlagOnPflagCommandLine(t *testing.T) {
|
||||||
|
flagName := "flagOnCommandLine"
|
||||||
|
pflag.CommandLine.String(flagName, "", "about my flag")
|
||||||
|
r := fullSetupTest("--help")
|
||||||
|
|
||||||
|
checkResultContains(t, r, flagName)
|
||||||
|
}
|
||||||
|
|
|
@ -158,7 +158,6 @@ func (c *Command) SetHelpTemplate(s string) {
|
||||||
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
|
func (c *Command) SetGlobalNormalizationFunc(n func(f *flag.FlagSet, name string) flag.NormalizedName) {
|
||||||
c.Flags().SetNormalizeFunc(n)
|
c.Flags().SetNormalizeFunc(n)
|
||||||
c.PersistentFlags().SetNormalizeFunc(n)
|
c.PersistentFlags().SetNormalizeFunc(n)
|
||||||
c.LocalFlags().SetNormalizeFunc(n)
|
|
||||||
c.globNormFunc = n
|
c.globNormFunc = n
|
||||||
|
|
||||||
for _, command := range c.commands {
|
for _, command := range c.commands {
|
||||||
|
@ -873,6 +872,13 @@ func (c *Command) LocalFlags() *flag.FlagSet {
|
||||||
c.lflags.VisitAll(func(f *flag.Flag) {
|
c.lflags.VisitAll(func(f *flag.Flag) {
|
||||||
local.AddFlag(f)
|
local.AddFlag(f)
|
||||||
})
|
})
|
||||||
|
if !c.HasParent() {
|
||||||
|
flag.CommandLine.VisitAll(func(f *flag.Flag) {
|
||||||
|
if local.Lookup(f.Name) == nil {
|
||||||
|
local.AddFlag(f)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
return local
|
return local
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue