Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Several 'help' fixes #51

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ package cobra
import (
"bytes"
"fmt"
"github.com/inconshreveable/mousetrap"
flag "github.com/spf13/pflag"
"io"
"os"
"runtime"
"strings"
"time"

"github.com/inconshreveable/mousetrap"
flag "github.com/spf13/pflag"
)

// Command is just that, a command for your application.
Expand Down Expand Up @@ -790,6 +791,13 @@ func (c *Command) HasParent() bool {
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the change here is that you are calling c.mergePersistentFlags() here. (And then making sure mergePersistentFlags() won't bug later)

So is there a real bug that somehow we missed a meaningful and necessary c.mergePersistendFlags() call somewhere else? I really wish I knew how to trigger it...

func (c *Command) Flags() *flag.FlagSet {
if c.flags == nil {
Expand All @@ -798,7 +806,7 @@ func (c *Command) Flags() *flag.FlagSet {
c.flagErrorBuf = new(bytes.Buffer)
}
c.flags.SetOutput(c.flagErrorBuf)
c.PersistentFlags().BoolVarP(&c.helpFlagVal, "help", "h", false, "help for "+c.Name())
c.assureHelpFlag()
}
return c.flags
}
Expand Down Expand Up @@ -934,7 +942,9 @@ func (c *Command) mergePersistentFlags() {
}
c.lflags.SetOutput(c.flagErrorBuf)
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.PersistentFlags().VisitAll(addtolocal)
Expand Down