-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
127 lines (117 loc) · 3.12 KB
/
command.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/rglujing/gf.
//
// Package command provides console operations, like options/arguments reading.
package command
import (
"os"
"regexp"
"strings"
)
var (
defaultParsedArgs = make([]string, 0)
defaultParsedOptions = make(map[string]string)
argumentRegex = regexp.MustCompile(`^\-{1,2}([\w\?\.\-]+)(=){0,1}(.*)$`)
)
// Init does custom initialization.
func Init(args ...string) {
if len(args) == 0 {
if len(defaultParsedArgs) == 0 && len(defaultParsedOptions) == 0 {
args = os.Args
} else {
return
}
} else {
defaultParsedArgs = make([]string, 0)
defaultParsedOptions = make(map[string]string)
}
// Parsing os.Args with default algorithm.
for i := 0; i < len(args); {
array := argumentRegex.FindStringSubmatch(args[i])
if len(array) > 2 {
if array[2] == "=" {
defaultParsedOptions[array[1]] = array[3]
} else if i < len(args)-1 {
if len(args[i+1]) > 0 && args[i+1][0] == '-' {
// Eg: gf gen -d -n 1
defaultParsedOptions[array[1]] = array[3]
} else {
// Eg: gf gen -n 2
defaultParsedOptions[array[1]] = args[i+1]
i += 2
continue
}
} else {
// Eg: gf gen -h
defaultParsedOptions[array[1]] = array[3]
}
} else {
defaultParsedArgs = append(defaultParsedArgs, args[i])
}
i++
}
}
// GetOpt returns the option value named `name`.
func GetOpt(name string, def ...string) string {
Init()
if v, ok := defaultParsedOptions[name]; ok {
return v
}
if len(def) > 0 {
return def[0]
}
return ""
}
// GetOptAll returns all parsed options.
func GetOptAll() map[string]string {
Init()
return defaultParsedOptions
}
// ContainsOpt checks whether option named `name` exist in the arguments.
func ContainsOpt(name string) bool {
Init()
_, ok := defaultParsedOptions[name]
return ok
}
// GetArg returns the argument at `index`.
func GetArg(index int, def ...string) string {
Init()
if index < len(defaultParsedArgs) {
return defaultParsedArgs[index]
}
if len(def) > 0 {
return def[0]
}
return ""
}
// GetArgAll returns all parsed arguments.
func GetArgAll() []string {
Init()
return defaultParsedArgs
}
// GetOptWithEnv returns the command line argument of the specified `key`.
// If the argument does not exist, then it returns the environment variable with specified `key`.
// It returns the default value `def` if none of them exists.
//
// Fetching Rules:
// 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
// 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>;
func GetOptWithEnv(key string, def ...string) string {
cmdKey := strings.ToLower(strings.Replace(key, "_", ".", -1))
if ContainsOpt(cmdKey) {
return GetOpt(cmdKey)
} else {
envKey := strings.ToUpper(strings.Replace(key, ".", "_", -1))
if r, ok := os.LookupEnv(envKey); ok {
return r
} else {
if len(def) > 0 {
return def[0]
}
}
}
return ""
}