JMS #4: Getting some cli tests going. Testing out flags
This commit is contained in:
parent
04496e4364
commit
3f76896c4f
15
cli.go
15
cli.go
@ -22,11 +22,7 @@ var Action = ShowHelp
|
|||||||
func Run(args []string) {
|
func Run(args []string) {
|
||||||
|
|
||||||
set := flagSet(Flags)
|
set := flagSet(Flags)
|
||||||
err := set.Parse(args[1:])
|
set.Parse(args[1:])
|
||||||
if err != nil {
|
|
||||||
println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
context := NewContext(set)
|
context := NewContext(set)
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
@ -43,13 +39,4 @@ func Run(args []string) {
|
|||||||
Action(context)
|
Action(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Command struct {
|
|
||||||
Name string
|
|
||||||
ShortName string
|
|
||||||
Usage string
|
|
||||||
Description string
|
|
||||||
Action Handler
|
|
||||||
Flags []Flag
|
|
||||||
}
|
|
||||||
|
|
||||||
type Handler func(context *Context)
|
type Handler func(context *Context)
|
||||||
|
30
cli_test.go
Normal file
30
cli_test.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"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" })
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
10
command.go
Normal file
10
command.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
Name string
|
||||||
|
ShortName string
|
||||||
|
Usage string
|
||||||
|
Description string
|
||||||
|
Action Handler
|
||||||
|
Flags []Flag
|
||||||
|
}
|
12
command_test.go
Normal file
12
command_test.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_True(t *testing.T) {
|
||||||
|
expect(t, true, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_Run(t *testing.T) {
|
||||||
|
}
|
@ -17,7 +17,7 @@ func NewContext(flagSet *flag.FlagSet) *Context {
|
|||||||
return &Context{flagSet}
|
return &Context{flagSet}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) IntFlag(name string) int {
|
func (c *Context) Int(name string) int {
|
||||||
flag := c.flagSet.Lookup(name)
|
flag := c.flagSet.Lookup(name)
|
||||||
if flag != nil {
|
if flag != nil {
|
||||||
val, err := strconv.Atoi(flag.Value.String())
|
val, err := strconv.Atoi(flag.Value.String())
|
||||||
@ -30,7 +30,7 @@ func (c *Context) IntFlag(name string) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) BoolFlag(name string) bool {
|
func (c *Context) Bool(name string) bool {
|
||||||
flag := c.flagSet.Lookup(name)
|
flag := c.flagSet.Lookup(name)
|
||||||
if flag != nil {
|
if flag != nil {
|
||||||
val, err := strconv.ParseBool(flag.Value.String())
|
val, err := strconv.ParseBool(flag.Value.String())
|
||||||
@ -43,7 +43,7 @@ func (c *Context) BoolFlag(name string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) StringFlag(name string) string {
|
func (c *Context) String(name string) string {
|
||||||
flag := c.flagSet.Lookup(name)
|
flag := c.flagSet.Lookup(name)
|
||||||
if flag != nil {
|
if flag != nil {
|
||||||
return flag.Value.String()
|
return flag.Value.String()
|
||||||
|
@ -2,41 +2,28 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
func Test_Int(t *testing.T) {
|
||||||
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 := flag.NewFlagSet("test", 0)
|
||||||
set.Int("myflag", 12, "doc")
|
set.Int("myflag", 12, "doc")
|
||||||
c := NewContext(set)
|
c := NewContext(set)
|
||||||
expect(t, c.IntFlag("myflag"), 12)
|
expect(t, c.Int("myflag"), 12)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_StringFlag(t *testing.T) {
|
func Test_String(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.String("myflag", "hello world", "doc")
|
set.String("myflag", "hello world", "doc")
|
||||||
c := NewContext(set)
|
c := NewContext(set)
|
||||||
expect(t, c.StringFlag("myflag"), "hello world")
|
expect(t, c.String("myflag"), "hello world")
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_BoolFlag(t *testing.T) {
|
func Test_Bool(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
c := NewContext(set)
|
c := NewContext(set)
|
||||||
expect(t, c.BoolFlag("myflag"), false)
|
expect(t, c.Bool("myflag"), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_Args(t *testing.T) {
|
func Test_Args(t *testing.T) {
|
||||||
@ -45,5 +32,5 @@ func Test_Args(t *testing.T) {
|
|||||||
c := NewContext(set)
|
c := NewContext(set)
|
||||||
set.Parse([]string{"--myflag", "bat", "baz"})
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
||||||
expect(t, len(c.Args()), 2)
|
expect(t, len(c.Args()), 2)
|
||||||
expect(t, c.BoolFlag("myflag"), true)
|
expect(t, c.Bool("myflag"), true)
|
||||||
}
|
}
|
||||||
|
2
flag.go
2
flag.go
@ -36,7 +36,7 @@ func (f BoolFlag) Apply(set *flag.FlagSet) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func flagSet(flags []Flag) *flag.FlagSet {
|
func flagSet(flags []Flag) *flag.FlagSet {
|
||||||
set := flag.NewFlagSet(Name, 0)
|
set := flag.NewFlagSet(Name, flag.ExitOnError)
|
||||||
for _, f := range flags {
|
for _, f := range flags {
|
||||||
f.Apply(set)
|
f.Apply(set)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user