From 5bb54ace578d17a134feb806f22163e7064ede87 Mon Sep 17 00:00:00 2001 From: Erin Call Date: Mon, 22 Jun 2020 17:17:47 -0700 Subject: [PATCH 1/3] fish.go: support PathFlag.TakesFile [#1156] --- fish.go | 4 ++++ fish_test.go | 4 ++++ testdata/expected-fish-full.fish | 1 + 3 files changed, 9 insertions(+) diff --git a/fish.go b/fish.go index 67122c9fe7..588e070eab 100644 --- a/fish.go +++ b/fish.go @@ -171,6 +171,10 @@ func fishAddFileFlag(flag Flag, completion *strings.Builder) { if f.TakesFile { return } + case *PathFlag: + if f.TakesFile { + return + } } completion.WriteString(" -f") } diff --git a/fish_test.go b/fish_test.go index a4c1871438..4ca8c47903 100644 --- a/fish_test.go +++ b/fish_test.go @@ -7,6 +7,10 @@ import ( func TestFishCompletion(t *testing.T) { // Given app := testApp() + app.Flags = append(app.Flags, &PathFlag{ + Name: "logfile", + TakesFile: true, + }) // When res, err := app.ToFishCompletion() diff --git a/testdata/expected-fish-full.fish b/testdata/expected-fish-full.fish index b18d51e8fe..dc41e5aa9a 100644 --- a/testdata/expected-fish-full.fish +++ b/testdata/expected-fish-full.fish @@ -12,6 +12,7 @@ end complete -c greet -n '__fish_greet_no_subcommand' -l socket -s s -r -d 'some \'usage\' text' complete -c greet -n '__fish_greet_no_subcommand' -f -l flag -s fl -s f -r complete -c greet -n '__fish_greet_no_subcommand' -f -l another-flag -s b -d 'another usage text' +complete -c greet -n '__fish_greet_no_subcommand' -l logfile -r complete -c greet -n '__fish_greet_no_subcommand' -f -l help -s h -d 'show help' complete -c greet -n '__fish_greet_no_subcommand' -f -l version -s v -d 'print the version' complete -c greet -n '__fish_seen_subcommand_from config c' -f -l help -s h -d 'show help' From fcce511478c1aabd0e4a6af313acd74a89aaa21c Mon Sep 17 00:00:00 2001 From: Erin Call Date: Tue, 13 Oct 2020 12:55:44 -0700 Subject: [PATCH 2/3] 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") } From 5c8d915be7a2444901068fb08544b0b405bae178 Mon Sep 17 00:00:00 2001 From: Erin Call Date: Thu, 22 Oct 2020 14:22:01 -0700 Subject: [PATCH 3/3] Revert "Refactor fishAddFileFlag for better flexibility [#1156]" This reverts commit fcce511478c1aabd0e4a6af313acd74a89aaa21c. --- fish.go | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/fish.go b/fish.go index 474c5c7be1..588e070eab 100644 --- a/fish.go +++ b/fish.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "io" - "reflect" "strings" "text/template" ) @@ -159,17 +158,24 @@ func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string } func fishAddFileFlag(flag Flag, completion *strings.Builder) { - 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' + 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 { return } } - // append '-f', indicating that arguments to this flag are *not* meant to be file paths completion.WriteString(" -f") }