alecthomas / alecthomas/kong

`xor`, `required` and `and` don't work together

Aperta
#516 2 commenti 3 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Go
Stelle
3.2k
Fork
188
Merge medio
1g 1h
PR unite (30g)
2

Descrizione

If a `required` tag is attached to one of the flags, there is no error even when the flag is not passed.

Test case:
```go
func TestXorAndRequired(t *testing.T) {
var cli struct {
Hello string `xor:"hello" required:""`
One string `xor:"hello" and:"two"`
Two string `and:"two"`
}

p := mustNew(t, &cli)
_, err := p.Parse([]string{})
assert.EqualError(t, err, "missing flags: --hello=STRING or --one=STRING, --one=STRING and --two=STRING")
}
```

Error:
```console
$ go test ./...
--- FAIL: TestXorAndRequired (0.00s)
kong_test.go:1195: Expected an error
FAIL
FAIL github.com/alecthomas/kong 0.597s
FAIL
```

Full test case

```go
func TestXorAndRequired(t *testing.T) {
var cli struct {
Hello string `xor:"hello" required:""`
One string `xor:"hello" and:"two"`
Two string `and:"two"`
}

p := mustNew(t, &cli)
_, err := p.Parse([]string{})
assert.EqualError(t, err, "missing flags: --hello=STRING or --one=STRING, --one=STRING and --two=STRING")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--one=valOne"})
assert.EqualError(t, err, "--one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--two=valTwo"})
assert.EqualError(t, err, "--one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello", "--one=valOne"})
assert.EqualError(t, err, "--hello and --one can't be used together, --one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello", "--two=valTwo"})
assert.EqualError(t, err, "--one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello", "--one=valOne", "--two=valTwo"})
assert.EqualError(t, err, "--hello and --one can't be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello"})
assert.NoError(t, err)

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--one=valOne", "--two=valTwo"})
assert.NoError(t, err)
}

func TestXorAndRequiredMany(t *testing.T) {
var cli struct {
Hello string `required:"" xor:"hello"`
One string `required:"" xor:"hello" and:"two"`
Two string `required:"" and:"two"`
}

p := mustNew(t, &cli)
_, err := p.Parse([]string{})
assert.EqualError(t, err, "missing flags: --hello=STRING or --one=STRING, --one=STRING and --two=STRING")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--one=valOne"})
assert.EqualError(t, err, "--one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--two=valTwo"})
assert.EqualError(t, err, "--one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello", "--one=valOne"})
assert.EqualError(t, err, "--hello and --one can't be used together, --one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello", "--two=valTwo"})
assert.EqualError(t, err, "--one and --two must be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello", "--one=valOne", "--two=valTwo"})
assert.EqualError(t, err, "--hello and --one can't be used together")

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--hello=valHello"})
assert.NoError(t, err)

p = mustNew(t, &cli)
_, err = p.Parse([]string{"--one=valOne", "--two=valTwo"})
assert.NoError(t, err)
}
```

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.