// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package pflagimport (goflag)// flagValueWrapper implements pflag.Value around a flag.Value. The main// difference here is the addition of the Type method that returns a string// name of the type. As this is generally unknown, we approximate that with// reflection.type flagValueWrapper struct { inner goflag.Value flagType string}// We are just copying the boolFlag interface out of goflag as that is what// they use to decide if a flag should get "true" when no arg is given.type goBoolFlag interface {goflag.Value IsBoolFlag() bool}func wrapFlagValue( goflag.Value) Value {// If the flag.Value happens to also be a pflag.Value, just use it directly.if , := .(Value); {return } := &flagValueWrapper{inner: , } := reflect.TypeOf()if .Kind() == reflect.Interface || .Kind() == reflect.Ptr { = .Elem() } .flagType = strings.TrimSuffix(.Name(), "Value")return}func ( *flagValueWrapper) () string {return .inner.String()}func ( *flagValueWrapper) ( string) error {return .inner.Set()}func ( *flagValueWrapper) () string {return .flagType}// PFlagFromGoFlag will return a *pflag.Flag given a *flag.Flag// If the *flag.Flag.Name was a single character (ex: `v`) it will be accessiblei// with both `-v` and `--v` in flags. If the golang flag was more than a single// character (ex: `verbose`) it will only be accessible via `--verbose`func ( *goflag.Flag) *Flag {// Remember the default value as a string; it won't change. := &Flag{Name: .Name,Usage: .Usage,Value: wrapFlagValue(.Value),// Looks like golang flags don't set DefValue correctly :-( //DefValue: goflag.DefValue,DefValue: .Value.String(), }// Ex: if the golang flag was -v, allow both -v and --v to workiflen(.Name) == 1 { .Shorthand = .Name }if , := .Value.(goBoolFlag); && .IsBoolFlag() { .NoOptDefVal = "true" }return}// AddGoFlag will add the given *flag.Flag to the pflag.FlagSetfunc ( *FlagSet) ( *goflag.Flag) {if .Lookup(.Name) != nil {return } := PFlagFromGoFlag() .AddFlag()}// AddGoFlagSet will add the given *flag.FlagSet to the pflag.FlagSetfunc ( *FlagSet) ( *goflag.FlagSet) {if == nil {return } .VisitAll(func( *goflag.Flag) { .AddGoFlag() })if .addedGoFlagSets == nil { .addedGoFlagSets = make([]*goflag.FlagSet, 0) } .addedGoFlagSets = append(.addedGoFlagSets, )}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.