如何在 cobra 中设置前缀为 -- 的命令名称

How to set command name prefixed with -- in cobra

我想创建一个名为 --list 的命令,但是如果我将 --list 设置为 Use 属性 of cobra.Command 它不起作用。像波纹管代码不起作用。有帮助吗?

    list := &cobra.Command{
    Use:   "--list",
    Short: "Lists all data",
    Run: func(*cobra.Command, []string) {}

您不能创建名称以 --- 开头的命令,因为在 cobra 中这些是标志。见对应的source code in the library, which removes them when parsing命令。