TimestampFlag.ValueFromContext() as convenient accessor
This commit is contained in:
parent
6b336c478f
commit
889c7b5d7a
@ -2264,6 +2264,15 @@ func TestTimestampFlagApply_Fail_Parse_Wrong_Time(t *testing.T) {
|
||||
expect(t, err, fmt.Errorf("invalid value \"2006-01-02T15:04:05Z\" for flag -time: parsing time \"2006-01-02T15:04:05Z\" as \"Jan 2, 2006 at 3:04pm (MST)\": cannot parse \"2006-01-02T15:04:05Z\" as \"Jan\""))
|
||||
}
|
||||
|
||||
func TestTimestampFlagValueFromContext(t *testing.T) {
|
||||
set := flag.NewFlagSet("test", 0)
|
||||
now := time.Now()
|
||||
set.Var(NewTimestamp(now), "myflag", "doc")
|
||||
ctx := NewContext(nil, set, nil)
|
||||
f := &TimestampFlag{Name: "myflag"}
|
||||
expect(t, f.ValueFromContext(ctx), &now)
|
||||
}
|
||||
|
||||
type flagDefaultTestCase struct {
|
||||
name string
|
||||
flag Flag
|
||||
|
@ -164,6 +164,11 @@ func (f *TimestampFlag) Apply(set *flag.FlagSet) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValueFromContext returns the flag’s value in the given Context.
|
||||
func (f *TimestampFlag) ValueFromContext(ctx *Context) *time.Time {
|
||||
return ctx.Timestamp(f.Name)
|
||||
}
|
||||
|
||||
// Timestamp gets the timestamp from a flag name
|
||||
func (c *Context) Timestamp(name string) *time.Time {
|
||||
if fs := c.lookupFlagSet(name); fs != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user