diff --git a/cli.go b/cli.go index 9d477d2..ff06d75 100644 --- a/cli.go +++ b/cli.go @@ -48,7 +48,4 @@ type Command struct { Flags []Flag } -type Context struct { -} - type Handler func(context Context) diff --git a/context.go b/context.go new file mode 100644 index 0000000..9674d2f --- /dev/null +++ b/context.go @@ -0,0 +1,57 @@ +package cli + +import ( + "flag" + "strconv" +) + +// Context is a type that is passed through to +// each Handler action in a cli application. Context +// can be used to retrieve context-specific Args and +// parsed command-line options. +type Context struct { + flagSet *flag.FlagSet +} + +func NewContext(flagSet *flag.FlagSet) *Context { + return &Context{flagSet} +} + +func (c *Context) IntFlag(name string) int { + flag := c.flagSet.Lookup(name) + if flag != nil { + val, err := strconv.Atoi(flag.Value.String()) + if err != nil { + panic(err) + } + return val + } else { + return 0 + } +} + +func (c *Context) BoolFlag(name string) bool { + flag := c.flagSet.Lookup(name) + if flag != nil { + val, err := strconv.ParseBool(flag.Value.String()) + if err != nil { + panic(err) + } + return val + } else { + return false + } +} + +func (c *Context) StringFlag(name string) string { + flag := c.flagSet.Lookup(name) + if flag != nil { + return flag.Value.String() + } else { + return "" + } +} + +func (c *Context) Args() []string { + return c.flagSet.Args() +} diff --git a/context_test.go b/context_test.go new file mode 100644 index 0000000..1ce6a35 --- /dev/null +++ b/context_test.go @@ -0,0 +1,49 @@ +package cli + +import ( + "flag" + "reflect" + "testing" +) + +func expect(t *testing.T, a interface{}, b interface{}) { + if a != b { + t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} + +func refute(t *testing.T, a interface{}, b interface{}) { + if a == b { + t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} + +func Test_IntFlag(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Int("myflag", 12, "doc") + c := NewContext(set) + expect(t, c.IntFlag("myflag"), 12) +} + +func Test_StringFlag(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.String("myflag", "hello world", "doc") + c := NewContext(set) + expect(t, c.StringFlag("myflag"), "hello world") +} + +func Test_BoolFlag(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + c := NewContext(set) + expect(t, c.BoolFlag("myflag"), false) +} + +func Test_Args(t *testing.T) { + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + c := NewContext(set) + set.Parse([]string{"--myflag", "bat", "baz"}) + expect(t, len(c.Args()), 2) + expect(t, c.BoolFlag("myflag"), true) +} diff --git a/options.go b/options.go deleted file mode 100644 index 20ccce5..0000000 --- a/options.go +++ /dev/null @@ -1,30 +0,0 @@ -package cli - -type Options map[string] interface{} - -func (o Options) Int(key string) int { - val := o[key] - if val != nil { - return val.(int) - } else { - return 0 - } -} - -func (o Options) String(key string) string { - val := o[key] - if val != nil { - return val.(string) - } else { - return "" - } -} - -func (o Options) Bool(key string) bool { - val := o[key] - if val != nil { - return val.(bool) - } else { - return false - } -} diff --git a/options_test.go b/options_test.go deleted file mode 100644 index ac4167a..0000000 --- a/options_test.go +++ /dev/null @@ -1,52 +0,0 @@ -package cli - -import "testing" -import "reflect" - -func expect(t *testing.T, a interface{}, b interface{}) { - if a != b { - t.Errorf("Expected %v (%v) - Got %v (%v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) - } -} - -func refute(t *testing.T, a interface{}, b interface{}) { - if a == b { - t.Errorf("Did not expect %v (%v) - Got %v (%v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) - } -} - -func Test_Int(t *testing.T) { - opts := Options{ - "foo": 1, - "bar": 2, - "bat": 3, - } - - expect(t, opts.Int("foo"), 1) - expect(t, opts.Int("bar"), 2) - expect(t, opts.Int("bat"), 3) - refute(t, opts.Int("foo"), "1") - expect(t, opts.Int("nope"), 0) -} - -func Test_String(t *testing.T) { - opts := Options{ - "foo": "bar", - "bat": "baz", - } - - expect(t, opts.String("foo"), "bar") - expect(t, opts.String("bat"), "baz") - expect(t, opts.String("nope"), "") -} - -func Test_Bool(t *testing.T) { - opts := Options{ - "foo": false, - "bar": true, - } - - expect(t, opts.Bool("foo"), false) - expect(t, opts.Bool("bar"), true) - expect(t, opts.Bool("nope"), false) -}