1 回答
TA贡献1802条经验 获得超5个赞
每个子命令都可以有自己的标志。您可以按以下方式执行此操作:
package main
import (
"fmt"
"log"
"github.com/spf13/cobra"
)
var rootCmd = &cobra.Command{
Use: "app",
}
func NewCmdRest() *cobra.Command {
var env string
var restCmd = &cobra.Command{
Use: "rest",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("rest:", env)
},
}
restCmd.Flags().StringVarP(&env, "env", "e", "", "environment")
return restCmd
}
func NewCmdEvent() *cobra.Command {
var env string
var eventCmd = &cobra.Command{
Use: "event",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("event:", env)
},
}
eventCmd.Flags().StringVarP(&env, "env", "e", "", "environment")
return eventCmd
}
func init() {
rootCmd.AddCommand(NewCmdRest())
rootCmd.AddCommand(NewCmdEvent())
}
func main() {
if err := rootCmd.Execute(); err != nil {
log.Fatal(err)
}
}
- 1 回答
- 0 关注
- 114 浏览
添加回答
举报