golang cobra 本地标志分配

问题描述 投票:0回答:1

好吧,我正在掌握眼镜蛇的窍门。 为了识别正在调用子子模块的父级,我必须使用指针,因为有多个父级(以及多个祖父母)。

现在分配本地标志,下面的代码正确吗?

var (
    GcpAccessAnnotateCmd    = *AnnotateCmd
    GcpApprovalsAnnotateCmd = *AnnotateCmd
    SshAccessAnnotateCmd    = *AnnotateCmd
    SshApprovalsAnnotateCmd = *AnnotateCmd
)

func init() {
    GcpAccessAnnotateCmd.Flags().
        StringP("note", "n", "", "Note/Comment to add to the request or task")
    GcpApprovalsAnnotateCmd.Flags().
        StringP("note", "n", "", "Note/Comment to add to the request or task")
    SshAccessAnnotateCmd.Flags().
        StringP("note", "n", "", "Note/Comment to add to the request or task")
    SshApprovalsAnnotateCmd.Flags().
        StringP("note", "n", "", "Note/Comment to add to the request or task")
}

换句话说,既然我有 4 个指针,我是否必须声明该标志 4 次?

go go-cobra
1个回答
0
投票

您还可以对父命令使用持久标志。否则,是的,您必须将标志分配给使用它的每个命令。不过,您可以使用循环而不是重复同一行。

© www.soinside.com 2019 - 2024. All rights reserved.