Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PathFlag.TakesFile in fish completion #1198

Merged
merged 5 commits into from Oct 31, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 9 additions & 11 deletions fish.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"reflect"
"strings"
"text/template"
)
Expand Down Expand Up @@ -158,20 +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 {
val := reflect.ValueOf(flag)
// if flag is a non-nil pointer to a struct...
rliebz marked this conversation as resolved.
Show resolved Hide resolved
if val.Kind() != reflect.Invalid && val.Elem().Kind() == reflect.Struct {
ErinCall marked this conversation as resolved.
Show resolved Hide resolved
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")
}

Expand Down
4 changes: 4 additions & 0 deletions fish_test.go
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions testdata/expected-fish-full.fish
Expand Up @@ -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'
Expand Down