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

"help" subcommand does not work when MarkPersistentRequiredFlag is used. #1918

Open
makiuchi-d opened this issue Feb 28, 2023 · 4 comments · May be fixed by #1992
Open

"help" subcommand does not work when MarkPersistentRequiredFlag is used. #1918

makiuchi-d opened this issue Feb 28, 2023 · 4 comments · May be fixed by #1992
Labels
area/cobra-command Core `cobra.Command` implementations kind/bug A bug in cobra; unintended behavior

Comments

@makiuchi-d
Copy link

This issue seems to be similar to #1093.

The auto-implemented subcommands (help and completion) don't work when MarkPersistentRequiredFlag() called on root command.
Of course it works if the required flag is specified.
IMHO, these auto-implemented subcommands should be able to work without flags defined by user.
Additionaly, these subcommends should not ignore auto-implement flags like -h, --help (this differs from #1093).

package main

import (
	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use: "mycmd",
}

var subCmd = &cobra.Command{
	Use: "subcmd",
	Run: func(cmd *cobra.Command, args []string) {},
}

func main() {
	rootCmd.PersistentFlags().Bool("required", true, "required")
	rootCmd.MarkPersistentFlagRequired("required")
	rootCmd.AddCommand(subCmd)
	rootCmd.Execute()
}
$ go run . help
Error: required flag(s) "required" not set
Usage:
  mycmd help [command] [flags]

Flags:
  -h, --help   help for help

Global Flags:
      --required   required (default true)
@marckhouzam
Copy link
Collaborator

Thanks @makiuchi-d. You're point sounds reasonable to me. I would also consider this a bug.

@marckhouzam marckhouzam added kind/bug A bug in cobra; unintended behavior area/cobra-command Core `cobra.Command` implementations labels Feb 28, 2023
@payxn
Copy link

payxn commented Mar 21, 2023

Hello,

Do you have a workaround for this ?

@makiuchi-d
Copy link
Author

No, I always specify the required flags. (although I often use aliases or shell scripts)

@JunNishimura
Copy link
Contributor

JunNishimura commented Jul 9, 2023

I made PR for this Issue!

@marckhouzam
I would appreciate it if you could check the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/cobra-command Core `cobra.Command` implementations kind/bug A bug in cobra; unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants