-
Notifications
You must be signed in to change notification settings - Fork 1
/
flag.go
176 lines (148 loc) · 3.54 KB
/
flag.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package pflagfork
import (
"fmt"
"reflect"
"strings"
"github.com/carapace-sh/carapace/pkg/style"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// mode defines how flags are represented.
type mode int
const (
Default mode = iota // default behaviour
ShorthandOnly // only the shorthand should be used
NameAsShorthand // non-posix mode where the name is also added as shorthand (single `-` prefix)
)
type Flag struct {
*pflag.Flag
}
func (f Flag) Nargs() int {
if field := reflect.ValueOf(f.Flag).Elem().FieldByName("Nargs"); field.IsValid() && field.Kind() == reflect.Int {
return int(field.Int())
}
return 0
}
func (f Flag) Mode() mode {
if field := reflect.ValueOf(f.Flag).Elem().FieldByName("Mode"); field.IsValid() && field.Kind() == reflect.Int {
return mode(field.Int())
}
return Default
}
func (f Flag) OptargDelimiter() rune {
if field := reflect.ValueOf(f.Flag).Elem().FieldByName("OptargDelimiter"); field.IsValid() && field.Kind() == reflect.Int32 {
return (rune(field.Int()))
}
return '='
}
func (f Flag) IsRepeatable() bool {
if strings.Contains(f.Value.Type(), "Slice") ||
strings.Contains(f.Value.Type(), "Array") ||
f.Value.Type() == "count" {
return true
}
return false
}
func (f Flag) Split(arg string) (prefix, optarg string) {
delimiter := string(f.OptargDelimiter())
splitted := strings.SplitN(arg, delimiter, 2)
return splitted[0] + delimiter, splitted[1]
}
func (f Flag) Matches(arg string, posix bool) bool {
if !strings.HasPrefix(arg, "-") { // not a flag
return false
}
switch {
case strings.HasPrefix(arg, "--"):
name := strings.TrimPrefix(arg, "--")
name = strings.SplitN(name, string(f.OptargDelimiter()), 2)[0]
switch f.Mode() {
case ShorthandOnly, NameAsShorthand:
return false
default:
return name == f.Name
}
case !posix:
name := strings.TrimPrefix(arg, "-")
name = strings.SplitN(name, string(f.OptargDelimiter()), 2)[0]
if name == "" {
return false
}
switch f.Mode() {
case ShorthandOnly:
return name == f.Shorthand
default:
return name == f.Name || name == f.Shorthand
}
default:
if f.Shorthand != "" {
return strings.HasSuffix(arg, f.Shorthand)
}
return false
}
}
func (f Flag) TakesValue() bool {
switch f.Value.Type() {
case "bool", "boolSlice", "count":
return false
default:
return true
}
}
func (f Flag) IsOptarg() bool {
return f.NoOptDefVal != ""
}
func (f Flag) Style() string {
switch {
case !f.TakesValue():
return style.Carapace.FlagNoArg
case f.IsOptarg():
return style.Carapace.FlagOptArg
case f.Nargs() != 0:
return style.Carapace.FlagMultiArg
default:
return style.Carapace.FlagArg
}
}
func (f Flag) Required() bool {
if annotation := f.Annotations[cobra.BashCompOneRequiredFlag]; len(annotation) == 1 && annotation[0] == "true" {
return true
}
return false
}
func (f Flag) Definition() string {
var definition string
switch f.Mode() {
case ShorthandOnly:
definition = fmt.Sprintf("-%v", f.Shorthand)
case NameAsShorthand:
definition = fmt.Sprintf("-%v, -%v", f.Shorthand, f.Name)
default:
switch f.Shorthand {
case "":
definition = fmt.Sprintf("--%v", f.Name)
default:
definition = fmt.Sprintf("-%v, --%v", f.Shorthand, f.Name)
}
}
if f.Hidden {
definition += "&"
}
if f.Required() {
definition += "!"
}
if f.IsRepeatable() {
definition += "*"
}
switch {
case f.IsOptarg():
switch f.Value.Type() {
case "bool", "boolSlice", "count":
default:
definition += "?"
}
case f.TakesValue():
definition += "="
}
return definition
}