Skip to content

Commit a41b2e8

Browse files
committed
fix: slices/maps of existingfile would fail to work
1 parent 8e675d6 commit a41b2e8

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

mapper.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ func sliceDecoder(r *Registry) MapperFunc {
540540
var childScanner *Scanner
541541
if ctx.Value.Flag != nil {
542542
t := ctx.Scan.Pop()
543-
// If decoding a flag, we need an value.
543+
// If decoding a flag, we need a value.
544544
if t.IsEOL() {
545545
return fmt.Errorf("missing value, expecting \"<arg>%c...\"", sep)
546546
}
@@ -641,7 +641,7 @@ func existingFileMapper(r *Registry) MapperFunc {
641641
return err
642642
}
643643

644-
if !ctx.Value.Active || ctx.Value.Set {
644+
if !ctx.Value.Active || (ctx.Value.Set && ctx.Value.Target.Type() == target.Type()) {
645645
// early return to avoid checking extra files that may not exist;
646646
// this hack only works because the value provided on the cli is
647647
// checked before the default value is checked (if default is set).
@@ -677,7 +677,7 @@ func existingDirMapper(r *Registry) MapperFunc {
677677
return err
678678
}
679679

680-
if !ctx.Value.Active || ctx.Value.Set {
680+
if !ctx.Value.Active || (ctx.Value.Set && ctx.Value.Target.Type() == target.Type()) {
681681
// early return to avoid checking extra dirs that may not exist;
682682
// this hack only works because the value provided on the cli is
683683
// checked before the default value is checked (if default is set).

mapper_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,20 @@ func TestExistingFileMapper(t *testing.T) {
553553
assert.Contains(t, err.Error(), "exists but is a directory")
554554
}
555555

556+
func TestExistingFileMapperSlice(t *testing.T) {
557+
type CLI struct {
558+
Files []string `type:"existingfile"`
559+
}
560+
var cli CLI
561+
p := mustNew(t, &cli)
562+
_, err := p.Parse([]string{"--files", "testdata/file.txt", "--files", "testdata/file.txt"})
563+
assert.NoError(t, err)
564+
assert.NotZero(t, cli.Files)
565+
pwd, err := os.Getwd()
566+
assert.NoError(t, err)
567+
assert.Equal(t, []string{filepath.Join(pwd, "testdata", "file.txt"), filepath.Join(pwd, "testdata", "file.txt")}, cli.Files)
568+
}
569+
556570
func TestExistingFileMapperDefaultMissing(t *testing.T) {
557571
type CLI struct {
558572
File string `type:"existingfile" default:"testdata/missing.txt"`

0 commit comments

Comments
 (0)