Добавлена возможность захвата параметров CLI.
All checks were successful
test / test (push) Successful in 58s
All checks were successful
test / test (push) Successful in 58s
This commit is contained in:
parent
207759e9e4
commit
9c66b33f45
37
cli.go
37
cli.go
@ -21,6 +21,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
@ -190,8 +191,8 @@ func NewCliWith(
|
|||||||
// Init analyzes the given structure interface, extracts cli definitions from
|
// Init analyzes the given structure interface, extracts cli definitions from
|
||||||
// its tag and installs command flagset by flag APIs. The interface must be a
|
// its tag and installs command flagset by flag APIs. The interface must be a
|
||||||
// structure pointer, otherwise will return an error
|
// structure pointer, otherwise will return an error
|
||||||
func (c *Command) Init(i interface{}) error {
|
func (c *Command) Init(in interface{}) error {
|
||||||
ptrRef := reflect.ValueOf(i)
|
ptrRef := reflect.ValueOf(in)
|
||||||
|
|
||||||
if ptrRef.IsNil() || ptrRef.Kind() != reflect.Ptr {
|
if ptrRef.IsNil() || ptrRef.Kind() != reflect.Ptr {
|
||||||
return fmt.Errorf("%w: %s",
|
return fmt.Errorf("%w: %s",
|
||||||
@ -207,6 +208,38 @@ func (c *Command) Init(i interface{}) error {
|
|||||||
return c.parseValue(valueOfStruct)
|
return c.parseValue(valueOfStruct)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Capture filter config args and return rest args
|
||||||
|
func (c *Command) Capture(args []string) []string {
|
||||||
|
var (
|
||||||
|
result []string
|
||||||
|
expectValue bool
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, arg := range args {
|
||||||
|
if !strings.HasPrefix(arg, "-") {
|
||||||
|
if expectValue {
|
||||||
|
c.Args = append(c.Args, arg)
|
||||||
|
expectValue = false
|
||||||
|
} else {
|
||||||
|
result = append(result, arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
name := strings.TrimPrefix(strings.TrimPrefix(arg, "-"), "-")
|
||||||
|
|
||||||
|
if c.FlagSet.Lookup(name) != nil {
|
||||||
|
c.Args = append(c.Args, arg)
|
||||||
|
expectValue = true
|
||||||
|
} else {
|
||||||
|
result = append(result, arg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// parseValue parses a reflect.Value object and extracts cli definitions
|
// parseValue parses a reflect.Value object and extracts cli definitions
|
||||||
func (c *Command) parseValue(value reflect.Value) error {
|
func (c *Command) parseValue(value reflect.Value) error {
|
||||||
var err error
|
var err error
|
||||||
|
@ -275,6 +275,9 @@ func TestDuration(t *testing.T) {
|
|||||||
cmd := NewCLI("Config")
|
cmd := NewCLI("Config")
|
||||||
assert.NoError(cmd.Init(&conf), "init config command")
|
assert.NoError(cmd.Init(&conf), "init config command")
|
||||||
|
|
||||||
args := []string{"--interval", "5s"}
|
args := []string{"-v", "--interval", "5s", "help"}
|
||||||
assert.NoError(cmd.Parse(args))
|
|
||||||
|
assert.ElementsMatch([]string{"-v", "help"}, cmd.Capture(args))
|
||||||
|
assert.ElementsMatch([]string{"--interval", "5s"}, cmd.Args)
|
||||||
|
assert.NoError(cmd.Parse(cmd.Args))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user