diff --git a/docs_test.go b/docs_test.go index 177eb19491..25afd23909 100644 --- a/docs_test.go +++ b/docs_test.go @@ -10,9 +10,10 @@ func testApp() *App { app.Name = "greet" app.Flags = []Flag{ StringFlag{ - Name: "socket, s", - Usage: "some usage text", - Value: "value", + Name: "socket, s", + Usage: "some usage text", + Value: "value", + TakesFile: true, }, StringFlag{Name: "flag, fl, f"}, BoolFlag{ @@ -23,7 +24,10 @@ func testApp() *App { app.Commands = []Command{{ Aliases: []string{"c"}, Flags: []Flag{ - StringFlag{Name: "flag, fl, f"}, + StringFlag{ + Name: "flag, fl, f", + TakesFile: true, + }, BoolFlag{ Name: "another-flag, b", Usage: "another usage text", diff --git a/fish.go b/fish.go index 0f51065ced..166435c0d2 100644 --- a/fish.go +++ b/fish.go @@ -64,18 +64,14 @@ func (a *App) writeFishCompletionTemplate(w io.Writer) error { }) } -func (a *App) prepareFishCommands( - commands []Command, - allCommands *[]string, - previousCommands []string, -) []string { +func (a *App) prepareFishCommands(commands []Command, allCommands *[]string, previousCommands []string) []string { completions := []string{} for i := range commands { command := &commands[i] var completion strings.Builder completion.WriteString(fmt.Sprintf( - "complete -c %s -f -n '%s' -a '%s'", + "complete -r -c %s -n '%s' -a '%s'", a.Name, a.fishSubcommandHelper(previousCommands), strings.Join(command.Names(), " "), @@ -113,10 +109,7 @@ func (a *App) prepareFishCommands( return completions } -func (a *App) prepareFishFlags( - flags []Flag, - previousCommands []string, -) []string { +func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string { completions := []string{} for _, f := range flags { flag, ok := f.(DocGenerationFlag) @@ -124,13 +117,15 @@ func (a *App) prepareFishFlags( continue } - var completion strings.Builder + completion := &strings.Builder{} completion.WriteString(fmt.Sprintf( - "complete -c %s -f -n '%s'", + "complete -c %s -n '%s'", a.Name, a.fishSubcommandHelper(previousCommands), )) + fishAddFileFlag(f, completion) + for idx, opt := range strings.Split(flag.GetName(), ",") { if idx == 0 { completion.WriteString(fmt.Sprintf( @@ -158,6 +153,24 @@ func (a *App) prepareFishFlags( return completions } +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 + } + } + completion.WriteString(" -f") +} + func (a *App) fishSubcommandHelper(allCommands []string) string { fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", a.Name) if len(allCommands) > 0 { diff --git a/testdata/expected-fish-full.fish b/testdata/expected-fish-full.fish index 3538c199b0..bda03228f2 100644 --- a/testdata/expected-fish-full.fish +++ b/testdata/expected-fish-full.fish @@ -9,20 +9,20 @@ function __fish_greet_no_subcommand --description 'Test if there has been any su return 0 end -complete -c greet -f -n '__fish_greet_no_subcommand' -l socket -s s -r -d 'some usage text' -complete -c greet -f -n '__fish_greet_no_subcommand' -l flag -s fl -s f -r -complete -c greet -f -n '__fish_greet_no_subcommand' -l another-flag -s b -d 'another usage text' -complete -c greet -f -n '__fish_greet_no_subcommand' -l help -s h -d 'show help' -complete -c greet -f -n '__fish_greet_no_subcommand' -l version -s v -d 'print the version' -complete -c greet -f -n '__fish_seen_subcommand_from config c' -l help -s h -d 'show help' -complete -c greet -f -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test' -complete -c greet -f -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r -complete -c greet -f -n '__fish_seen_subcommand_from config c' -l another-flag -s b -d 'another usage text' -complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l help -s h -d 'show help' -complete -c greet -f -n '__fish_seen_subcommand_from config c' -a 'sub-config s ss' -d 'another usage test' -complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l sub-flag -s sub-fl -s s -r -complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l sub-command-flag -s s -d 'some usage text' -complete -c greet -f -n '__fish_seen_subcommand_from info i in' -l help -s h -d 'show help' -complete -c greet -f -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information' -complete -c greet -f -n '__fish_seen_subcommand_from some-command' -l help -s h -d 'show help' -complete -c greet -f -n '__fish_greet_no_subcommand' -a 'some-command' +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' -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' +complete -r -c greet -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test' +complete -c greet -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r +complete -c greet -n '__fish_seen_subcommand_from config c' -f -l another-flag -s b -d 'another usage text' +complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l help -s h -d 'show help' +complete -r -c greet -n '__fish_seen_subcommand_from config c' -a 'sub-config s ss' -d 'another usage test' +complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-flag -s sub-fl -s s -r +complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-command-flag -s s -d 'some usage text' +complete -c greet -n '__fish_seen_subcommand_from info i in' -f -l help -s h -d 'show help' +complete -r -c greet -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information' +complete -c greet -n '__fish_seen_subcommand_from some-command' -f -l help -s h -d 'show help' +complete -r -c greet -n '__fish_greet_no_subcommand' -a 'some-command'