Skip to content

Commit

Permalink
✨ feat(cli,cflag): add add new flag var ConfString
Browse files Browse the repository at this point in the history
- ConfString - support auto parse input as config map
- cli - update some comments
  • Loading branch information
inhere committed Feb 19, 2023
1 parent dcc5261 commit 40a14e0
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
61 changes: 52 additions & 9 deletions cflag/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"fmt"
"strconv"

"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/maputil"
"github.com/gookit/goutil/strutil"
"github.com/gookit/goutil/strutil/textutil"
)

/*************************************************************************
Expand Down Expand Up @@ -80,15 +83,9 @@ func (s *EnumString) SetEnum(enum []string) {

// 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
}
}
s.val = value

if !ok {
if !arrutil.InStrings(value, s.enum) {
return fmt.Errorf("value must one of the: %v", s.enum)
}
return nil
Expand Down Expand Up @@ -124,12 +121,58 @@ func (s *String) String() string {
return string(*s)
}

// Strings split value to []string by sep ','
func (s *String) Strings() []string {
return strutil.Split(string(*s), ",")
}

// Split value to []string
func (s *String) Split(sep string) []string {
return strutil.ToStrings(string(*s), sep)
return strutil.Split(string(*s), sep)
}

// Ints value to []int
func (s *String) Ints(sep string) []int {
return strutil.Ints(string(*s), sep)
}

// ConfString The config-string flag, INI format, like nginx-config.
// Implemented the flag.Value interface.
//
// Example:
//
// --config 'k0=val0;k1=val1' => string map {k0:val0, k1:val1}
type ConfString struct {
maputil.SMap
val string
}

// String to string
func (s *ConfString) String() string {
return s.val
}

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

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

// Set new value, will check value is right
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
}
s.SMap = mp
}
return nil
}
36 changes: 36 additions & 0 deletions cflag/ext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cflag_test

import (
"testing"

"github.com/gookit/goutil/cflag"
"github.com/gookit/goutil/testutil/assert"
)

func TestEnumString_Set(t *testing.T) {
es := cflag.EnumString{}
es.SetEnum([]string{"php", "go"})

assert.Err(t, es.Set("no-match"))

assert.NoErr(t, es.Set("go"))
assert.Eq(t, "go", es.String())
}

func TestConfString_Set(t *testing.T) {
cs := cflag.ConfString{}
cs.SetData(map[string]string{"key": "val"})

assert.NotEmpty(t, cs.Data())
assert.Eq(t, "val", cs.Data().Str("key"))

assert.NoErr(t, cs.Set(""))
assert.Err(t, cs.Set("no-value"))

cs = cflag.ConfString{}
err := cs.Set("name=inhere;age=123")
assert.NoErr(t, err)
assert.NotEmpty(t, cs.Data())
assert.Eq(t, "inhere", cs.Str("name"))
assert.Eq(t, 123, cs.Int("age"))
}
2 changes: 1 addition & 1 deletion cflag/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func IsFlagHelpErr(err error) bool {
// regex: "`.+`"
var codeReg = regexp.MustCompile("`" + `.+` + "`")

// WrapColorForCode WrapColorForCode. convert "hello `keywords`" to "hello <mga>keywords</>"
// WrapColorForCode convert "hello `keywords`" to "hello <mga>keywords</>"
func WrapColorForCode(s string) string {
if !strings.ContainsRune(s, '`') {
return s
Expand Down
5 changes: 3 additions & 2 deletions cliutil/cmdline/parser.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package cmdline

import (
"os"
"os/exec"
"strings"

"github.com/gookit/goutil/internal/comfunc"
)

// LineParser struct
Expand Down Expand Up @@ -53,7 +54,7 @@ func (p *LineParser) Parse() []string {

// enable parse Env var
if p.ParseEnv {
p.Line = os.ExpandEnv(p.Line)
p.Line = comfunc.ParseEnvVar(p.Line, nil)
}

p.nodes = strings.Split(p.Line, " ")
Expand Down
7 changes: 7 additions & 0 deletions cliutil/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func BinName() string {
return path.Base(os.Args[0])
}

// exec: `stty -a 2>&1`
// const (
// mac: speed 9600 baud; 97 rows; 362 columns;
// macSttyMsgPattern = `(\d+)\s+rows;\s*(\d+)\s+columns;`
// linux: speed 38400 baud; rows 97; columns 362; line = 0;
// linuxSttyMsgPattern = `rows\s+(\d+);\s*columns\s+(\d+);`
// )
var terminalWidth, terminalHeight int

// GetTermSize for current console terminal.
Expand Down

0 comments on commit 40a14e0

Please sign in to comment.