Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
kardolus
GitHub Repository: kardolus/chatgpt-cli
Path: blob/main/vendor/github.com/spf13/pflag/golangflag.go
2875 views
1
// Copyright 2009 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
5
package pflag
6
7
import (
8
goflag "flag"
9
"reflect"
10
"strings"
11
"time"
12
)
13
14
// go test flags prefixes
15
func isGotestFlag(flag string) bool {
16
return strings.HasPrefix(flag, "-test.")
17
}
18
19
func isGotestShorthandFlag(flag string) bool {
20
return strings.HasPrefix(flag, "test.")
21
}
22
23
// flagValueWrapper implements pflag.Value around a flag.Value. The main
24
// difference here is the addition of the Type method that returns a string
25
// name of the type. As this is generally unknown, we approximate that with
26
// reflection.
27
type flagValueWrapper struct {
28
inner goflag.Value
29
flagType string
30
}
31
32
// We are just copying the boolFlag interface out of goflag as that is what
33
// they use to decide if a flag should get "true" when no arg is given.
34
type goBoolFlag interface {
35
goflag.Value
36
IsBoolFlag() bool
37
}
38
39
func wrapFlagValue(v goflag.Value) Value {
40
// If the flag.Value happens to also be a pflag.Value, just use it directly.
41
if pv, ok := v.(Value); ok {
42
return pv
43
}
44
45
pv := &flagValueWrapper{
46
inner: v,
47
}
48
49
t := reflect.TypeOf(v)
50
if t.Kind() == reflect.Interface || t.Kind() == reflect.Ptr {
51
t = t.Elem()
52
}
53
54
pv.flagType = strings.TrimSuffix(t.Name(), "Value")
55
return pv
56
}
57
58
func (v *flagValueWrapper) String() string {
59
return v.inner.String()
60
}
61
62
func (v *flagValueWrapper) Set(s string) error {
63
return v.inner.Set(s)
64
}
65
66
func (v *flagValueWrapper) Type() string {
67
return v.flagType
68
}
69
70
// PFlagFromGoFlag will return a *pflag.Flag given a *flag.Flag
71
// If the *flag.Flag.Name was a single character (ex: `v`) it will be accessiblei
72
// with both `-v` and `--v` in flags. If the golang flag was more than a single
73
// character (ex: `verbose`) it will only be accessible via `--verbose`
74
func PFlagFromGoFlag(goflag *goflag.Flag) *Flag {
75
// Remember the default value as a string; it won't change.
76
flag := &Flag{
77
Name: goflag.Name,
78
Usage: goflag.Usage,
79
Value: wrapFlagValue(goflag.Value),
80
// Looks like golang flags don't set DefValue correctly :-(
81
//DefValue: goflag.DefValue,
82
DefValue: goflag.Value.String(),
83
}
84
// Ex: if the golang flag was -v, allow both -v and --v to work
85
if len(flag.Name) == 1 {
86
flag.Shorthand = flag.Name
87
}
88
if fv, ok := goflag.Value.(goBoolFlag); ok && fv.IsBoolFlag() {
89
flag.NoOptDefVal = "true"
90
}
91
return flag
92
}
93
94
// AddGoFlag will add the given *flag.Flag to the pflag.FlagSet
95
func (f *FlagSet) AddGoFlag(goflag *goflag.Flag) {
96
if f.Lookup(goflag.Name) != nil {
97
return
98
}
99
newflag := PFlagFromGoFlag(goflag)
100
f.AddFlag(newflag)
101
}
102
103
// AddGoFlagSet will add the given *flag.FlagSet to the pflag.FlagSet
104
func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet) {
105
if newSet == nil {
106
return
107
}
108
newSet.VisitAll(func(goflag *goflag.Flag) {
109
f.AddGoFlag(goflag)
110
})
111
if f.addedGoFlagSets == nil {
112
f.addedGoFlagSets = make([]*goflag.FlagSet, 0)
113
}
114
f.addedGoFlagSets = append(f.addedGoFlagSets, newSet)
115
}
116
117
// CopyToGoFlagSet will add all current flags to the given Go flag set.
118
// Deprecation remarks get copied into the usage description.
119
// Whenever possible, a flag gets added for which Go flags shows
120
// a proper type in the help message.
121
func (f *FlagSet) CopyToGoFlagSet(newSet *goflag.FlagSet) {
122
f.VisitAll(func(flag *Flag) {
123
usage := flag.Usage
124
if flag.Deprecated != "" {
125
usage += " (DEPRECATED: " + flag.Deprecated + ")"
126
}
127
128
switch value := flag.Value.(type) {
129
case *stringValue:
130
newSet.StringVar((*string)(value), flag.Name, flag.DefValue, usage)
131
case *intValue:
132
newSet.IntVar((*int)(value), flag.Name, *(*int)(value), usage)
133
case *int64Value:
134
newSet.Int64Var((*int64)(value), flag.Name, *(*int64)(value), usage)
135
case *uintValue:
136
newSet.UintVar((*uint)(value), flag.Name, *(*uint)(value), usage)
137
case *uint64Value:
138
newSet.Uint64Var((*uint64)(value), flag.Name, *(*uint64)(value), usage)
139
case *durationValue:
140
newSet.DurationVar((*time.Duration)(value), flag.Name, *(*time.Duration)(value), usage)
141
case *float64Value:
142
newSet.Float64Var((*float64)(value), flag.Name, *(*float64)(value), usage)
143
default:
144
newSet.Var(flag.Value, flag.Name, usage)
145
}
146
})
147
}
148
149
// ParseSkippedFlags explicitly Parses go test flags (i.e. the one starting with '-test.') with goflag.Parse(),
150
// since by default those are skipped by pflag.Parse().
151
// Typical usage example: `ParseGoTestFlags(os.Args[1:], goflag.CommandLine)`
152
func ParseSkippedFlags(osArgs []string, goFlagSet *goflag.FlagSet) error {
153
var skippedFlags []string
154
for _, f := range osArgs {
155
if isGotestFlag(f) {
156
skippedFlags = append(skippedFlags, f)
157
}
158
}
159
return goFlagSet.Parse(skippedFlags)
160
}
161
162
163