diff --git a/cli.go b/cli.go index 049b471..5ac02ca 100644 --- a/cli.go +++ b/cli.go @@ -21,8 +21,8 @@ var Action = ShowHelp func Run(args []string) { - set := flagSet(Flags) - set.Parse(args[1:]) + set := flagSet(Flags) + set.Parse(args[1:]) context := NewContext(set) if len(args) > 1 { diff --git a/cli_test.go b/cli_test.go index 2309a5f..0955590 100644 --- a/cli_test.go +++ b/cli_test.go @@ -1,22 +1,39 @@ package cli import ( - "reflect" - "testing" + "reflect" + "testing" ) -func Test_SimpleCLIFlags(t *testing.T) { - Flags = []Flag{ - StringFlag{"foo", "default", "a foo flag"}, - } - Action = func(c *Context) { - expect(t, c.String("foo"), "hello world") - } - Run([]string{ "command", "--foo", "hello world" }) +func Test_SettingFlags(t *testing.T) { + Flags = []Flag{ + StringFlag{"foo", "default", "a string flag"}, + IntFlag{"bar", 42, "an int flag"}, + BoolFlag{"bat", "a bool flag"}, + } + Action = func(c *Context) { + expect(t, c.String("foo"), "hello world") + expect(t, c.Int("bar"), 245) + expect(t, c.Bool("bat"), true) + } + Run([]string{"command", "--foo", "hello world", "--bar", "245", "--bat"}) } -/* Helpers */ +func Test_FlagDefaults(t *testing.T) { + Flags = []Flag{ + StringFlag{"foo", "default", "a string flag"}, + IntFlag{"bar", 42, "an int flag"}, + BoolFlag{"bat", "a bool flag"}, + } + Action = func(c *Context) { + expect(t, c.String("foo"), "default") + expect(t, c.Int("bar"), 42) + expect(t, c.Bool("bat"), false) + } + Run([]string{"command"}) +} +/* Test Helpers */ 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)) diff --git a/command_test.go b/command_test.go index 81c75c0..9730c6d 100644 --- a/command_test.go +++ b/command_test.go @@ -1,11 +1,11 @@ package cli import ( - "testing" + "testing" ) func Test_True(t *testing.T) { - expect(t, true, true) + expect(t, true, true) } func Test_Run(t *testing.T) { diff --git a/context_test.go b/context_test.go index 7df9793..042ddaa 100644 --- a/context_test.go +++ b/context_test.go @@ -1,36 +1,36 @@ package cli import ( - "flag" - "testing" + "flag" + "testing" ) func Test_Int(t *testing.T) { - set := flag.NewFlagSet("test", 0) - set.Int("myflag", 12, "doc") - c := NewContext(set) - expect(t, c.Int("myflag"), 12) + set := flag.NewFlagSet("test", 0) + set.Int("myflag", 12, "doc") + c := NewContext(set) + expect(t, c.Int("myflag"), 12) } func Test_String(t *testing.T) { - set := flag.NewFlagSet("test", 0) - set.String("myflag", "hello world", "doc") - c := NewContext(set) - expect(t, c.String("myflag"), "hello world") + set := flag.NewFlagSet("test", 0) + set.String("myflag", "hello world", "doc") + c := NewContext(set) + expect(t, c.String("myflag"), "hello world") } func Test_Bool(t *testing.T) { - set := flag.NewFlagSet("test", 0) - set.Bool("myflag", false, "doc") - c := NewContext(set) - expect(t, c.Bool("myflag"), false) + set := flag.NewFlagSet("test", 0) + set.Bool("myflag", false, "doc") + c := NewContext(set) + expect(t, c.Bool("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.Bool("myflag"), true) + 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.Bool("myflag"), true) } diff --git a/flag.go b/flag.go index 528f9d1..0e28ea0 100644 --- a/flag.go +++ b/flag.go @@ -4,41 +4,55 @@ import "fmt" import "flag" type Flag interface { - fmt.Stringer - Apply(*flag.FlagSet) -} - -type BoolFlag struct { - Name string - Usage string -} - -type StringFlag struct { - Name string - Value string - Usage string -} - -func (f StringFlag) String() string { - return fmt.Sprintf("--%v 'string'\t%v", f.Name, f.Usage) -} - -func (f StringFlag) Apply(set *flag.FlagSet) { - set.String(f.Name, f.Value, f.Usage) -} - -func (f BoolFlag) String() string { - return fmt.Sprintf("--%v\t%v", f.Name, f.Usage) -} - -func (f BoolFlag) Apply(set *flag.FlagSet) { - set.Bool(f.Name, false, f.Usage) + fmt.Stringer + Apply(*flag.FlagSet) } func flagSet(flags []Flag) *flag.FlagSet { - set := flag.NewFlagSet(Name, flag.ExitOnError) - for _, f := range flags { - f.Apply(set) - } - return set + set := flag.NewFlagSet(Name, flag.ExitOnError) + for _, f := range flags { + f.Apply(set) + } + return set +} + +type BoolFlag struct { + Name string + Usage string +} + +func (f BoolFlag) String() string { + return fmt.Sprintf("--%v\t%v", f.Name, f.Usage) +} + +func (f BoolFlag) Apply(set *flag.FlagSet) { + set.Bool(f.Name, false, f.Usage) +} + +type StringFlag struct { + Name string + Value string + Usage string +} + +func (f StringFlag) String() string { + return fmt.Sprintf("--%v '%v'\t%v", f.Name, f.Value, f.Usage) +} + +func (f StringFlag) Apply(set *flag.FlagSet) { + set.String(f.Name, f.Value, f.Usage) +} + +type IntFlag struct { + Name string + Value int + Usage string +} + +func (f IntFlag) String() string { + return fmt.Sprintf("--%v '%v'\t%v", f.Name, f.Value, f.Usage) +} + +func (f IntFlag) Apply(set *flag.FlagSet) { + set.Int(f.Name, f.Value, f.Usage) }