简体   繁体   English

Golang Cobra 多个标志没有价值

[英]Golang Cobra multiple flags with no value

I'm new to Golang, and i'm trying out my first CLI application, using the Cobra framework.我是 Golang 的新手,我正在使用 Cobra 框架尝试我的第一个 CLI 应用程序。

My plan is to have few commands, with many flags.我的计划是使用很少的命令,使用很多标志。 These flags, don't have to have a value attached to them, since they can simply be -r to restart the device.这些标志不必附加任何值,因为它们可以简单地是 -r 来重新启动设备。

Currently, i have the following working, but i keep thinking, that this cannot be the correct way to do it.目前,我有以下工作,但我一直在想,这不是正确的方法。 So any help is appreciated.所以任何帮助表示赞赏。

The logic is currently, that each command, get's a default value attached to it, and then i look for this, in the run command, and triggers my function, once it captures it.目前的逻辑是,每个命令都附加一个默认值,然后我在运行命令中查找它,并在捕获它后触发我的 function。

My "working code" looks like below.我的“工作代码”如下所示。

My init function, in the command contains the following.我的 init function,在命令中包含以下内容。

chargerCmd.Flags().StringP("UpdateFirmware", "u", "", "Updeates the firmware of the charger")
    chargerCmd.Flags().Lookup("UpdateFirmware").NoOptDefVal = "yes"
    chargerCmd.Flags().StringP("reboot", "r", "", "Reboots the charger")
    chargerCmd.Flags().Lookup("reboot").NoOptDefVal = "yes"

And the run section looks like this.运行部分看起来像这样。

Run: func(cmd *cobra.Command, args []string) {
        input, _ := cmd.Flags().GetString("UpdateFirmware")
        if input == "yes" {
            fmt.Println("Updating firmware")
            UpdateFirmware(os.Getenv("Test"), os.Getenv("Test2")) 
        }
        input, _ = cmd.Flags().GetString("reboot")
        if input == "yes" {
            fmt.Println("Rebooting Charger")
        }
    },

Thanks in advance.提前致谢。

Maybe to make the usage a bit cleaner, as stated in the comment from Burak - you can better differentiate between commands and flags.也许是为了让用法更简洁一些,正如 Burak 的评论中所述 - 您可以更好地区分命令和标志。 With cobra you have the root command and sub-commands attached to the root command.使用 Cobra,您拥有根命令和附加到根命令的子命令。 Additionaly each command can accept flags.此外,每个命令都可以接受标志。

In your case, charger is the root commands and you want two sub-commands: update_firmware and reboot .在您的情况下, charger是根命令,您需要两个子命令: update_firmwarereboot

So as an example to reboot the charger, you would execute the command:因此,作为重启充电器的示例,您将执行以下命令:

$ charger reboot

In the code above, you are trying to define sub-commands as flags, which is possible, but likely not good practice.在上面的代码中,您试图将子命令定义为标志,这是可能的,但可能不是好的做法。

Instead, the project should be set-up something like this: https://github.com/hesamchobanlou/stackoverflow/tree/main/74934087相反,项目应该像这样设置: https://github.com/hesamchobanlou/stackoverflow/tree/main/74934087

You can then move the UpdateFirmware(...) operation within the respective command definition under cmd/update_firmware.go instead of trying to check each flag variation on the root chargerCmd.然后,您可以在 cmd/update_firmware.go 下的相应命令定义中移动UpdateFirmware(...)操作,而不是尝试检查根 chargerCmd 上的每个标志变化。

If that does not help, provide some more details on why you think your approach might not be correct?如果这没有帮助,请提供更多详细信息,说明为什么您认为您的方法可能不正确?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM