diff --git a/cli.go b/cli.go index b864865..9d477d2 100644 --- a/cli.go +++ b/cli.go @@ -1,6 +1,5 @@ package cli -import "fmt" import "os" // The name of the program. Defaults to os.Args[0] diff --git a/options.go b/options.go new file mode 100644 index 0000000..20ccce5 --- /dev/null +++ b/options.go @@ -0,0 +1,30 @@ +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 new file mode 100644 index 0000000..ac4167a --- /dev/null +++ b/options_test.go @@ -0,0 +1,52 @@ +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) +}