From 80d3d863d9dade84a6ade0729923e87310d84253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bruno=20Franc=CC=A7a=20dos=20Reis?= Date: Sat, 11 Jun 2016 15:41:24 -0700 Subject: [PATCH] Adding Int64 and GlobalInt64 in context, plus related tests --- context.go | 26 ++++++++++++++++++++++++++ context_test.go | 19 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/context.go b/context.go index c342463..112dac3 100644 --- a/context.go +++ b/context.go @@ -31,6 +31,11 @@ func (c *Context) Int(name string) int { 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 // time.Duration flag exists func (c *Context) Duration(name string) time.Duration { @@ -84,6 +89,14 @@ func (c *Context) GlobalInt(name string) int { 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) // if no float64 flag exists func (c *Context) GlobalFloat64(name string) float64 { @@ -316,6 +329,19 @@ func lookupInt(name string, set *flag.FlagSet) int { 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 { f := set.Lookup(name) if f != nil { diff --git a/context_test.go b/context_test.go index 28d4884..7625c5b 100644 --- a/context_test.go +++ b/context_test.go @@ -9,17 +9,21 @@ import ( func TestNewContext(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Int("myflag", 12, "doc") + set.Int("myflagInt64", 12, "doc") set.Float64("myflag64", float64(17), "doc") globalSet := flag.NewFlagSet("test", 0) globalSet.Int("myflag", 42, "doc") + globalSet.Int("myflagInt64", 42, "doc") globalSet.Float64("myflag64", float64(47), "doc") globalCtx := NewContext(nil, globalSet, nil) command := Command{Name: "mycommand"} c := NewContext(nil, set, globalCtx) c.Command = command expect(t, c.Int("myflag"), 12) + expect(t, c.Int64("myflagInt64"), int64(12)) expect(t, c.Float64("myflag64"), float64(17)) expect(t, c.GlobalInt("myflag"), 42) + expect(t, c.GlobalInt64("myflagInt64"), int64(42)) expect(t, c.GlobalFloat64("myflag64"), float64(47)) expect(t, c.Command.Name, "mycommand") } @@ -31,6 +35,13 @@ func TestContext_Int(t *testing.T) { 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) { set := flag.NewFlagSet("test", 0) set.Int("myflag", 12, "doc") @@ -39,6 +50,14 @@ func TestContext_GlobalInt(t *testing.T) { 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) { set := flag.NewFlagSet("test", 0) set.Float64("myflag", float64(17), "doc")