add simple test , fix errors and unused interface SliceFlag

main
Thesyncim 11 years ago
parent 4d9038a156
commit ed96efff1b

@ -84,3 +84,61 @@ func TestApp_CommandWithArgBeforeFlags(t *testing.T) {
expect(t, parsedOption, "my-option")
expect(t, firstArg, "my-arg")
}
func TestApp_ParseSliceFlags(t *testing.T) {
var parsedOption, firstArg string
var parsedIntSlice []int
var parsedStringSlice []string
app := cli.NewApp()
command := cli.Command{
Name: "cmd",
Flags: []cli.Flag{
cli.IntSliceFlag{"p", &cli.IntSlice{}, "set one or more ip addr"},
cli.StringSliceFlag{"ip", &cli.StringSlice{}, "set one or more ports to open"},
},
Action: func(c *cli.Context) {
parsedIntSlice = c.IntSlice("p")
parsedStringSlice = c.StringSlice("ip")
parsedOption = c.String("option")
firstArg = c.Args()[0]
},
}
app.Commands = []cli.Command{command}
app.Run([]string{"", "cmd", "my-arg", "-p", "22", "-p", "80", "-ip", "8.8.8.8", "-ip", "8.8.4.4"})
IntsEquals := func(a, b []int) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
StrsEquals := func(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
var expectedIntSlice = []int{22, 80}
var expectedStringSlice = []string{"8.8.8.8", "8.8.4.4"}
if !IntsEquals(parsedIntSlice, expectedIntSlice) {
t.Errorf("%s does not match %s", parsedIntSlice, expectedIntSlice)
}
if !StrsEquals(parsedStringSlice, expectedStringSlice) {
t.Errorf("%s does not match %s", parsedStringSlice, expectedStringSlice)
}
}

@ -34,10 +34,16 @@ func (c *Context) String(name string) string {
return c.lookupString(name, c.flagSet)
}
func (c *Context) StringSlice(name string) flag.Value {
// Looks up the value of a local string slice flag, returns nil if no string slice flag exists
func (c *Context) StringSlice(name string) []string {
return c.lookupStringSlice(name, c.flagSet)
}
// Looks up the value of a local int slice flag, returns nil if no int slice flag exists
func (c *Context) IntSlice(name string) []int {
return c.lookupIntSlice(name, c.flagSet)
}
// Looks up the value of a global int flag, returns 0 if no int flag exists
func (c *Context) GlobalInt(name string) int {
return c.lookupInt(name, c.globalSet)
@ -53,6 +59,16 @@ func (c *Context) GlobalString(name string) string {
return c.lookupString(name, c.globalSet)
}
// Looks up the value of a global string slice flag, returns nil if no string slice flag exists
func (c *Context) GlobalStringSlice(name string) []string {
return c.lookupStringSlice(name, c.globalSet)
}
// Looks up the value of a global int slice flag, returns nil if no int slice flag exists
func (c *Context) GlobalIntSlice(name string) []int {
return c.lookupIntSlice(name, c.globalSet)
}
func (c *Context) Args() []string {
return c.flagSet.Args()
}
@ -79,10 +95,21 @@ func (c *Context) lookupString(name string, set *flag.FlagSet) string {
return ""
}
func (c *Context) lookupStringSlice(name string, set *flag.FlagSet) flag.Value {
func (c *Context) lookupStringSlice(name string, set *flag.FlagSet) []string {
f := set.Lookup(name)
if f != nil {
return f.Value
return (f.Value.(*StringSlice)).Value()
}
return nil
}
func (c *Context) lookupIntSlice(name string, set *flag.FlagSet) []int {
f := set.Lookup(name)
if f != nil {
return (f.Value.(*IntSlice)).Value()
}
return nil

@ -2,16 +2,13 @@ package cli
import "fmt"
import "flag"
import "strconv"
type Flag interface {
fmt.Stringer
Apply(*flag.FlagSet)
}
type SliceFlag interface {
Value() []string
}
func flagSet(name string, flags []Flag) *flag.FlagSet {
set := flag.NewFlagSet(name, flag.ContinueOnError)
@ -23,17 +20,17 @@ func flagSet(name string, flags []Flag) *flag.FlagSet {
type StringSlice []string
func (i *StringSlice) Set(value string) error {
*i = append(*i, value)
func (f *StringSlice) Set(value string) error {
*f = append(*f, value)
return nil
}
func (i *StringSlice) String() string {
return fmt.Sprintf("%s", *i)
func (f *StringSlice) String() string {
return fmt.Sprintf("%s", *f)
}
func (i *StringSlice) Value() []string {
return *i
func (f *StringSlice) Value() []string {
return *f
}
type StringSliceFlag struct {
@ -50,6 +47,41 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) {
set.Var(f.Value, f.Name, f.Usage)
}
type IntSlice []int
func (f *IntSlice) Set(value string) error {
tmp, err := strconv.Atoi(value)
if err != nil {
return err
} else {
*f = append(*f, tmp)
}
return nil
}
func (f *IntSlice) String() string {
return fmt.Sprintf("%d", *f)
}
func (f *IntSlice) Value() []int {
return *f
}
type IntSliceFlag struct {
Name string
Value *IntSlice
Usage string
}
func (f IntSliceFlag) String() string {
return fmt.Sprintf("%s%v '%v'\t%v", prefixFor(f.Name), f.Name, "-"+f.Name+" option -"+f.Name+" option", f.Usage)
}
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
set.Var(f.Value, f.Name, f.Usage)
}
type BoolFlag struct {
Name string
Usage string

Loading…
Cancel
Save