-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
62 lines (56 loc) · 1.61 KB
/
option.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
package cliz
import (
"strings"
)
type (
// Option is the interface for the option.
Option interface {
// GetName returns the name of the option.
GetName() string
// GetAliases returns the alias names of the option.
GetAliases() []string
// GetEnv returns the environment variable name of the option.
GetEnv() string
// GetDefault returns the default value of the option.
GetDefault() interface{}
// IsRequired returns the required flag of the option.
IsRequired() bool
// IsZero returns whether the option has a value.
IsZero() bool
// IsHidden returns the hidden flag of the option.
IsHidden() bool
// GetDescription returns the description of the option.
GetDescription() string
}
)
// --long or -s
func argIsHyphenOption(o Option, osArg string) bool {
return osArg == longOptionPrefix+o.GetName() ||
func() bool {
for _, alias := range o.GetAliases() {
if osArg == shortOptionPrefix+alias || osArg == longOptionPrefix+alias {
return true
}
}
return false
}()
}
// --long=value or -s=value
func argIsHyphenOptionEqual(o Option, osArg string) bool {
return strings.HasPrefix(osArg, longOptionPrefix+o.GetName()+"=") ||
func() bool {
for _, alias := range o.GetAliases() {
if strings.HasPrefix(osArg, shortOptionPrefix+alias+"=") || strings.HasPrefix(osArg, longOptionPrefix+alias+"=") {
return true
}
}
return false
}()
}
func extractValueFromHyphenOptionEqual(osArg string) string {
return strings.Join(strings.Split(osArg, "=")[1:], "=")
}
func hasNoOptionValue(osArgs []string, i int) bool {
lastIndex := len(osArgs) - 1
return i+1 > lastIndex
}