Add support for a mapper interface directly on fields.

If a field implements the MapperValue interface that interface will be
used.
This commit is contained in:
Alec Thomas
2018-06-29 12:33:50 +10:00
parent 0bc26a865d
commit b2cab08684
4 changed files with 72 additions and 13 deletions
+19
View File
@@ -120,3 +120,22 @@ func TestSliceConsumesRemainingPositionalArgs(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []string{"ls", "-lart"}, cli.Remainder)
}
type mappedValue struct {
decoded string
}
func (m *mappedValue) Decode(ctx *kong.DecodeContext) error {
m.decoded = ctx.Scan.PopValue("mapped")
return nil
}
func TestMapperValue(t *testing.T) {
var cli struct {
Value mappedValue `arg:""`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"foo"})
require.NoError(t, err)
require.Equal(t, "foo", cli.Value.decoded)
}