Implement flag "resolvers". (#24)

* Propagate errors.
* Use junit test output.
* Expand role of DecodeContext to include Scanner.
* Inject resolved flags as Path elements in the Context.
  This allows all existing logic to apply seamlessly: hooks, required
flags, etc.
* Clarify that hooks can be called multiple times.
This commit is contained in:
Alec Thomas
2018-06-12 07:20:55 +10:00
committed by Gerald Kaszuba
parent 73064a687f
commit e9d88d6528
16 changed files with 579 additions and 58 deletions
+43 -1
View File
@@ -101,9 +101,19 @@ func TestFlagSlice(t *testing.T) {
require.Equal(t, []int{1, 2, 3}, cli.Slice)
}
func TestFlagSliceWithSeparator(t *testing.T) {
var cli struct {
Slice []string
}
parser := mustNew(t, &cli)
_, err := parser.Parse([]string{`--slice=a\,b,c`})
require.NoError(t, err)
require.Equal(t, []string{"a,b", "c"}, cli.Slice)
}
func TestArgSlice(t *testing.T) {
var cli struct {
Slice []int `kong:"arg"`
Slice []int `arg`
Flag bool
}
parser := mustNew(t, &cli)
@@ -113,6 +123,18 @@ func TestArgSlice(t *testing.T) {
require.Equal(t, true, cli.Flag)
}
func TestArgSliceWithSeparator(t *testing.T) {
var cli struct {
Slice []string `arg`
Flag bool
}
parser := mustNew(t, &cli)
_, err := parser.Parse([]string{"a,b", "c", "--flag"})
require.NoError(t, err)
require.Equal(t, []string{"a,b", "c"}, cli.Slice)
require.Equal(t, true, cli.Flag)
}
func TestUnsupportedFieldErrors(t *testing.T) {
var cli struct {
Keys map[string]string
@@ -356,3 +378,23 @@ func TestShort(t *testing.T) {
require.True(t, cli.Bool)
require.Equal(t, "hello", cli.String)
}
func TestDuplicateFlagChoosesLast(t *testing.T) {
var cli struct {
Flag int
}
_, err := mustNew(t, &cli).Parse([]string{"--flag=1", "--flag=2"})
require.NoError(t, err)
require.Equal(t, 2, cli.Flag)
}
func TestDuplicateSliceDoesNotAccumulate(t *testing.T) {
var cli struct {
Flag []int
}
_, err := mustNew(t, &cli).Parse([]string{"--flag=1,2", "--flag=3,4"})
require.NoError(t, err)
require.Equal(t, []int{3, 4}, cli.Flag)
}