Adding Int64 and GlobalInt64 in context, plus related tests

This commit is contained in:
Bruno França dos Reis 2016-06-11 15:41:24 -07:00
parent 4962423cba
commit 80d3d863d9
2 changed files with 45 additions and 0 deletions

View File

@ -31,6 +31,11 @@ func (c *Context) Int(name string) int {
return lookupInt(name, c.flagSet) return lookupInt(name, c.flagSet)
} }
// Int64 looks up the value of a local int flag, returns 0 if no int flag exists
func (c *Context) Int64(name string) int64 {
return lookupInt64(name, c.flagSet)
}
// Duration looks up the value of a local time.Duration flag, returns 0 if no // Duration looks up the value of a local time.Duration flag, returns 0 if no
// time.Duration flag exists // time.Duration flag exists
func (c *Context) Duration(name string) time.Duration { func (c *Context) Duration(name string) time.Duration {
@ -84,6 +89,14 @@ func (c *Context) GlobalInt(name string) int {
return 0 return 0
} }
// GlobalInt64 looks up the value of a global int flag, returns 0 if no int flag exists
func (c *Context) GlobalInt64(name string) int64 {
if fs := lookupGlobalFlagSet(name, c); fs != nil {
return lookupInt64(name, fs)
}
return 0
}
// GlobalFloat64 looks up the value of a global float64 flag, returns float64(0) // GlobalFloat64 looks up the value of a global float64 flag, returns float64(0)
// if no float64 flag exists // if no float64 flag exists
func (c *Context) GlobalFloat64(name string) float64 { func (c *Context) GlobalFloat64(name string) float64 {
@ -316,6 +329,19 @@ func lookupInt(name string, set *flag.FlagSet) int {
return 0 return 0
} }
func lookupInt64(name string, set *flag.FlagSet) int64 {
f := set.Lookup(name)
if f != nil {
val, err := strconv.ParseInt(f.Value.String(), 10, 64)
if err != nil {
return 0
}
return val
}
return 0
}
func lookupDuration(name string, set *flag.FlagSet) time.Duration { func lookupDuration(name string, set *flag.FlagSet) time.Duration {
f := set.Lookup(name) f := set.Lookup(name)
if f != nil { if f != nil {

View File

@ -9,17 +9,21 @@ import (
func TestNewContext(t *testing.T) { func TestNewContext(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Int("myflag", 12, "doc") set.Int("myflag", 12, "doc")
set.Int("myflagInt64", 12, "doc")
set.Float64("myflag64", float64(17), "doc") set.Float64("myflag64", float64(17), "doc")
globalSet := flag.NewFlagSet("test", 0) globalSet := flag.NewFlagSet("test", 0)
globalSet.Int("myflag", 42, "doc") globalSet.Int("myflag", 42, "doc")
globalSet.Int("myflagInt64", 42, "doc")
globalSet.Float64("myflag64", float64(47), "doc") globalSet.Float64("myflag64", float64(47), "doc")
globalCtx := NewContext(nil, globalSet, nil) globalCtx := NewContext(nil, globalSet, nil)
command := Command{Name: "mycommand"} command := Command{Name: "mycommand"}
c := NewContext(nil, set, globalCtx) c := NewContext(nil, set, globalCtx)
c.Command = command c.Command = command
expect(t, c.Int("myflag"), 12) expect(t, c.Int("myflag"), 12)
expect(t, c.Int64("myflagInt64"), int64(12))
expect(t, c.Float64("myflag64"), float64(17)) expect(t, c.Float64("myflag64"), float64(17))
expect(t, c.GlobalInt("myflag"), 42) expect(t, c.GlobalInt("myflag"), 42)
expect(t, c.GlobalInt64("myflagInt64"), int64(42))
expect(t, c.GlobalFloat64("myflag64"), float64(47)) expect(t, c.GlobalFloat64("myflag64"), float64(47))
expect(t, c.Command.Name, "mycommand") expect(t, c.Command.Name, "mycommand")
} }
@ -31,6 +35,13 @@ func TestContext_Int(t *testing.T) {
expect(t, c.Int("myflag"), 12) expect(t, c.Int("myflag"), 12)
} }
func TestContext_Int64(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Int64("myflagInt64", 12, "doc")
c := NewContext(nil, set, nil)
expect(t, c.Int64("myflagInt64"), int64(12))
}
func TestContext_GlobalInt(t *testing.T) { func TestContext_GlobalInt(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Int("myflag", 12, "doc") set.Int("myflag", 12, "doc")
@ -39,6 +50,14 @@ func TestContext_GlobalInt(t *testing.T) {
expect(t, c.GlobalInt("nope"), 0) expect(t, c.GlobalInt("nope"), 0)
} }
func TestContext_GlobalInt64(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Int64("myflagInt64", 12, "doc")
c := NewContext(nil, set, nil)
expect(t, c.GlobalInt64("myflagInt64"), int64(12))
expect(t, c.GlobalInt64("nope"), int64(0))
}
func TestContext_Float64(t *testing.T) { func TestContext_Float64(t *testing.T) {
set := flag.NewFlagSet("test", 0) set := flag.NewFlagSet("test", 0)
set.Float64("myflag", float64(17), "doc") set.Float64("myflag", float64(17), "doc")