add simple test , fix errors and unused interface SliceFlag
This commit is contained in:
52
flag.go
52
flag.go
@@ -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
|
||||
|
Reference in New Issue
Block a user