2 回答

TA貢獻1871條經驗 獲得超13個贊
您可以通過查閱此鏈接來解決此問題。
簡而言之,您需要的是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()
// 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()
}
讓我們看看終端中發生了什么:
> ./cobraApp sub --test a
> [a]
- 2 回答
- 0 關注
- 102 瀏覽
添加回答
舉報