英文:
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 (
"fmt"
"github.com/spf13/cobra"
)
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "Addition value of given Numbers",
Run: func(cmd *cobra.Command, args []string) {
length := 0
fmt.Println("Enter the number of inputs")
fmt.Scanln(&length)
fmt.Println("Enter the inputs")
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("The Sum :",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 := &cobra.Command{
Use: "add",
Short: "Addition value of given Numbers",
Run: func(cmd *cobra.Command, args []string) {
if len(args) != input {
fmt.Println(fmt.Sprintf("You need to provide %v number to sum up", 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 _, numbers := range numbers {
sum += numbers
}
fmt.Println("The Sum :", sum)
},
}
c.Flags().IntVar(&input, "input", 0, "Number of input")
return c
}
func init() {
cmd := NewCmd()
RootCmd.AddCommand(cmd)
}
Input:
Calc add --input=3 6 3 6
Output:
The Sum : 15
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论