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.
13 // flagValueWrapper implements pflag.Value around a flag.Value. The main
14 // difference here is the addition of the Type method that returns a string
15 // name of the type. As this is generally unknown, we approximate that with
17 type flagValueWrapper struct {
22 // We are just copying the boolFlag interface out of goflag as that is what
23 // they use to decide if a flag should get "true" when no arg is given.
24 type goBoolFlag interface {
29 func wrapFlagValue(v goflag.Value) Value {
30 // If the flag.Value happens to also be a pflag.Value, just use it directly.
31 if pv, ok := v.(Value); ok {
35 pv := &flagValueWrapper{
39 t := reflect.TypeOf(v)
40 if t.Kind() == reflect.Interface || t.Kind() == reflect.Ptr {
44 pv.flagType = strings.TrimSuffix(t.Name(), "Value")
48 func (v *flagValueWrapper) String() string {
49 return v.inner.String()
52 func (v *flagValueWrapper) Set(s string) error {
56 func (v *flagValueWrapper) Type() string {
60 // PFlagFromGoFlag will return a *pflag.Flag given a *flag.Flag
61 // If the *flag.Flag.Name was a single character (ex: `v`) it will be accessiblei
62 // with both `-v` and `--v` in flags. If the golang flag was more than a single
63 // character (ex: `verbose`) it will only be accessible via `--verbose`
64 func PFlagFromGoFlag(goflag *goflag.Flag) *Flag {
65 // Remember the default value as a string; it won't change.
69 Value: wrapFlagValue(goflag.Value),
70 // Looks like golang flags don't set DefValue correctly :-(
71 //DefValue: goflag.DefValue,
72 DefValue: goflag.Value.String(),
74 // Ex: if the golang flag was -v, allow both -v and --v to work
75 if len(flag.Name) == 1 {
76 flag.Shorthand = flag.Name
78 if fv, ok := goflag.Value.(goBoolFlag); ok && fv.IsBoolFlag() {
79 flag.NoOptDefVal = "true"
84 // AddGoFlag will add the given *flag.Flag to the pflag.FlagSet
85 func (f *FlagSet) AddGoFlag(goflag *goflag.Flag) {
86 if f.Lookup(goflag.Name) != nil {
89 newflag := PFlagFromGoFlag(goflag)
93 // AddGoFlagSet will add the given *flag.FlagSet to the pflag.FlagSet
94 func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet) {
98 newSet.VisitAll(func(goflag *goflag.Flag) {
101 if f.addedGoFlagSets == nil {
102 f.addedGoFlagSets = make([]*goflag.FlagSet, 0)
104 f.addedGoFlagSets = append(f.addedGoFlagSets, newSet)