Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,9 @@ func (c *Command) InitDefaultHelpFlag() {
} else {
usage += name
}
if f := c.Flags().ShorthandLookup("h"); f != nil {
panic(fmt.Sprintf("shorthand '-h' is reserved for the help flag, but is already used by flag '%s'", f.Name))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would mention that InitDefaultFlag cannot be used because -h is already registered by f.Name

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@ccoVeille you are correct: with the latest change we keep the "-h is reserved for help" behavior.
If a developer registers -h for another flag (e.g. ayuda), Cobra now panics early with a clearer message:
panic: shorthand '-h' is reserved for the help flag, but is already used by flag 'ayuda'

This is also covered by a unit test:
TestInitDefaultHelpFlagPanicsWhenHShorthandAlreadyUsed

cc: @marckhouzam

}
c.Flags().BoolP(helpFlagName, "h", false, usage)
_ = c.Flags().SetAnnotation(helpFlagName, FlagSetByCobraAnnotation, []string{"true"})
}
Expand Down
23 changes: 23 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,29 @@ func TestInitHelpFlagMergesFlags(t *testing.T) {
}
}

// related to https://github.com/spf13/cobra/issues/2359
func TestInitDefaultHelpFlagPanicsWhenHShorthandAlreadyUsed(t *testing.T) {
cmd := &Command{Use: "root"}

cmd.PersistentFlags().BoolP("ayuda", "h", false, "help")

func() {
defer func() {
r := recover()
if r == nil {
t.Fatalf("expected InitDefaultHelpFlag to panic")
}
// Without Cobra's explicit guard, pflag will also panic on duplicate shorthands.
// Assert on the message to ensure we hit Cobra's clearer, intentional panic.
msg := fmt.Sprint(r)
if !strings.Contains(msg, "reserved for the help flag") {
t.Fatalf("expected reserved -h panic message, got: %v", r)
}
}()
cmd.InitDefaultHelpFlag()
}()
}

func TestHelpCommandExecuted(t *testing.T) {
rootCmd := &Command{Use: "root", Long: "Long description", Run: emptyRun}
rootCmd.AddCommand(&Command{Use: "child", Run: emptyRun})
Expand Down
2 changes: 1 addition & 1 deletion completions.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ func CompDebug(msg string, printToStdErr bool) {
// Such logs are only printed when the user has set the environment
// variable BASH_COMP_DEBUG_FILE to the path of some file to be used.
if path := os.Getenv("BASH_COMP_DEBUG_FILE"); path != "" {
f, err := os.OpenFile(path,
f, err := os.OpenFile(path, //nolint:gosec
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err == nil {
defer f.Close()
Expand Down
Loading