add getfloat and gettime

master
Bel LaPointe 2019-03-24 10:54:44 -06:00
parent b78e8b1320
commit 65f9b3e4f7
2 changed files with 30 additions and 27 deletions

20
arg.go
View File

@ -67,6 +67,26 @@ func (a *Arg) GetDuration() time.Duration {
return a.Value.(time.Duration)
}
func (a *Arg) GetTime() time.Time {
if a.ArgType != TIME {
return time.Date(1995, 1, 1, 0, 0, 0, 0, time.UTC)
}
if _, ok := a.Value.(time.Time); !ok {
return time.Date(1995, 1, 1, 0, 0, 0, 0, time.UTC)
}
return a.Value.(time.Time)
}
func (a *Arg) GetFloat() float32 {
if a.ArgType != FLOAT {
return float32(-1)
}
if _, ok := a.Value.(float32); !ok {
return float32(-1)
}
return a.Value.(float32)
}
func (a *Arg) Set(value interface{}) error {
err := fmt.Errorf("incompatible set of type %T for arg $%v/-%v type %v", value, a.Env, a.Flag, a.ArgType)
switch a.ArgType {

View File

@ -22,22 +22,7 @@ func TestGets(t *testing.T) {
},
{
t: FLOAT,
v: "1.0",
ok: true,
},
{
t: FLOAT,
v: 1.0,
ok: true,
},
{
t: FLOAT,
v: 1,
ok: true,
},
{
t: FLOAT,
v: "1",
v: float32(1.0),
ok: true,
},
{
@ -47,12 +32,7 @@ func TestGets(t *testing.T) {
},
{
t: TIME,
v: "2019-03-22",
ok: true,
},
{
t: TIME,
v: "-24h",
v: time.Date(2019, 03, 22, 0, 0, 0, 0, time.UTC),
ok: true,
},
{
@ -116,6 +96,14 @@ func TestGets(t *testing.T) {
if (a.GetDuration() == c.v) != c.ok {
t.Errorf("failed to get duration: want %v, got %v", c.v, a.GetDuration())
}
case TIME:
if (a.GetTime() == c.v) != c.ok {
t.Errorf("failed to get time: want %v, got %v", c.v, a.GetTime())
}
case FLOAT:
if (a.GetFloat() == c.v) != c.ok {
t.Errorf("failed to get float: want %v, got %v", c.v, a.GetFloat())
}
}
}
}
@ -161,11 +149,6 @@ func TestSets(t *testing.T) {
v: "2019-03-22",
ok: true,
},
{
t: TIME,
v: "-24h",
ok: true,
},
{
t: STRING,
v: 5,