mirror of
https://github.com/spf13/cobra
synced 2025-05-05 12:57:22 +00:00
Merge 88ff9afb73
into f576d29563
This commit is contained in:
commit
eef7a86b0f
1 changed files with 14 additions and 4 deletions
18
command.go
18
command.go
|
@ -18,13 +18,14 @@ package cobra
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/inconshreveable/mousetrap"
|
|
||||||
flag "github.com/spf13/pflag"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/inconshreveable/mousetrap"
|
||||||
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Command is just that, a command for your application.
|
// Command is just that, a command for your application.
|
||||||
|
@ -790,6 +791,13 @@ func (c *Command) HasParent() bool {
|
||||||
return c.parent != nil
|
return c.parent != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) assureHelpFlag() {
|
||||||
|
if c.Flags().Lookup("help") == nil && c.PersistentFlags().Lookup("help") == nil {
|
||||||
|
c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())
|
||||||
|
}
|
||||||
|
c.mergePersistentFlags()
|
||||||
|
}
|
||||||
|
|
||||||
// Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)
|
// Get the complete FlagSet that applies to this command (local and persistent declared here and by all parents)
|
||||||
func (c *Command) Flags() *flag.FlagSet {
|
func (c *Command) Flags() *flag.FlagSet {
|
||||||
if c.flags == nil {
|
if c.flags == nil {
|
||||||
|
@ -798,7 +806,7 @@ func (c *Command) Flags() *flag.FlagSet {
|
||||||
c.flagErrorBuf = new(bytes.Buffer)
|
c.flagErrorBuf = new(bytes.Buffer)
|
||||||
}
|
}
|
||||||
c.flags.SetOutput(c.flagErrorBuf)
|
c.flags.SetOutput(c.flagErrorBuf)
|
||||||
c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())
|
c.assureHelpFlag()
|
||||||
}
|
}
|
||||||
return c.flags
|
return c.flags
|
||||||
}
|
}
|
||||||
|
@ -934,7 +942,9 @@ func (c *Command) mergePersistentFlags() {
|
||||||
}
|
}
|
||||||
c.lflags.SetOutput(c.flagErrorBuf)
|
c.lflags.SetOutput(c.flagErrorBuf)
|
||||||
addtolocal := func(f *flag.Flag) {
|
addtolocal := func(f *flag.Flag) {
|
||||||
c.lflags.AddFlag(f)
|
if c.lflags.Lookup(f.Name) == nil {
|
||||||
|
c.lflags.AddFlag(f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
c.Flags().VisitAll(addtolocal)
|
c.Flags().VisitAll(addtolocal)
|
||||||
c.PersistentFlags().VisitAll(addtolocal)
|
c.PersistentFlags().VisitAll(addtolocal)
|
||||||
|
|
Loading…
Add table
Reference in a new issue