getopt: init module

This commit is contained in:
Jeremy Baxter 2026-03-19 14:07:07 +13:00
parent 8290a04536
commit 2729e58eab
17 changed files with 1076 additions and 3 deletions

39
getopt/duration.go Normal file
View file

@ -0,0 +1,39 @@
package getopt
import (
"time"
)
type durationVal time.Duration
func (d *durationVal) String() string {
return time.Duration(*d).String()
}
func (d *durationVal) Set(val string) error {
v, err := time.ParseDuration(val)
if err != nil {
return err
}
*d = durationVal(v)
return nil
}
// DurationVar defines a time.Duration flag with specified name, default
// value, and usage string. The argument p points to a time.Duration
// variable in which to store the value of the flag. The flag accepts a
// value acceptable to time.ParseDuration.
func (set *FlagSet) DurationVar(p *time.Duration, name string, value time.Duration, usage string) {
*p = value
set.Var((*durationVal)(p), name, usage)
}
// Duration defines a time.Duration flag with specified name, default
// value, and usage string. The return value is the address of a
// time.Duration variable that stores the value of the flag. The flag
// accepts a value acceptable to time.ParseDuration.
func (set *FlagSet) Duration(name string, value time.Duration, usage string) *time.Duration {
var d time.Duration
set.DurationVar(&d, name, value, usage)
return &d
}