Path: blob/main/vendor/github.com/spf13/pflag/golangflag.go
2875 views
// Copyright 2009 The Go Authors. All rights reserved.1// Use of this source code is governed by a BSD-style2// license that can be found in the LICENSE file.34package pflag56import (7goflag "flag"8"reflect"9"strings"10"time"11)1213// go test flags prefixes14func isGotestFlag(flag string) bool {15return strings.HasPrefix(flag, "-test.")16}1718func isGotestShorthandFlag(flag string) bool {19return strings.HasPrefix(flag, "test.")20}2122// flagValueWrapper implements pflag.Value around a flag.Value. The main23// difference here is the addition of the Type method that returns a string24// name of the type. As this is generally unknown, we approximate that with25// reflection.26type flagValueWrapper struct {27inner goflag.Value28flagType string29}3031// We are just copying the boolFlag interface out of goflag as that is what32// they use to decide if a flag should get "true" when no arg is given.33type goBoolFlag interface {34goflag.Value35IsBoolFlag() bool36}3738func wrapFlagValue(v goflag.Value) Value {39// If the flag.Value happens to also be a pflag.Value, just use it directly.40if pv, ok := v.(Value); ok {41return pv42}4344pv := &flagValueWrapper{45inner: v,46}4748t := reflect.TypeOf(v)49if t.Kind() == reflect.Interface || t.Kind() == reflect.Ptr {50t = t.Elem()51}5253pv.flagType = strings.TrimSuffix(t.Name(), "Value")54return pv55}5657func (v *flagValueWrapper) String() string {58return v.inner.String()59}6061func (v *flagValueWrapper) Set(s string) error {62return v.inner.Set(s)63}6465func (v *flagValueWrapper) Type() string {66return v.flagType67}6869// PFlagFromGoFlag will return a *pflag.Flag given a *flag.Flag70// If the *flag.Flag.Name was a single character (ex: `v`) it will be accessiblei71// with both `-v` and `--v` in flags. If the golang flag was more than a single72// character (ex: `verbose`) it will only be accessible via `--verbose`73func PFlagFromGoFlag(goflag *goflag.Flag) *Flag {74// Remember the default value as a string; it won't change.75flag := &Flag{76Name: goflag.Name,77Usage: goflag.Usage,78Value: wrapFlagValue(goflag.Value),79// Looks like golang flags don't set DefValue correctly :-(80//DefValue: goflag.DefValue,81DefValue: goflag.Value.String(),82}83// Ex: if the golang flag was -v, allow both -v and --v to work84if len(flag.Name) == 1 {85flag.Shorthand = flag.Name86}87if fv, ok := goflag.Value.(goBoolFlag); ok && fv.IsBoolFlag() {88flag.NoOptDefVal = "true"89}90return flag91}9293// AddGoFlag will add the given *flag.Flag to the pflag.FlagSet94func (f *FlagSet) AddGoFlag(goflag *goflag.Flag) {95if f.Lookup(goflag.Name) != nil {96return97}98newflag := PFlagFromGoFlag(goflag)99f.AddFlag(newflag)100}101102// AddGoFlagSet will add the given *flag.FlagSet to the pflag.FlagSet103func (f *FlagSet) AddGoFlagSet(newSet *goflag.FlagSet) {104if newSet == nil {105return106}107newSet.VisitAll(func(goflag *goflag.Flag) {108f.AddGoFlag(goflag)109})110if f.addedGoFlagSets == nil {111f.addedGoFlagSets = make([]*goflag.FlagSet, 0)112}113f.addedGoFlagSets = append(f.addedGoFlagSets, newSet)114}115116// CopyToGoFlagSet will add all current flags to the given Go flag set.117// Deprecation remarks get copied into the usage description.118// Whenever possible, a flag gets added for which Go flags shows119// a proper type in the help message.120func (f *FlagSet) CopyToGoFlagSet(newSet *goflag.FlagSet) {121f.VisitAll(func(flag *Flag) {122usage := flag.Usage123if flag.Deprecated != "" {124usage += " (DEPRECATED: " + flag.Deprecated + ")"125}126127switch value := flag.Value.(type) {128case *stringValue:129newSet.StringVar((*string)(value), flag.Name, flag.DefValue, usage)130case *intValue:131newSet.IntVar((*int)(value), flag.Name, *(*int)(value), usage)132case *int64Value:133newSet.Int64Var((*int64)(value), flag.Name, *(*int64)(value), usage)134case *uintValue:135newSet.UintVar((*uint)(value), flag.Name, *(*uint)(value), usage)136case *uint64Value:137newSet.Uint64Var((*uint64)(value), flag.Name, *(*uint64)(value), usage)138case *durationValue:139newSet.DurationVar((*time.Duration)(value), flag.Name, *(*time.Duration)(value), usage)140case *float64Value:141newSet.Float64Var((*float64)(value), flag.Name, *(*float64)(value), usage)142default:143newSet.Var(flag.Value, flag.Name, usage)144}145})146}147148// ParseSkippedFlags explicitly Parses go test flags (i.e. the one starting with '-test.') with goflag.Parse(),149// since by default those are skipped by pflag.Parse().150// Typical usage example: `ParseGoTestFlags(os.Args[1:], goflag.CommandLine)`151func ParseSkippedFlags(osArgs []string, goFlagSet *goflag.FlagSet) error {152var skippedFlags []string153for _, f := range osArgs {154if isGotestFlag(f) {155skippedFlags = append(skippedFlags, f)156}157}158return goFlagSet.Parse(skippedFlags)159}160161162163