1// Copyright 2015 Google Inc.  All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package getopt
6
7import "time"
8
9type durationValue time.Duration
10
11func (d *durationValue) Set(value string, opt Option) error {
12	v, err := time.ParseDuration(value)
13	if err != nil {
14		return err
15	}
16	*d = durationValue(v)
17	return nil
18}
19
20func (d *durationValue) String() string {
21	return time.Duration(*d).String()
22}
23
24// Duration creates an option that parses its value as a time.Duration.
25func Duration(name rune, value time.Duration, helpvalue ...string) *time.Duration {
26	return CommandLine.Duration(name, value, helpvalue...)
27}
28
29func (s *Set) Duration(name rune, value time.Duration, helpvalue ...string) *time.Duration {
30	return s.DurationLong("", name, value, helpvalue...)
31}
32
33func DurationLong(name string, short rune, value time.Duration, helpvalue ...string) *time.Duration {
34	return CommandLine.DurationLong(name, short, value, helpvalue...)
35}
36
37func (s *Set) DurationLong(name string, short rune, value time.Duration, helpvalue ...string) *time.Duration {
38	s.DurationVarLong(&value, name, short, helpvalue...)
39	return &value
40}
41
42func DurationVar(p *time.Duration, name rune, helpvalue ...string) Option {
43	return CommandLine.DurationVar(p, name, helpvalue...)
44}
45
46func (s *Set) DurationVar(p *time.Duration, name rune, helpvalue ...string) Option {
47	return s.DurationVarLong(p, "", name, helpvalue...)
48}
49
50func DurationVarLong(p *time.Duration, name string, short rune, helpvalue ...string) Option {
51	return CommandLine.DurationVarLong(p, name, short, helpvalue...)
52}
53
54func (s *Set) DurationVarLong(p *time.Duration, name string, short rune, helpvalue ...string) Option {
55	return s.VarLong((*durationValue)(p), name, short, helpvalue...)
56}
57