如何使用本地标志在 Cobra 中使用标志
package main
import (
"fmt"
"github.com/spf13/cobra"
)
func main() {
myCmd := &cobra.Command{
Use: "myaction",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 1 {
flags := cmd.Flags()
var strTmp string
flags.StringVarP(&strTmp, "test", "t", "", "Source directory to read from")
fmt.Println(strTmp)
}
},
}
myCmd.Execute()
}
错误
go run main.go myaction --test="hello"
Error: unknown flag: --test
Usage:
myaction [flags]
Flags:
-h, --help help for myaction