-
Notifications
You must be signed in to change notification settings - Fork 0
/
gengf.go
91 lines (79 loc) · 1.91 KB
/
gengf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"github.com/gioco-play/goctl-gfrpc/rpcx"
"github.com/gioco-play/goctl-gfrpc/tplx"
"github.com/logrusorgru/aurora"
"github.com/spf13/cobra"
"os"
"strings"
)
const (
codeFailure = 1
dash = "-"
doubleDash = "--"
assign = "="
)
var (
rootCmd = &cobra.Command{
Use: "goctl-gfrpc",
Short: "A cli tool to generate go-zero code",
Long: "A cli tool to generate api, zrpc, model code\n\n" +
"GitHub: https://github.com/zeromicro/go-zero\n" +
"Site: https://go-zero.dev",
}
)
func init() {
rootCmd.AddCommand(tplx.Cmd)
rootCmd.AddCommand(rpcx.Cmd)
}
func supportGoStdFlag(args []string) []string {
copyArgs := append([]string(nil), args...)
parentCmd, _, err := rootCmd.Traverse(args[:1])
if err != nil { // ignore it to let cobra handle the error.
return copyArgs
}
for idx, arg := range copyArgs[0:] {
parentCmd, _, err = parentCmd.Traverse([]string{arg})
if err != nil { // ignore it to let cobra handle the error.
break
}
if !strings.HasPrefix(arg, dash) {
continue
}
flagExpr := strings.TrimPrefix(arg, doubleDash)
flagExpr = strings.TrimPrefix(flagExpr, dash)
flagName, flagValue := flagExpr, ""
assignIndex := strings.Index(flagExpr, assign)
if assignIndex > 0 {
flagName = flagExpr[:assignIndex]
flagValue = flagExpr[assignIndex:]
}
if !isBuiltin(flagName) {
// The method Flag can only match the user custom flags.
f := parentCmd.Flag(flagName)
if f == nil {
continue
}
if f.Shorthand == flagName {
continue
}
}
goStyleFlag := doubleDash + flagName
if assignIndex > 0 {
goStyleFlag += flagValue
}
copyArgs[idx] = goStyleFlag
}
return copyArgs
}
func isBuiltin(name string) bool {
return name == "version" || name == "help"
}
func main() {
os.Args = supportGoStdFlag(os.Args)
if err := rootCmd.Execute(); err != nil {
fmt.Println(aurora.Red(err.Error()))
os.Exit(codeFailure)
}
}