Sorry but I cannot find a way to use the example
./client echo --times 10 "coucou"
./client --times 10 "coucou" echo
No way to use it... sorry for my mistake.
Best regards, 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()
}
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
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