github.com/haraldrudell/parl@v0.4.176/pflags/visited-options.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package pflags
     7  
     8  import "flag"
     9  
    10  type VisitedOptions struct {
    11  	m map[string]bool
    12  	s []string
    13  }
    14  
    15  func NewVisitedOptions() (o *VisitedOptions) { return &VisitedOptions{} }
    16  
    17  func (o *VisitedOptions) Map() (m map[string]bool) {
    18  	o.m = make(map[string]bool)
    19  	flag.Visit(o.flagVisitFunc)
    20  	m = o.m
    21  	o.m = nil
    22  	return
    23  }
    24  
    25  func (o *VisitedOptions) Slice() (s []string) {
    26  	flag.Visit(o.flagVisitFunc)
    27  	s = o.s
    28  	o.s = nil
    29  	return
    30  }
    31  
    32  func (o *VisitedOptions) flagVisitFunc(flagFlag *flag.Flag) {
    33  	if m := o.m; m != nil {
    34  		m[flagFlag.Name] = true
    35  	} else {
    36  		o.s = append(o.s, flagFlag.Name)
    37  	}
    38  }