mirror of
https://github.com/spf13/cobra
synced 2025-05-06 13:27:26 +00:00
allow for a word other than "help" for the help command and flag
This commit is contained in:
parent
d2d81d9a96
commit
191297c568
1 changed files with 20 additions and 12 deletions
32
command.go
32
command.go
|
@ -83,6 +83,9 @@ type Command struct {
|
||||||
// will print content of the "Version" variable.
|
// will print content of the "Version" variable.
|
||||||
Version string
|
Version string
|
||||||
|
|
||||||
|
// HelpVerb deines the word used for "help" in the default command and flag.
|
||||||
|
HelpVerb string
|
||||||
|
|
||||||
// The *Run functions are executed in the following order:
|
// The *Run functions are executed in the following order:
|
||||||
// * PersistentPreRun()
|
// * PersistentPreRun()
|
||||||
// * PreRun()
|
// * PreRun()
|
||||||
|
@ -404,7 +407,7 @@ Aliases:
|
||||||
Examples:
|
Examples:
|
||||||
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
|
{{.Example}}{{end}}{{if .HasAvailableSubCommands}}
|
||||||
|
|
||||||
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
|
Available Commands:{{range .Commands}}{{if (or .IsAvailableCommand (eq .Name .HelpVerb))}}
|
||||||
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
|
{{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .HasAvailableLocalFlags}}
|
||||||
|
|
||||||
Flags:
|
Flags:
|
||||||
|
@ -681,6 +684,11 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
|
c.Printf("Command %q is deprecated, %s\n", c.Name(), c.Deprecated)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set the help verb to "help" if a custom word is not defined.
|
||||||
|
if len(c.HelpVerb) == 0 {
|
||||||
|
c.HelpVerb = "help"
|
||||||
|
}
|
||||||
|
|
||||||
// initialize help and version flag at the last point possible to allow for user
|
// initialize help and version flag at the last point possible to allow for user
|
||||||
// overriding
|
// overriding
|
||||||
c.InitDefaultHelpFlag()
|
c.InitDefaultHelpFlag()
|
||||||
|
@ -693,11 +701,11 @@ func (c *Command) execute(a []string) (err error) {
|
||||||
|
|
||||||
// If help is called, regardless of other flags, return we want help.
|
// If help is called, regardless of other flags, return we want help.
|
||||||
// Also say we need help if the command isn't runnable.
|
// Also say we need help if the command isn't runnable.
|
||||||
helpVal, err := c.Flags().GetBool("help")
|
helpVal, err := c.Flags().GetBool(c.HelpVerb)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// should be impossible to get here as we always declare a help
|
// should be impossible to get here as we always declare a help
|
||||||
// flag in InitDefaultHelpFlag()
|
// flag in InitDefaultHelpFlag()
|
||||||
c.Println("\"help\" flag declared as non-bool. Please correct your code")
|
c.Printf("\"%s\" flag declared as non-bool. Please correct your code\n", c.HelpVerb)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -839,7 +847,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
||||||
}
|
}
|
||||||
if !c.SilenceErrors {
|
if !c.SilenceErrors {
|
||||||
c.Println("Error:", err.Error())
|
c.Println("Error:", err.Error())
|
||||||
c.Printf("Run '%v --help' for usage.\n", c.CommandPath())
|
c.Printf("Run '%v --%s' for usage.\n", c.CommandPath(), c.HelpVerb)
|
||||||
}
|
}
|
||||||
return c, err
|
return c, err
|
||||||
}
|
}
|
||||||
|
@ -904,14 +912,14 @@ func (c *Command) validateRequiredFlags() error {
|
||||||
// If c already has help flag, it will do nothing.
|
// If c already has help flag, it will do nothing.
|
||||||
func (c *Command) InitDefaultHelpFlag() {
|
func (c *Command) InitDefaultHelpFlag() {
|
||||||
c.mergePersistentFlags()
|
c.mergePersistentFlags()
|
||||||
if c.Flags().Lookup("help") == nil {
|
if c.Flags().Lookup(c.HelpVerb) == nil {
|
||||||
usage := "help for "
|
usage := c.HelpVerb + " for "
|
||||||
if c.Name() == "" {
|
if c.Name() == "" {
|
||||||
usage += "this command"
|
usage += "this command"
|
||||||
} else {
|
} else {
|
||||||
usage += c.Name()
|
usage += c.Name()
|
||||||
}
|
}
|
||||||
c.Flags().BoolP("help", "h", false, usage)
|
c.Flags().BoolP(c.HelpVerb, "h", false, usage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -946,15 +954,15 @@ func (c *Command) InitDefaultHelpCmd() {
|
||||||
|
|
||||||
if c.helpCommand == nil {
|
if c.helpCommand == nil {
|
||||||
c.helpCommand = &Command{
|
c.helpCommand = &Command{
|
||||||
Use: "help [command]",
|
Use: c.HelpVerb + " [command]",
|
||||||
Short: "Help about any command",
|
Short: strings.Title(c.HelpVerb) + " about any command",
|
||||||
Long: `Help provides help for any command in the application.
|
Long: fmt.Sprintf(`%s provides help for any command in the application.
|
||||||
Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
Simply type %s %s [path to command] for full details.`, strings.Title(c.HelpVerb), c.Name(), c.HelpVerb),
|
||||||
|
|
||||||
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 {
|
||||||
c.Printf("Unknown help topic %#q\n", args)
|
c.Printf("Unknown %s topic %#q\n", c.HelpVerb, args)
|
||||||
c.Root().Usage()
|
c.Root().Usage()
|
||||||
} else {
|
} else {
|
||||||
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
|
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
|
||||||
|
|
Loading…
Add table
Reference in a new issue