cobra icon indicating copy to clipboard operation
cobra copied to clipboard

Is it possible to handle -h and --help differently?

Open TLINDEN opened this issue 3 months ago • 2 comments

Many programs show a short help message with -h and a detailed one with --help. Currently cobra handles both options in the same way. Is it possible to change this somehow with custom help messages?

TLINDEN avatar Oct 01 '25 19:10 TLINDEN

Interesting question. You may be able to figure out which form of the flag was used on the command line in your custom function.

marckhouzam avatar Oct 01 '25 22:10 marckhouzam

My current simple workaround is to catch -h before cobra takes over:

[..]
        rootCmd.SetUsageTemplate(strings.TrimSpace(usage) + "\n")

        if slices.Contains(os.Args, "-h") {
                fmt.Println(shortusage)
                os.Exit(0)
        }

        err := rootCmd.Execute()
        if err != nil {
                os.Exit(1)
        }

TLINDEN avatar Oct 02 '25 19:10 TLINDEN