我创建了一个命令,如下所示:
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")
现在,当运行命令时,--help
我会收到如下帮助消息。
Flags:
arg1 Arg1 definition
arg3 Arg3 definition
arg2 Arg2 definition
我想将标志分组为持久(必需)和非持久(不需要),如下所示
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
我知道我可以使用该SetHelpTemplate()
函数并设置我自己的字符串,该字符串将在实例化帮助时显示,我想知道是否有本地 cobra 库或 pflag 方法来定义不同类别的标志。