如何在Golang中使用Cobra的子命令?

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

How to use subcommand with cobra golang?

问题

抱歉,但我找不到使用以下示例的方法:

./client echo --times 10 "coucou"
./client --times 10 "coucou" echo

无法使用它...对我的错误表示抱歉。

最好的问候,
Nicolas

func main() {
    var echoTimes int

    var cmdEcho = &cobra.Command{
        Use:   "echo [string to echo]",
        Short: "Echo anything to the screen",
        Long:  `echo is for echoing anything back.
        Echo works a lot like print, except it has a child command.
        `,
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println("Print: " + strings.Join(args, " "))
        },
    }

    var cmdTimes = &cobra.Command{
        Use:   "times [# times] [string to echo]",
        Short: "Echo anything to the screen more times",
        Long:  `echo things multiple times back to the user by providing
        a count and a string.`,
        Run: func(cmd *cobra.Command, args []string) {
            for i:=0; i < echoTimes; i++ {
                fmt.Println("Echo: " + strings.Join(args, " "))
            }
        },
    }

    cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input")
    var rootCmd = &cobra.Command{Use: "app"}
    rootCmd.AddCommand(cmdEcho)
    cmdEcho.AddCommand(cmdTimes)
    rootCmd.Execute()
}

以上是要翻译的内容。

英文:

Sorry but I cannot find a way to use the example

./client echo --times 10 &quot;coucou&quot; 
./client  --times 10 &quot;coucou&quot; echo

No way to use it... sorry for my mistake.

Best regards,
Nicolas

    func main() {
    var echoTimes int

    var cmdEcho = &amp;cobra.Command{
        Use:   &quot;echo [string to echo]&quot;,
        Short: &quot;Echo anything to the screen&quot;,
        Long:  `echo is for echoing anything back.
        Echo works a lot like print, except it has a child command.
        `,
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Println(&quot;Print: &quot; + strings.Join(args, &quot; &quot;))
        },
    }

    var cmdTimes = &amp;cobra.Command{
        Use:   &quot;times [# times] [string to echo]&quot;,
        Short: &quot;Echo anything to the screen more times&quot;,
        Long:  `echo things multiple times back to the user by providing
        a count and a string.`,
        Run: func(cmd *cobra.Command, args []string) {
            for i:=0; i &lt; echoTimes; i++ {
                fmt.Println(&quot;Echo: &quot; + strings.Join(args, &quot; &quot;))
            }
        },
    }

    cmdTimes.Flags().IntVarP(&amp;echoTimes, &quot;times&quot;, &quot;t&quot;, 1, &quot;times to echo the input&quot;)
    var rootCmd = &amp;cobra.Command{Use: &quot;app&quot;}
    rootCmd.AddCommand(cmdEcho)
    cmdEcho.AddCommand(cmdTimes)
    rootCmd.Execute()
}

答案1

得分: 1

如果times是一个命令,你不应该在前面加上'--'。

你可以在cobra_test.go中看到一些示例,展示了times的用法:

echo times -j 99 one two
echo times -s again -c test here
英文:

If times is a command, you are not suppose to prefix it with '--'.

The examples you can see in cobra_test.go shows times uses like:

echo times -j 99 one two
echo times -s again -c test here

答案2

得分: 0

例如,如果你的可执行文件名为 app,只需运行

$./app echo times -t 3 hello
 Echo: hello
 Echo: hello
 Echo: hello

或者

$./app echo times --times=3 hello
Echo: hello
Echo: hello
Echo: hello
英文:

for example, if your executed file named app, just run

$./app echo times -t 3 hello
 Echo: hello
 Echo: hello
 Echo: hello

or

$./app echo times --times=3 hello
Echo: hello
Echo: hello
Echo: hello

huangapple
  • 本文由 发表于 2014年12月16日 05:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/27493482.html
匿名

发表评论

匿名网友

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

确定