github.com/puellanivis/breton@v0.2.16/lib/gnuflag/duration.go (about)

     1  package gnuflag
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  // -- time.Duration Value
     8  type durationValue time.Duration
     9  
    10  func (d *durationValue) Set(s string) error {
    11  	v, err := time.ParseDuration(s)
    12  	*d = durationValue(v)
    13  	return err
    14  }
    15  
    16  func (d durationValue) Get() interface{} { return time.Duration(d) }
    17  
    18  func (d durationValue) String() string { return (time.Duration)(d).String() }
    19  
    20  // Duration defines a time.Duration flag with specified name, and usage string.
    21  // The return value is the address of a time.Duration variable that stores the value of the flag.
    22  // The flag accepts values acceptable to time.ParseDuration.
    23  func (f *FlagSet) Duration(name string, usage string, options ...Option) *time.Duration {
    24  	p := new(time.Duration)
    25  	if err := f.Var((*durationValue)(p), name, usage, options...); err != nil {
    26  		panic(err)
    27  	}
    28  	return p
    29  }
    30  
    31  // Duration defines a time.Duration flag with specified name, and usage string.
    32  // The return value is the address of a time.Duration variable that stores the value of the flag.
    33  // The flag accepts values acceptable to time.ParseDuration.
    34  func Duration(name string, usage string, options ...Option) *time.Duration {
    35  	return CommandLine.Duration(name, usage, options...)
    36  }