Skip to content

Commit 38a6c56

Browse files
Merge pull request #857 from saschagrunert/takes-file-fish
Add `TakesFile` to fish shell completion
2 parents fa6797b + 687f721 commit 38a6c56

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed

docs_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ func testApp() *App {
1010
app.Name = "greet"
1111
app.Flags = []Flag{
1212
StringFlag{
13-
Name: "socket, s",
14-
Usage: "some usage text",
15-
Value: "value",
13+
Name: "socket, s",
14+
Usage: "some usage text",
15+
Value: "value",
16+
TakesFile: true,
1617
},
1718
StringFlag{Name: "flag, fl, f"},
1819
BoolFlag{
@@ -23,7 +24,10 @@ func testApp() *App {
2324
app.Commands = []Command{{
2425
Aliases: []string{"c"},
2526
Flags: []Flag{
26-
StringFlag{Name: "flag, fl, f"},
27+
StringFlag{
28+
Name: "flag, fl, f",
29+
TakesFile: true,
30+
},
2731
BoolFlag{
2832
Name: "another-flag, b",
2933
Usage: "another usage text",

fish.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,14 @@ func (a *App) writeFishCompletionTemplate(w io.Writer) error {
6464
})
6565
}
6666

67-
func (a *App) prepareFishCommands(
68-
commands []Command,
69-
allCommands *[]string,
70-
previousCommands []string,
71-
) []string {
67+
func (a *App) prepareFishCommands(commands []Command, allCommands *[]string, previousCommands []string) []string {
7268
completions := []string{}
7369
for i := range commands {
7470
command := &commands[i]
7571

7672
var completion strings.Builder
7773
completion.WriteString(fmt.Sprintf(
78-
"complete -c %s -f -n '%s' -a '%s'",
74+
"complete -r -c %s -n '%s' -a '%s'",
7975
a.Name,
8076
a.fishSubcommandHelper(previousCommands),
8177
strings.Join(command.Names(), " "),
@@ -113,24 +109,23 @@ func (a *App) prepareFishCommands(
113109
return completions
114110
}
115111

116-
func (a *App) prepareFishFlags(
117-
flags []Flag,
118-
previousCommands []string,
119-
) []string {
112+
func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string {
120113
completions := []string{}
121114
for _, f := range flags {
122115
flag, ok := f.(DocGenerationFlag)
123116
if !ok {
124117
continue
125118
}
126119

127-
var completion strings.Builder
120+
completion := &strings.Builder{}
128121
completion.WriteString(fmt.Sprintf(
129-
"complete -c %s -f -n '%s'",
122+
"complete -c %s -n '%s'",
130123
a.Name,
131124
a.fishSubcommandHelper(previousCommands),
132125
))
133126

127+
fishAddFileFlag(f, completion)
128+
134129
for idx, opt := range strings.Split(flag.GetName(), ",") {
135130
if idx == 0 {
136131
completion.WriteString(fmt.Sprintf(
@@ -158,6 +153,24 @@ func (a *App) prepareFishFlags(
158153
return completions
159154
}
160155

156+
func fishAddFileFlag(flag Flag, completion *strings.Builder) {
157+
switch f := flag.(type) {
158+
case GenericFlag:
159+
if f.TakesFile {
160+
return
161+
}
162+
case StringFlag:
163+
if f.TakesFile {
164+
return
165+
}
166+
case StringSliceFlag:
167+
if f.TakesFile {
168+
return
169+
}
170+
}
171+
completion.WriteString(" -f")
172+
}
173+
161174
func (a *App) fishSubcommandHelper(allCommands []string) string {
162175
fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", a.Name)
163176
if len(allCommands) > 0 {

testdata/expected-fish-full.fish

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ function __fish_greet_no_subcommand --description 'Test if there has been any su
99
return 0
1010
end
1111

12-
complete -c greet -f -n '__fish_greet_no_subcommand' -l socket -s s -r -d 'some usage text'
13-
complete -c greet -f -n '__fish_greet_no_subcommand' -l flag -s fl -s f -r
14-
complete -c greet -f -n '__fish_greet_no_subcommand' -l another-flag -s b -d 'another usage text'
15-
complete -c greet -f -n '__fish_greet_no_subcommand' -l help -s h -d 'show help'
16-
complete -c greet -f -n '__fish_greet_no_subcommand' -l version -s v -d 'print the version'
17-
complete -c greet -f -n '__fish_seen_subcommand_from config c' -l help -s h -d 'show help'
18-
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test'
19-
complete -c greet -f -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r
20-
complete -c greet -f -n '__fish_seen_subcommand_from config c' -l another-flag -s b -d 'another usage text'
21-
complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l help -s h -d 'show help'
22-
complete -c greet -f -n '__fish_seen_subcommand_from config c' -a 'sub-config s ss' -d 'another usage test'
23-
complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l sub-flag -s sub-fl -s s -r
24-
complete -c greet -f -n '__fish_seen_subcommand_from sub-config s ss' -l sub-command-flag -s s -d 'some usage text'
25-
complete -c greet -f -n '__fish_seen_subcommand_from info i in' -l help -s h -d 'show help'
26-
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information'
27-
complete -c greet -f -n '__fish_seen_subcommand_from some-command' -l help -s h -d 'show help'
28-
complete -c greet -f -n '__fish_greet_no_subcommand' -a 'some-command'
12+
complete -c greet -n '__fish_greet_no_subcommand' -l socket -s s -r -d 'some usage text'
13+
complete -c greet -n '__fish_greet_no_subcommand' -f -l flag -s fl -s f -r
14+
complete -c greet -n '__fish_greet_no_subcommand' -f -l another-flag -s b -d 'another usage text'
15+
complete -c greet -n '__fish_greet_no_subcommand' -f -l help -s h -d 'show help'
16+
complete -c greet -n '__fish_greet_no_subcommand' -f -l version -s v -d 'print the version'
17+
complete -c greet -n '__fish_seen_subcommand_from config c' -f -l help -s h -d 'show help'
18+
complete -r -c greet -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test'
19+
complete -c greet -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r
20+
complete -c greet -n '__fish_seen_subcommand_from config c' -f -l another-flag -s b -d 'another usage text'
21+
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l help -s h -d 'show help'
22+
complete -r -c greet -n '__fish_seen_subcommand_from config c' -a 'sub-config s ss' -d 'another usage test'
23+
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-flag -s sub-fl -s s -r
24+
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-command-flag -s s -d 'some usage text'
25+
complete -c greet -n '__fish_seen_subcommand_from info i in' -f -l help -s h -d 'show help'
26+
complete -r -c greet -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information'
27+
complete -c greet -n '__fish_seen_subcommand_from some-command' -f -l help -s h -d 'show help'
28+
complete -r -c greet -n '__fish_greet_no_subcommand' -a 'some-command'

0 commit comments

Comments
 (0)