Skip to content

Commit 2668153

Browse files
authored
Merge pull request urfave#2121 from bittrance/fish-completion-toplevel-detection
Simpler top-level context detection for fish completions
2 parents 53ad542 + 55db2a0 commit 2668153

File tree

2 files changed

+11
-40
lines changed

2 files changed

+11
-40
lines changed

fish.go

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,29 @@ func (cmd *Command) writeFishCompletionTemplate(w io.Writer) error {
3030
if err != nil {
3131
return err
3232
}
33-
allCommands := []string{}
3433

3534
// Add global flags
36-
completions := cmd.prepareFishFlags(cmd.VisibleFlags(), allCommands)
37-
38-
// Add help flag
39-
if !cmd.HideHelp {
40-
completions = append(
41-
completions,
42-
cmd.prepareFishFlags([]Flag{HelpFlag}, allCommands)...,
43-
)
44-
}
45-
46-
// Add version flag
47-
if !cmd.HideVersion {
48-
completions = append(
49-
completions,
50-
cmd.prepareFishFlags([]Flag{VersionFlag}, allCommands)...,
51-
)
52-
}
35+
completions := cmd.prepareFishFlags(cmd.VisibleFlags(), []string{})
5336

5437
// Add commands and their flags
5538
completions = append(
5639
completions,
57-
cmd.prepareFishCommands(cmd.VisibleCommands(), &allCommands, []string{})...,
40+
cmd.prepareFishCommands(cmd.VisibleCommands(), []string{})...,
5841
)
5942

43+
toplevelCommandNames := []string{}
44+
for _, child := range cmd.Commands {
45+
toplevelCommandNames = append(toplevelCommandNames, child.Names()...)
46+
}
47+
6048
return t.ExecuteTemplate(w, name, &fishCommandCompletionTemplate{
6149
Command: cmd,
6250
Completions: completions,
63-
AllCommands: allCommands,
51+
AllCommands: toplevelCommandNames,
6452
})
6553
}
6654

67-
func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]string, previousCommands []string) []string {
55+
func (cmd *Command) prepareFishCommands(commands []*Command, previousCommands []string) []string {
6856
completions := []string{}
6957
for _, command := range commands {
7058
var completion strings.Builder
@@ -80,15 +68,6 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri
8068
" -d '%s'",
8169
escapeSingleQuotes(command.Usage))
8270
}
83-
84-
if !command.HideHelp {
85-
completions = append(
86-
completions,
87-
cmd.prepareFishFlags([]Flag{HelpFlag}, command.Names())...,
88-
)
89-
}
90-
91-
*allCommands = append(*allCommands, command.Names()...)
9271
completions = append(completions, completion.String())
9372
completions = append(
9473
completions,
@@ -100,7 +79,7 @@ func (cmd *Command) prepareFishCommands(commands []*Command, allCommands *[]stri
10079
completions = append(
10180
completions,
10281
cmd.prepareFishCommands(
103-
command.Commands, allCommands, command.Names(),
82+
command.Commands, command.Names(),
10483
)...,
10584
)
10685
}

testdata/expected-fish-full.fish

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
function __fish_greet_no_subcommand --description 'Test if there has been any subcommand yet'
44
for i in (commandline -opc)
5-
if contains -- $i config c sub-config s ss info i in some-command usage u sub-usage su
5+
if contains -- $i config c info i in some-command hidden-command usage u
66
return 1
77
end
88
end
@@ -14,24 +14,16 @@ complete -c greet -n '__fish_greet_no_subcommand' -f -l flag -s fl -s f -r
1414
complete -c greet -n '__fish_greet_no_subcommand' -f -l another-flag -s b -d 'another usage text'
1515
complete -c greet -n '__fish_greet_no_subcommand' -l logfile -r
1616
complete -c greet -n '__fish_greet_no_subcommand' -l foofile -r
17-
complete -c greet -n '__fish_greet_no_subcommand' -f -l help -s h -d 'show help'
18-
complete -c greet -n '__fish_greet_no_subcommand' -f -l version -s v -d 'print the version'
19-
complete -c greet -n '__fish_seen_subcommand_from config c' -f -l help -s h -d 'show help'
2017
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'config c' -d 'another usage test'
2118
complete -c greet -n '__fish_seen_subcommand_from config c' -l flag -s fl -s f -r
2219
complete -c greet -n '__fish_seen_subcommand_from config c' -f -l another-flag -s b -d 'another usage text'
23-
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l help -s h -d 'show help'
2420
complete -x -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'
2521
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-flag -s sub-fl -s s -r
2622
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-command-flag -s s -d 'some usage text'
27-
complete -c greet -n '__fish_seen_subcommand_from info i in' -f -l help -s h -d 'show help'
2823
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information'
29-
complete -c greet -n '__fish_seen_subcommand_from some-command' -f -l help -s h -d 'show help'
3024
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'some-command'
31-
complete -c greet -n '__fish_seen_subcommand_from usage u' -f -l help -s h -d 'show help'
3225
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'usage u' -d 'standard usage text'
3326
complete -c greet -n '__fish_seen_subcommand_from usage u' -l flag -s fl -s f -r
3427
complete -c greet -n '__fish_seen_subcommand_from usage u' -f -l another-flag -s b -d 'another usage text'
35-
complete -c greet -n '__fish_seen_subcommand_from sub-usage su' -f -l help -s h -d 'show help'
3628
complete -x -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'
3729
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)