time.go raw

   1  package pflag
   2  
   3  import (
   4  	"fmt"
   5  	"strings"
   6  	"time"
   7  )
   8  
   9  // TimeValue adapts time.Time for use as a flag.
  10  type timeValue struct {
  11  	*time.Time
  12  	formats []string
  13  }
  14  
  15  func newTimeValue(val time.Time, p *time.Time, formats []string) *timeValue {
  16  	*p = val
  17  	return &timeValue{
  18  		Time:    p,
  19  		formats: formats,
  20  	}
  21  }
  22  
  23  // Set time.Time value from string based on accepted formats.
  24  func (d *timeValue) Set(s string) error {
  25  	s = strings.TrimSpace(s)
  26  	for _, f := range d.formats {
  27  		v, err := time.Parse(f, s)
  28  		if err != nil {
  29  			continue
  30  		}
  31  		*d.Time = v
  32  		return nil
  33  	}
  34  
  35  	formatsString := ""
  36  	for i, f := range d.formats {
  37  		if i > 0 {
  38  			formatsString += ", "
  39  		}
  40  		formatsString += fmt.Sprintf("`%s`", f)
  41  	}
  42  
  43  	return fmt.Errorf("invalid time format `%s` must be one of: %s", s, formatsString)
  44  }
  45  
  46  // Type name for time.Time flags.
  47  func (d *timeValue) Type() string {
  48  	return "time"
  49  }
  50  
  51  func (d *timeValue) String() string { return d.Time.Format(time.RFC3339Nano) }
  52  
  53  // GetTime return the time value of a flag with the given name
  54  func (f *FlagSet) GetTime(name string) (time.Time, error) {
  55  	flag := f.Lookup(name)
  56  	if flag == nil {
  57  		err := fmt.Errorf("flag accessed but not defined: %s", name)
  58  		return time.Time{}, err
  59  	}
  60  
  61  	if flag.Value.Type() != "time" {
  62  		err := fmt.Errorf("trying to get %s value of flag of type %s", "time", flag.Value.Type())
  63  		return time.Time{}, err
  64  	}
  65  
  66  	val, ok := flag.Value.(*timeValue)
  67  	if !ok {
  68  		return time.Time{}, fmt.Errorf("value %s is not a time", flag.Value)
  69  	}
  70  
  71  	return *val.Time, nil
  72  }
  73  
  74  // TimeVar defines a time.Time flag with specified name, default value, and usage string.
  75  // The argument p points to a time.Time variable in which to store the value of the flag.
  76  func (f *FlagSet) TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {
  77  	f.TimeVarP(p, name, "", value, formats, usage)
  78  }
  79  
  80  // TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
  81  func (f *FlagSet) TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {
  82  	f.VarP(newTimeValue(value, p, formats), name, shorthand, usage)
  83  }
  84  
  85  // TimeVar defines a time.Time flag with specified name, default value, and usage string.
  86  // The argument p points to a time.Time variable in which to store the value of the flag.
  87  func TimeVar(p *time.Time, name string, value time.Time, formats []string, usage string) {
  88  	CommandLine.TimeVarP(p, name, "", value, formats, usage)
  89  }
  90  
  91  // TimeVarP is like TimeVar, but accepts a shorthand letter that can be used after a single dash.
  92  func TimeVarP(p *time.Time, name, shorthand string, value time.Time, formats []string, usage string) {
  93  	CommandLine.VarP(newTimeValue(value, p, formats), name, shorthand, usage)
  94  }
  95  
  96  // Time defines a time.Time flag with specified name, default value, and usage string.
  97  // The return value is the address of a time.Time variable that stores the value of the flag.
  98  func (f *FlagSet) Time(name string, value time.Time, formats []string, usage string) *time.Time {
  99  	return f.TimeP(name, "", value, formats, usage)
 100  }
 101  
 102  // TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
 103  func (f *FlagSet) TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {
 104  	p := new(time.Time)
 105  	f.TimeVarP(p, name, shorthand, value, formats, usage)
 106  	return p
 107  }
 108  
 109  // Time defines a time.Time flag with specified name, default value, and usage string.
 110  // The return value is the address of a time.Time variable that stores the value of the flag.
 111  func Time(name string, value time.Time, formats []string, usage string) *time.Time {
 112  	return CommandLine.TimeP(name, "", value, formats, usage)
 113  }
 114  
 115  // TimeP is like Time, but accepts a shorthand letter that can be used after a single dash.
 116  func TimeP(name, shorthand string, value time.Time, formats []string, usage string) *time.Time {
 117  	return CommandLine.TimeP(name, shorthand, value, formats, usage)
 118  }
 119