Skip to content

Commit

Permalink
✨ feat(cflag): new add cflag.KVString flag value
Browse files Browse the repository at this point in the history
- support parse k=v value to string map
  • Loading branch information
inhere committed Feb 25, 2023
1 parent 0cd4fda commit 8fe06c4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
61 changes: 59 additions & 2 deletions cflag/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"

"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/strutil/textutil"
Expand Down Expand Up @@ -71,6 +72,11 @@ type EnumString struct {
enum []string
}

// NewEnumString instance
func NewEnumString(enum ...string) EnumString {
return EnumString{enum: enum}
}

// String to string
func (s *EnumString) String() string {
return s.val
Expand All @@ -81,6 +87,12 @@ func (s *EnumString) SetEnum(enum []string) {
s.enum = enum
}

// WithEnum values
func (s *EnumString) WithEnum(enum []string) *EnumString {
s.enum = enum
return s
}

// Set new value, will check value is right
func (s *EnumString) Set(value string) error {
s.val = value
Expand Down Expand Up @@ -136,6 +148,52 @@ func (s *String) Ints(sep string) []int {
return strutil.Ints(string(*s), sep)
}

// KVString The kv-string flag, allow input multi.
// Implemented the flag.Value interface.
//
// Example:
//
// --var name=inhere => string map {name:inhere}
// --var name=inhere --var age=234 => string map {name:inhere, age:234}
type KVString struct {
maputil.SMap
Sep string
}

// NewKVString instance
func NewKVString() KVString {
return KVString{
Sep: comdef.EqualStr,
SMap: make(maputil.SMap),
}
}

// String to string
func (s *KVString) String() string {
return s.SMap.String()
}

// SetData value
func (s *KVString) SetData(mp map[string]string) {
s.SMap = mp
}

// Data map get
func (s *KVString) Data() maputil.SMap {
return s.SMap
}

// Set new value, will check value is right
func (s *KVString) Set(value string) error {
if value != "" {
key, val := strutil.SplitKV(value, s.Sep)
if key != "" {
s.SMap[key] = val
}
}
return nil
}

// ConfString The config-string flag, INI format, like nginx-config.
// Implemented the flag.Value interface.
//
Expand Down Expand Up @@ -166,9 +224,8 @@ func (s *ConfString) Data() maputil.SMap {
func (s *ConfString) Set(value string) error {
if value != "" {
s.val = value

// parse to map
mp, err := textutil.ParseInlineINI(value)

if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion cliutil/cmdline/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,15 @@ func NewParser(line string) *LineParser {
// ParseLine input command line text. alias of the StringToOSArgs()
func ParseLine(line string) []string {
p := &LineParser{Line: line}

return p.Parse()
}

// WithParseEnv with parse ENV var
func (p *LineParser) WithParseEnv() *LineParser {
p.ParseEnv = true
return p
}

// AlsoEnvParse input command line text to os.Args, will parse ENV var
func (p *LineParser) AlsoEnvParse() []string {
p.ParseEnv = true
Expand Down

0 comments on commit 8fe06c4

Please sign in to comment.