diff --git a/gcli.go b/gcli.go index ce8d528..282a067 100644 --- a/gcli.go +++ b/gcli.go @@ -17,6 +17,7 @@ import ( "strings" "github.com/gookit/gcli/v3/gflag" + "github.com/gookit/goutil/cflag" "github.com/gookit/goutil/envutil" ) @@ -243,16 +244,16 @@ func IsDebugMode() bool { return gOpts.Verbose >= VerbDebug } *************************************************************************/ // Ints The int flag list, implemented flag.Value interface -type Ints = gflag.Ints +type Ints = cflag.Ints // Strings The string flag list, implemented flag.Value interface -type Strings = gflag.Strings +type Strings = cflag.Strings // Booleans The bool flag list, implemented flag.Value interface -type Booleans = gflag.Booleans +type Booleans = cflag.Booleans // EnumString The string flag list, implemented flag.Value interface -type EnumString = gflag.EnumString +type EnumString = cflag.EnumString // String type, a special string // @@ -271,7 +272,7 @@ type EnumString = gflag.EnumString // // --names "23,34,56" // names.Ints(",") -> []int{23,34,56} -type String = gflag.String +type String = cflag.String /************************************************************************* * Verbose level diff --git a/gflag/ext_vars.go b/gflag/ext_vars.go deleted file mode 100644 index 6a4c8f5..0000000 --- a/gflag/ext_vars.go +++ /dev/null @@ -1,135 +0,0 @@ -package gflag - -import ( - "fmt" - "strconv" - - "github.com/gookit/goutil/strutil" -) - -/************************************************************************* - * options: some special flag vars - * - implemented flag.Value interface - *************************************************************************/ - -// Ints The int flag list, implemented flag.Value interface -type Ints []int - -// String to string -func (s *Ints) String() string { - return fmt.Sprintf("%v", *s) -} - -// Set new value -func (s *Ints) Set(value string) error { - intVal, err := strconv.Atoi(value) - if err == nil { - *s = append(*s, intVal) - } - - return err -} - -// Strings The string flag list, implemented flag.Value interface -type Strings []string - -// String to string -func (s *Strings) String() string { - return fmt.Sprintf("%v", *s) -} - -// Set new value -func (s *Strings) Set(value string) error { - *s = append(*s, value) - return nil -} - -// Booleans The bool flag list, implemented flag.Value interface -type Booleans []bool - -// String to string -func (s *Booleans) String() string { - return fmt.Sprintf("%v", *s) -} - -// Set new value -func (s *Booleans) Set(value string) error { - boolVal, err := strconv.ParseBool(value) - if err == nil { - *s = append(*s, boolVal) - } - - return err -} - -// EnumString The string flag list, implemented flag.Value interface -type EnumString struct { - val string - enum []string -} - -// String to string -func (s *EnumString) String() string { - return s.val -} - -// SetEnum values -func (s *EnumString) SetEnum(enum []string) { - s.enum = enum -} - -// Set new value, will check value is right -func (s *EnumString) Set(value string) error { - var ok bool - for _, item := range s.enum { - if value == item { - ok = true - break - } - } - - if !ok { - return fmt.Errorf("value must one of the: %v", s.enum) - } - return nil -} - -// String a special string -// -// Usage: -// -// // case 1: -// var names gcli.String -// c.VarOpt(&names, "names", "", "multi name by comma split") -// -// --names "tom,john,joy" -// names.Split(",") -> []string{"tom","john","joy"} -// -// // case 2: -// var ids gcli.String -// c.VarOpt(&ids, "ids", "", "multi id by comma split") -// -// --names "23,34,56" -// names.Ints(",") -> []int{23,34,56} -type String string - -// Set value -func (s *String) Set(val string) error { - *s = String(val) - return nil -} - -// String to string -func (s *String) String() string { - return string(*s) -} - -// Split value to []string -func (s *String) Split(sep string) []string { - return strutil.ToStrings(string(*s), sep) -} - -// Ints value to []int -func (s *String) Ints(sep string) []int { - return strutil.Ints(string(*s), sep) -} diff --git a/gflag/gflag.go b/gflag/gflag.go index 7d2ac02..d9ab155 100644 --- a/gflag/gflag.go +++ b/gflag/gflag.go @@ -2,6 +2,7 @@ package gflag import ( + "github.com/gookit/goutil/cflag" "github.com/gookit/goutil/strutil" ) @@ -50,3 +51,15 @@ type OptCategory struct { Name, Title string OptNames []string } + +// Ints The int flag list, implemented flag.Value interface +type Ints = cflag.Ints + +// Strings The string flag list, implemented flag.Value interface +type Strings = cflag.Strings + +// Booleans The bool flag list, implemented flag.Value interface +type Booleans = cflag.Booleans + +// EnumString The string flag list, implemented flag.Value interface +type EnumString = cflag.EnumString