在Cobra命令中,需要将标志作为第一个参数。

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

Require a flag as the first argument in a Cobra command

问题

我正在尝试创建一个使用标志(flag)来通知命令行操作的Cobra命令,具体来说是一个配置命令,可以添加或删除一个已配置的设置。例如:

cli> prog_name config --set config_var var_value
cli> prog_name config --unset config_var var_value

在Cobra中有没有办法实现这个功能?我已经阅读了文档,但没有找到验证标志是否是命令中的第一个值的方法。我看到了关于位置参数的信息,但从我所读到的内容来看,标志不被视为参数,因此它们不会被位置参数所覆盖。

我想我可以在我的PreRunE函数中手动进行验证,但如果Cobra有办法设置这个,那可能会更好,因为我更希望Cobra进行解析和匹配,而不是我自己将特定的值与os.Args中的"--set"和"--unset"进行比较或类似的操作。

英文:

I'm trying to create a Cobra command that uses a flag to inform the action of the command, specifically a configuration command that can either add or remove a configured setting. For example

cli> prog_name config --set config_var var_vlue
cli> prog_name config --unset config_var var_value

Is there a way to do this in Cobra? I have been reading through the documentation and haven't found any way to validate that a flag is the first value in the command. I've seen information about positional arguments, but from what I've read it sounds like flags aren't considered arguments, so they wouldn't be covered by positional arguments.

I'd imagine I can do this in my PreRunE function and do the validation manually, but if there's a way to set this in Cobra I think that'd most likely be better, since I'd prefer Cobra to be doing that parsing and matching rather than me have to be comparing specific values in os.Args to "--set" and "--unset" or something similar.

答案1

得分: 0

似乎最好的选择是使用子命令而不是标志来完成这个任务。

英文:

It seems like the best option is to just use a subcommand for this instead of a flag.

答案2

得分: -1

你可以通过查阅这个链接来解决这个问题。

简而言之,你需要使用Flags()函数。你可以在这里找到文档。

package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use: "testprog",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("rootCmd called")
	},
}

var subCmd = &cobra.Command{
	Use: "sub",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(args)
	},
}

func main() {
	rootCmd.AddCommand(subCmd)
    
	flags := subCmd.Flags()

    // 在你的情况下不是必需的
	flags.SetInterspersed(false)

	// Bool使用指定的名称、默认值和用法字符串定义一个bool标志。
	// 返回值是存储标志值的bool变量的地址。
	flags.Bool("test", false, "test flag")

	rootCmd.Execute()
}

让我们看看在终端中会发生什么:

    > ./cobraApp sub --test a
    > [a]
英文:

You can solve this by consulting this link.

In short what you need is the Flags() function. You can find documentation here.


package main

import (
	"fmt"

	"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
	Use: "testprog",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("rootCmd called")
	},
}

var subCmd = &cobra.Command{
	Use: "sub",
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println(args)
	},
}

func main() {
	rootCmd.AddCommand(subCmd)
    
	flags := subCmd.Flags()

    // not necessary in your case
	flags.SetInterspersed(false)

	// Bool defines a bool flag with specified name,
	// default value, and usage string. The return value
	// is the address of a bool variable that stores
	// the value of the flag.
	flags.Bool("test", false, "test flag")

	rootCmd.Execute()
}

Let's see what happens in the terminal:

    > ./cobraApp sub --test a
    > [a]

huangapple
  • 本文由 发表于 2022年10月27日 03:32:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/74213320.html
匿名

发表评论

匿名网友

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

确定