From 458d79748e944cc033fd025058f824a88cba6a45 Mon Sep 17 00:00:00 2001 From: Albert Nigmatzianov Date: Wed, 19 Apr 2017 10:49:22 +0200 Subject: [PATCH] Add merging of CommandLine flags to Root command --- cobra_test.go | 8 ++++++++ command.go | 2 ++ 2 files changed, 10 insertions(+) diff --git a/cobra_test.go b/cobra_test.go index 48d13d3..d6593a5 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -1147,6 +1147,14 @@ func TestGlobalNormFuncPropagation(t *testing.T) { } } +func TestFlagOnPflagCommandLine(t *testing.T) { + flagName := "flagOnCommandLine" + pflag.CommandLine.String(flagName, "", "about my flag") + r := fullSetupTest("--help") + + checkResultContains(t, r, flagName) +} + func TestAddTemplateFunctions(t *testing.T) { AddTemplateFunc("t", func() bool { return true }) AddTemplateFuncs(template.FuncMap{ diff --git a/command.go b/command.go index 52231c3..f912736 100644 --- a/command.go +++ b/command.go @@ -1258,6 +1258,8 @@ func (c *Command) updateParentsPflags() (added []*flag.Flag) { c.parentsPflags.SortFlags = false } + c.Root().PersistentFlags().AddFlagSet(flag.CommandLine) + c.VisitParents(func(x *Command) { if x.HasPersistentFlags() { x.PersistentFlags().VisitAll(func(f *flag.Flag) {