github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/server/debug/pprofui/fakeflags.go (about)

     1  // Copyright 2018 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package pprofui
    12  
    13  import (
    14  	"github.com/google/pprof/driver"
    15  	"github.com/spf13/pflag"
    16  )
    17  
    18  // pprofFlags is a wrapper to satisfy pprof's client flag interface.
    19  // That interface is satisfied by what the standard flag package
    20  // offers, with some tweaks. In this package, we just want to specify
    21  // the command line args directly; pprofFlags lets us do that
    22  // essentially by mocking out `os.Args`. `pprof` will register all of
    23  // its flags via this struct, and then they get populated from `args`
    24  // below.
    25  type pprofFlags struct {
    26  	args []string // passed to Parse()
    27  	*pflag.FlagSet
    28  }
    29  
    30  var _ driver.FlagSet = &pprofFlags{}
    31  
    32  // ExtraUsage is part of the driver.FlagSet interface.
    33  func (pprofFlags) ExtraUsage() string {
    34  	return ""
    35  }
    36  
    37  // AddExtraUsage is part of the driver.FlagSet interface.
    38  func (pprofFlags) AddExtraUsage(eu string) {
    39  }
    40  
    41  func (f pprofFlags) StringList(o, d, c string) *[]*string {
    42  	return &[]*string{f.String(o, d, c)}
    43  }
    44  
    45  func (f pprofFlags) Parse(usage func()) []string {
    46  	f.FlagSet.Usage = usage
    47  	if err := f.FlagSet.Parse(f.args); err != nil {
    48  		panic(err)
    49  	}
    50  	return f.FlagSet.Args()
    51  }