forked from mirror/cobra
Parsing persistentFlags properly (mostly)
This commit is contained in:
parent
ce6447a965
commit
36e08ffd5b
22
cobra.go
22
cobra.go
|
@ -136,7 +136,7 @@ func (c *Command) execute(args []string) (err error) {
|
||||||
|
|
||||||
cmd, a, e := c.Find(args)
|
cmd, a, e := c.Find(args)
|
||||||
if e == nil {
|
if e == nil {
|
||||||
cmd.Flags().Parse(a)
|
err = cmd.ParseFlags(a)
|
||||||
argWoFlags := cmd.Flags().Args()
|
argWoFlags := cmd.Flags().Args()
|
||||||
cmd.Run(cmd, argWoFlags)
|
cmd.Run(cmd, argWoFlags)
|
||||||
return nil
|
return nil
|
||||||
|
@ -296,16 +296,20 @@ func (c *Command) ParseFlags(args []string) (err error) {
|
||||||
|
|
||||||
// Climbs up the command tree parsing flags from top to bottom
|
// Climbs up the command tree parsing flags from top to bottom
|
||||||
func (c *Command) ParsePersistentFlags(args []string) (err error) {
|
func (c *Command) ParsePersistentFlags(args []string) (err error) {
|
||||||
if !c.HasParent() || c.parent.PersistentFlags().Parsed() {
|
if !c.HasParent() || (c.parent.HasPersistentFlags() && c.parent.PersistentFlags().Parsed()) {
|
||||||
err = c.PersistentFlags().Parse(args)
|
if c.HasPersistentFlags() {
|
||||||
if err != nil {
|
err = c.PersistentFlags().Parse(args)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
err = c.parent.ParsePersistentFlags(args)
|
if c.HasParent() && c.parent.HasPersistentFlags() {
|
||||||
if err != nil {
|
err = c.parent.ParsePersistentFlags(args)
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue