Rename Value.Value to Value.Target to correctly reflect its purpose.
This commit is contained in:
@@ -131,7 +131,7 @@ func buildField(k *Kong, node *Node, v reflect.Value, ft reflect.StructField, fv
|
||||
Default: tag.Default,
|
||||
Mapper: mapper,
|
||||
Tag: tag,
|
||||
Value: fv,
|
||||
Target: fv,
|
||||
|
||||
// Flags are optional by default, and args are required by default.
|
||||
Required: (!tag.Arg && tag.Required) || (tag.Arg && !tag.Optional),
|
||||
|
||||
@@ -197,7 +197,7 @@ func formatFlag(haveShort bool, flag *Flag) string {
|
||||
if !isBool {
|
||||
flagString += fmt.Sprintf("=%s", flag.FormatPlaceHolder())
|
||||
}
|
||||
if flag.Value.Value.Kind() == reflect.Slice {
|
||||
if flag.Value.Target.Kind() == reflect.Slice {
|
||||
flagString += " ..."
|
||||
}
|
||||
return flagString
|
||||
|
||||
@@ -90,7 +90,7 @@ func (k *Kong) extraFlags() []*Flag {
|
||||
Value: &Value{
|
||||
Name: "help",
|
||||
Help: "Show context-sensitive help.",
|
||||
Value: value,
|
||||
Target: value,
|
||||
Tag: &Tag{},
|
||||
Mapper: k.registry.ForValue(value),
|
||||
},
|
||||
@@ -152,9 +152,9 @@ func (k *Kong) applyHooks(ctx *Context) error {
|
||||
case trace.Command != nil:
|
||||
key = trace.Command.Target
|
||||
case trace.Positional != nil:
|
||||
key = trace.Positional.Value
|
||||
key = trace.Positional.Target
|
||||
case trace.Flag != nil:
|
||||
key = trace.Flag.Value.Value
|
||||
key = trace.Flag.Value.Target
|
||||
default:
|
||||
panic("unsupported Path")
|
||||
}
|
||||
|
||||
+4
-3
@@ -391,12 +391,13 @@ func TestDuplicateFlagChoosesLast(t *testing.T) {
|
||||
require.Equal(t, 2, cli.Flag)
|
||||
}
|
||||
|
||||
func TestDuplicateSliceDoesNotAccumulate(t *testing.T) {
|
||||
func TestDuplicateSliceAccumulates(t *testing.T) {
|
||||
var cli struct {
|
||||
Flag []int
|
||||
}
|
||||
|
||||
_, err := mustNew(t, &cli).Parse([]string{"--flag=1,2", "--flag=3,4"})
|
||||
args := []string{"--flag=1,2", "--flag=3,4"}
|
||||
_, err := mustNew(t, &cli).Parse(args)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []int{3, 4}, cli.Flag)
|
||||
require.Equal(t, []int{1, 2, 3, 4}, cli.Flag)
|
||||
}
|
||||
|
||||
@@ -226,6 +226,12 @@ func floatDecoder(bits int) MapperFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func mapDecoder(d *Registry) MapperFunc {
|
||||
return func(ctx *DecodeContext, target reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func sliceDecoder(d *Registry) MapperFunc {
|
||||
return func(ctx *DecodeContext, target reflect.Value) error {
|
||||
el := target.Type().Elem()
|
||||
@@ -242,7 +248,7 @@ func sliceDecoder(d *Registry) MapperFunc {
|
||||
if childDecoder == nil {
|
||||
return fmt.Errorf("no mapper for element type of %s", target.Type())
|
||||
}
|
||||
for childScanner.Peek().Type != EOLToken {
|
||||
for !childScanner.Peek().IsEOL() {
|
||||
childValue := reflect.New(el).Elem()
|
||||
err := childDecoder.Decode(ctx.WithScanner(childScanner), childValue)
|
||||
if err != nil {
|
||||
|
||||
@@ -139,7 +139,7 @@ type Value struct {
|
||||
Default string
|
||||
Mapper Mapper
|
||||
Tag *Tag
|
||||
Value reflect.Value
|
||||
Target reflect.Value
|
||||
Required bool
|
||||
Set bool // Set to true when this value is set through some mechanism.
|
||||
Format string // Formatting directive, if applicable.
|
||||
@@ -166,7 +166,7 @@ func (v *Value) Summary() string {
|
||||
|
||||
// IsCumulative returns true of the value is a slice.
|
||||
func (v *Value) IsCumulative() bool {
|
||||
return v.Value.Kind() == reflect.Slice
|
||||
return v.Target.Kind() == reflect.Slice
|
||||
}
|
||||
|
||||
// IsBool returns true if the underlying value is a boolean.
|
||||
@@ -174,12 +174,12 @@ func (v *Value) IsBool() bool {
|
||||
if m, ok := v.Mapper.(BoolMapper); ok && m.IsBool() {
|
||||
return true
|
||||
}
|
||||
return v.Value.Kind() == reflect.Bool
|
||||
return v.Target.Kind() == reflect.Bool
|
||||
}
|
||||
|
||||
// Parse tokens into value, parse, and validate, but do not write to the field.
|
||||
func (v *Value) Parse(scan *Scanner) (reflect.Value, error) {
|
||||
value := reflect.New(v.Value.Type()).Elem()
|
||||
value := reflect.New(v.Target.Type()).Elem()
|
||||
err := v.Mapper.Decode(&DecodeContext{Value: v, Scan: scan}, value)
|
||||
if err == nil {
|
||||
v.Set = true
|
||||
@@ -189,7 +189,7 @@ func (v *Value) Parse(scan *Scanner) (reflect.Value, error) {
|
||||
|
||||
// Apply value to field.
|
||||
func (v *Value) Apply(value reflect.Value) {
|
||||
v.Value.Set(value)
|
||||
v.Target.Set(value)
|
||||
v.Set = true
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ func (v *Value) Apply(value reflect.Value) {
|
||||
//
|
||||
// Does not include resolvers.
|
||||
func (v *Value) Reset() error {
|
||||
v.Value.Set(reflect.Zero(v.Value.Type()))
|
||||
v.Target.Set(reflect.Zero(v.Target.Type()))
|
||||
if v.Default != "" {
|
||||
value, err := v.Parse(Scan(v.Default))
|
||||
if err != nil {
|
||||
@@ -239,7 +239,7 @@ func (f *Flag) FormatPlaceHolder() string {
|
||||
tail += ", ..."
|
||||
}
|
||||
if f.Default != "" {
|
||||
if f.Value.Value.Kind() == reflect.String {
|
||||
if f.Value.Target.Kind() == reflect.String {
|
||||
return strconv.Quote(f.Default) + tail
|
||||
}
|
||||
return f.Default + tail
|
||||
|
||||
Reference in New Issue
Block a user