From fcce511478c1aabd0e4a6af313acd74a89aaa21c Mon Sep 17 00:00:00 2001 From: Erin Call Date: Tue, 13 Oct 2020 12:55:44 -0700 Subject: [PATCH] Refactor fishAddFileFlag for better flexibility [#1156] Performing reflection on the given flag ensures that fishAddFileFlag will behave correctly if any flag types get a TakesFile field in the future. --- fish.go | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/fish.go b/fish.go index 588e070eab..474c5c7be1 100644 --- a/fish.go +++ b/fish.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io" + "reflect" "strings" "text/template" ) @@ -158,24 +159,17 @@ func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string } func fishAddFileFlag(flag Flag, completion *strings.Builder) { - switch f := flag.(type) { - case *GenericFlag: - if f.TakesFile { - return - } - case *StringFlag: - if f.TakesFile { - return - } - case *StringSliceFlag: - if f.TakesFile { - return - } - case *PathFlag: - if f.TakesFile { + val := reflect.ValueOf(flag) + // if flag is a non-nil pointer to a struct... + if val.Kind() != reflect.Invalid && val.Elem().Kind() == reflect.Struct { + field := val.Elem().FieldByName("TakesFile") + // if flag's underlying type has a bool field called TakesFile, whose value is true... + if field.Kind() == reflect.Bool && field.Bool() { + // don't append '-f' return } } + // append '-f', indicating that arguments to this flag are *not* meant to be file paths completion.WriteString(" -f") }