mirror of
https://github.com/spf13/cobra
synced 2024-11-24 22:57:12 +00:00
add PersistentPreRun for help command
Disable any persistent required flags for the help command
This commit is contained in:
parent
10b0388a6e
commit
4b81cd58b2
1 changed files with 9 additions and 1 deletions
10
command.go
10
command.go
|
@ -1190,7 +1190,6 @@ func (c *Command) InitDefaultHelpCmd() {
|
|||
Short: "Help about any command",
|
||||
Long: `Help provides help for any command in the application.
|
||||
Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
||||
DisableFlagParsing: true,
|
||||
ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
|
||||
var completions []string
|
||||
cmd, _, e := c.Root().Find(args)
|
||||
|
@ -1210,6 +1209,15 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
|||
}
|
||||
return completions, ShellCompDirectiveNoFileComp
|
||||
},
|
||||
PersistentPreRun: func(cmd *Command, args []string) {
|
||||
cmd.Flags().VisitAll(func(pflag *flag.Flag) {
|
||||
requiredAnnotation, found := pflag.Annotations[BashCompOneRequiredFlag]
|
||||
if found && requiredAnnotation[0] == "true" {
|
||||
// Disable any persistent required flags for the help command
|
||||
pflag.Annotations[BashCompOneRequiredFlag] = []string{"false"}
|
||||
}
|
||||
})
|
||||
},
|
||||
Run: func(c *Command, args []string) {
|
||||
cmd, _, e := c.Root().Find(args)
|
||||
if cmd == nil || e != nil {
|
||||
|
|
Loading…
Reference in a new issue