Skip to content

Instantly share code, notes, and snippets.

@eparis
Created September 1, 2015 15:33
Show Gist options
  • Select an option

  • Save eparis/5d0c25f605209cf2c1c0 to your computer and use it in GitHub Desktop.

Select an option

Save eparis/5d0c25f605209cf2c1c0 to your computer and use it in GitHub Desktop.
Program testing user defined --help flag and contextual information in help text
package main
import (
"fmt"
"github.com/spf13/cobra"
)
var _ = fmt.Print
func main() {
cmd := &cobra.Command{
Use: "cmd",
Short: "cmd help",
Run: func(_ *cobra.Command, _ []string) {},
}
cmd.Flags().Bool("help", false, "help flag")
cmd.SetHelpTemplate(`{{.Name }} is so cool and awesome
`)
subcmd := &cobra.Command{
Use: "subcmd",
Short: "subcmd help",
Run: func(_ *cobra.Command, _ []string) {},
}
subcmd.Flags().BoolP("help", "s", false, "subcmd help flag")
subcmd.SetHelpTemplate(`But {{.Short}} is how it should be
`)
cmd.AddCommand(subcmd)
subcmd2 := &cobra.Command{
Use: "subcmd2",
Short: "subcmd help",
Run: func(_ *cobra.Command, _ []string) {},
}
subcmd2.Flags().String("help", "help", "subcmd2 help flag")
cmd.AddCommand(subcmd2)
cmd.Execute()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment