Исправлен метод захвата аргументов 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
9c66b33f45
commit
4b5c4706cb
5
cli.go
5
cli.go
@ -228,10 +228,11 @@ func (c *Command) Capture(args []string) []string {
|
||||
}
|
||||
|
||||
name := strings.TrimPrefix(strings.TrimPrefix(arg, "-"), "-")
|
||||
parts := strings.Split(name, "=")
|
||||
|
||||
if c.FlagSet.Lookup(name) != nil {
|
||||
if c.FlagSet.Lookup(parts[0]) != nil {
|
||||
c.Args = append(c.Args, arg)
|
||||
expectValue = true
|
||||
expectValue = len(parts) == 1
|
||||
} else {
|
||||
result = append(result, arg)
|
||||
}
|
||||
|
18
cli_test.go
18
cli_test.go
@ -280,4 +280,22 @@ func TestDuration(t *testing.T) {
|
||||
assert.ElementsMatch([]string{"-v", "help"}, cmd.Capture(args))
|
||||
assert.ElementsMatch([]string{"--interval", "5s"}, cmd.Args)
|
||||
assert.NoError(cmd.Parse(cmd.Args))
|
||||
assert.Equal(time.Second*5, conf.interval)
|
||||
}
|
||||
|
||||
func TestDurationWithEqual(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
var conf struct {
|
||||
interval time.Duration `cli:"interval" usage:"interval"`
|
||||
}
|
||||
|
||||
cmd := NewCLI("Config")
|
||||
assert.NoError(cmd.Init(&conf), "init config command")
|
||||
|
||||
args := []string{"-v", "--interval=5s", "help"}
|
||||
|
||||
assert.ElementsMatch([]string{"-v", "help"}, cmd.Capture(args))
|
||||
assert.ElementsMatch([]string{"--interval=5s"}, cmd.Args)
|
||||
assert.NoError(cmd.Parse(cmd.Args))
|
||||
assert.Equal(time.Second*5, conf.interval)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user