Golang Cobra多个没有值的标志位

huangapple go评论68阅读模式
英文:

Golang Cobra multiple flags with no value

问题

我是你的中文翻译助手,以下是翻译好的内容:

我是Golang的新手,正在尝试使用Cobra框架创建我的第一个CLI应用程序。

我的计划是创建几个命令,并带有许多标志。
这些标志不需要附加值,因为它们可以简单地是-r来重新启动设备。

目前,我已经有了以下的工作代码,但我一直在思考,这可能不是正确的做法。
所以任何帮助都将不胜感激。

目前的逻辑是,每个命令都附带一个默认值,然后在运行命令中查找它,并在捕获到时触发我的函数。

我的"工作代码"如下所示。

我的命令中的init函数包含以下内容。

chargerCmd.Flags().StringP("UpdateFirmware", "u", "", "更新充电器的固件")
	chargerCmd.Flags().Lookup("UpdateFirmware").NoOptDefVal = "yes"
	chargerCmd.Flags().StringP("reboot", "r", "", "重新启动充电器")
	chargerCmd.Flags().Lookup("reboot").NoOptDefVal = "yes"

而运行部分则如下所示。

Run: func(cmd *cobra.Command, args []string) {
		input, _ := cmd.Flags().GetString("UpdateFirmware")
		if input == "yes" {
			fmt.Println("正在更新固件")
			UpdateFirmware(os.Getenv("Test"), os.Getenv("Test2")) 
		}
		input, _ = cmd.Flags().GetString("reboot")
		if input == "yes" {
			fmt.Println("正在重新启动充电器")
		}
	},
英文:

I'm new to Golang, and i'm trying out my first CLI application, using the Cobra framework.

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.

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.

My "working code" looks like below.

My init function, in the command contains the following.

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")
		}
	},

答案1

得分: 0

也许为了让使用更清晰一些,正如Burak在评论中所说,你可以更好地区分命令和标志。使用cobra,你有根命令和附加到根命令的子命令。此外,每个命令都可以接受标志。

在你的情况下,charger是根命令,你想要两个子命令:update_firmwarereboot

所以,举个例子,要重新启动充电器,你可以执行以下命令:

$ charger reboot

在上面的代码中,你试图将子命令定义为标志,这是可能的,但可能不是一个好的做法。

相反,项目应该设置得像这样:https://github.com/hesamchobanlou/stackoverflow/tree/main/74934087

然后,你可以将UpdateFirmware(...)操作移动到cmd/update_firmware.go下相应的命令定义中,而不是尝试在根chargerCmd上检查每个标志变体。

如果这没有帮助,请提供一些更多细节,说明为什么你认为你的方法可能不正确?

英文:

Maybe to make the usage a bit cleaner, as stated in the comment from Burak - you can better differentiate between commands and flags. With cobra you have the root command and sub-commands attached to the root command. Additionaly each command can accept flags.

In your case, charger is the root commands and you want two sub-commands: update_firmware and reboot.

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

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.

If that does not help, provide some more details on why you think your approach might not be correct?

huangapple
  • 本文由 发表于 2022年12月28日 05:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/74934087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定