'Differentiate between required and optional flags in help message in Cobra
I have created a command as follows:
cmd := &cobra.Command{
Use: "get",
Short: "Gets information",
Long: heredoc.Doc("Long description"),
Example: heredoc.Doc("example"),
ValidArgs: []string{"arg1", "arg2", "arg3"},
RunE: func(cmd *cobra.Command, args []string) error {
return myCustomExecutionFunc()
},
SilenceErrors: true,
}
cmd.PersistentFlags().StringVarP(arg1, "arg1", "", "", "Arg1 definition")
cmd.MarkPersistentFlagRequired("arg1")
cmd.PersistentFlags().StringVarP(arg2, "arg2", "", "", "Arg2 definition")
cmd.MarkPersistentFlagRequired("arg2")
cmd.PersistentFlags().StringVarP(arg3, "arg3", "", "", "Arg3 definition")
Now when run the command with --help
I get the help message as follows.
Flags:
arg1 Arg1 definition
arg3 Arg3 definition
arg2 Arg2 definition
I want to group the flags as persistent (required) and non-persistent (not required) as follows
Required Flags:
arg1 Arg1 definition
arg2 Arg2 definition
Optional Flags:
arg3 Arg3 definition
Or something like below
Flags:
arg1 Required Arg1 definition
arg2 Required Arg2 definition
arg3 Optional Arg3definition
I know I can use the SetHelpTemplate()
function and set my own string that will be shown when help is instantiated, I'm wondering if there is a native cobra library or pflag way to define different categories of flags.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|