add PersistentPreRun for help command

Disable any persistent required flags for the help command
This commit is contained in:
Jun Nishimura 2023-07-11 00:02:09 +09:00
parent 10b0388a6e
commit 4b81cd58b2

View file

@ -1190,7 +1190,6 @@ func (c *Command) InitDefaultHelpCmd() {
Short: "Help about any command", Short: "Help about any command",
Long: `Help provides help for any command in the application. Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`, Simply type ` + c.Name() + ` help [path to command] for full details.`,
DisableFlagParsing: true,
ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) { ValidArgsFunction: func(c *Command, args []string, toComplete string) ([]string, ShellCompDirective) {
var completions []string var completions []string
cmd, _, e := c.Root().Find(args) cmd, _, e := c.Root().Find(args)
@ -1210,6 +1209,15 @@ Simply type ` + c.Name() + ` help [path to command] for full details.`,
} }
return completions, ShellCompDirectiveNoFileComp 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) { Run: func(c *Command, args []string) {
cmd, _, e := c.Root().Find(args) cmd, _, e := c.Root().Find(args)
if cmd == nil || e != nil { if cmd == nil || e != nil {