Skip to content

Commit 5357b6d

Browse files
authored
Merge pull request urfave#2117 from bittrance/fish-completion-repetition
Fish completions no longer suggest subcommands that have already been picked
2 parents 0264a15 + b6309f0 commit 5357b6d

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

fish.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri
7171
fmt.Fprintf(&completion,
7272
"complete -r -c %s -n '%s' -a '%s'",
7373
cmd.Name,
74-
cmd.fishSubcommandHelper(previousCommands),
74+
cmd.fishSubcommandHelper(previousCommands, commands),
7575
strings.Join(command.Names(), " "),
7676
)
7777

@@ -116,7 +116,7 @@ func (cmd *Command) prepareFishFlags(flags []Flag, previousCommands []string) []
116116
fmt.Fprintf(completion,
117117
"complete -c %s -n '%s'",
118118
cmd.Name,
119-
cmd.fishSubcommandHelper(previousCommands),
119+
cmd.fishFlagHelper(previousCommands),
120120
)
121121

122122
fishAddFileFlag(f, completion)
@@ -165,7 +165,23 @@ func fishAddFileFlag(flag Flag, completion *strings.Builder) {
165165
completion.WriteString(" -f")
166166
}
167167

168-
func (cmd *Command) fishSubcommandHelper(allCommands []string) string {
168+
func (cmd *Command) fishSubcommandHelper(allCommands []string, siblings []*Command) string {
169+
fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", cmd.Name)
170+
if len(allCommands) > 0 {
171+
var siblingNames []string
172+
for _, command := range siblings {
173+
siblingNames = append(siblingNames, command.Names()...)
174+
}
175+
fishHelper = fmt.Sprintf(
176+
"__fish_seen_subcommand_from %s; and not __fish_seen_subcommand_from %s",
177+
strings.Join(allCommands, " "),
178+
strings.Join(siblingNames, " "),
179+
)
180+
}
181+
return fishHelper
182+
}
183+
184+
func (cmd *Command) fishFlagHelper(allCommands []string) string {
169185
fishHelper := fmt.Sprintf("__fish_%s_no_subcommand", cmd.Name)
170186
if len(allCommands) > 0 {
171187
fishHelper = fmt.Sprintf(

testdata/expected-fish-full.fish

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ complete -r -c greet -n '__fish_greet_no_subcommand' -a 'config c' -d 'another u
2121
complete -c greet -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r
2222
complete -c greet -n '__fish_seen_subcommand_from config c' -f -l another-flag -s b -d 'another usage text'
2323
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l help -s h -d 'show help'
24-
complete -r -c greet -n '__fish_seen_subcommand_from config c' -a 'sub-config s ss' -d 'another usage test'
24+
complete -r -c greet -n '__fish_seen_subcommand_from config c; and not __fish_seen_subcommand_from sub-config s ss' -a 'sub-config s ss' -d 'another usage test'
2525
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-flag -s sub-fl -s s -r
2626
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-command-flag -s s -d 'some usage text'
2727
complete -c greet -n '__fish_seen_subcommand_from info i in' -f -l help -s h -d 'show help'
@@ -33,5 +33,5 @@ complete -r -c greet -n '__fish_greet_no_subcommand' -a 'usage u' -d 'standard u
3333
complete -c greet -n '__fish_seen_subcommand_from usage u' -l flag -s fl -s f -r
3434
complete -c greet -n '__fish_seen_subcommand_from usage u' -f -l another-flag -s b -d 'another usage text'
3535
complete -c greet -n '__fish_seen_subcommand_from sub-usage su' -f -l help -s h -d 'show help'
36-
complete -r -c greet -n '__fish_seen_subcommand_from usage u' -a 'sub-usage su' -d 'standard usage text'
36+
complete -r -c greet -n '__fish_seen_subcommand_from usage u; and not __fish_seen_subcommand_from sub-usage su' -a 'sub-usage su' -d 'standard usage text'
3737
complete -c greet -n '__fish_seen_subcommand_from sub-usage su' -f -l sub-command-flag -s s -d 'some usage text'

0 commit comments

Comments
 (0)