如何在Go中使用cobra库以单行方式接受输入

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

How to accept input in a single line using cobra library in Go

问题

我正在使用Go语言和Cobra编写代码,当前我给出的输入是:

 Calc add 
           输入输入的数量
           2
           输入数字
           2
           4
 输出:和为:6

对于熟悉Cobra的人来说,Calc是我的项目,add是我使用的命令,我希望输入以Calc add N2 2 4的形式给出(在一行中),并显示输出,其中N是一个变量,用于标识输入的数量,2和4是要相加的数字。

ADD命令的代码:

package cmd

import (
	"fmt"

	"github.com/spf13/cobra"
)

// addCmd represents the add command
var addCmd = &cobra.Command{
	Use:   "add",
	Short: "给定数字的求和",
	
	Run: func(cmd *cobra.Command, args []string) {
		length := 0
    fmt.Println("输入输入的数量")
    fmt.Scanln(&length)
    fmt.Println("输入数字")
    numbers := make([]int, length)
    for i := 0; i < length; i++ {
        fmt.Scanln(&numbers[i])
    }
      fmt.Println(numbers)
		
      sum:=0

for _, numbers := range numbers {

sum += numbers

}

fmt.Println("和为:",sum)


 },
}

func init() {
	RootCmd.AddCommand(addCmd)


}
英文:

I am writing a code in go language using cobra, currently the input Im giving is :

 Calc add 
           Enter the Number of inputs
           2
           Enter the Numbers
           2
           4
 Output: Sum is : 6

In this those who are familiar with cobra, Calc is my project and add is the command Im using,I want the input to be given as Calc add N2 2 4( in a single line) and the output should be displayed, where N is a variable that identifies the Number of inputs and 2 4 are the numbers to be added.

CODE FOR THE ADD COMMAND:

package cmd

import (
	&quot;fmt&quot;

	&quot;github.com/spf13/cobra&quot;
)

// addCmd represents the add command
var addCmd = &amp;cobra.Command{
	Use:   &quot;add&quot;,
	Short: &quot;Addition value of given Numbers&quot;,
	
	Run: func(cmd *cobra.Command, args []string) {
		length := 0
    fmt.Println(&quot;Enter the number of inputs&quot;)
    fmt.Scanln(&amp;length)
    fmt.Println(&quot;Enter the inputs&quot;)
    numbers := make([]int, length)
    for i := 0; i &lt; length; i++ {
        fmt.Scanln(&amp;numbers[i])
    }
      fmt.Println(numbers)
		
      sum:=0

for _, numbers := range numbers {

sum += numbers

}

fmt.Println(&quot;The Sum :&quot;,sum)


 },
}

func init() {
	RootCmd.AddCommand(addCmd)


}

P

答案1

得分: 3

这将满足你的目的。在一个名为--input的标志中输入你的数字,将其他数字作为参数添加。

func NewCmd() *cobra.Command {
    var input int
    c := &cobra.Command{
        Use:   "add",
        Short: "给定数字的求和",
        Run: func(cmd *cobra.Command, args []string) {
            if len(args) != input {
                fmt.Println(fmt.Sprintf("你需要提供%v个数字来求和", input))
                os.Exit(1)
            }
            numbers := make([]int, input)
            for i := 0; i < input; i++ {
                num, _ := strconv.Atoi(args[i])
                numbers[i] = num
            }
            sum := 0
            for _, number := range numbers {
                sum += number
            }
            fmt.Println("求和结果:", sum)
        },
    }
    c.Flags().IntVar(&input, "input", 0, "输入的数字个数")
    return c
}

func init() {
    cmd := NewCmd()
    RootCmd.AddCommand(cmd)
}

输入:

Calc add --input=3 6 3 6

输出:
求和结果:15

英文:

This will fulfill your purpose. Take your number in a flag --input. Give other numbers to add as arguments.

func NewCmd() *cobra.Command {
	var input int
	c := &amp;cobra.Command{
		Use:   &quot;add&quot;,
		Short: &quot;Addition value of given Numbers&quot;,

		Run: func(cmd *cobra.Command, args []string) {
			if len(args) != input {
				fmt.Println(fmt.Sprintf(&quot;You need to provide %v number to sum up&quot;, input))
				os.Exit(1)
			}
			numbers := make([]int, input)
			for i := 0; i &lt; input; i++ {
				num, _ := strconv.Atoi(args[i])
				numbers[i] = num
			}
			sum := 0
			for _, numbers := range numbers {
				sum += numbers
			}
			fmt.Println(&quot;The Sum :&quot;, sum)
		},
	}
	c.Flags().IntVar(&amp;input, &quot;input&quot;, 0, &quot;Number of input&quot;)
	return c
}

func init() {
    cmd := NewCmd()
    RootCmd.AddCommand(cmd)
}

Input:

Calc add --input=3 6 3 6

Output:
The Sum : 15

huangapple
  • 本文由 发表于 2017年1月20日 13:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/41756540.html
匿名

发表评论

匿名网友

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

确定