Skip to content

Commit 8f20a35

Browse files
authored
Merge pull request urfave#2122 from bittrance/fish-completion-hidden-commands
Fish completion inside hidden commands
2 parents 2668153 + 66d871f commit 8f20a35

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

command_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ func buildExtendedTestCommand() *Command {
9898
}, {
9999
Name: "hidden-command",
100100
Hidden: true,
101+
Flags: []Flag{
102+
&BoolFlag{
103+
Name: "completable",
104+
},
105+
},
101106
}, {
102107
Aliases: []string{"u"},
103108
Flags: []Flag{
@@ -4876,7 +4881,26 @@ func TestJSONExportCommand(t *testing.T) {
48764881
"defaultCommand": "",
48774882
"category": "",
48784883
"commands": null,
4879-
"flags": null,
4884+
"flags": [
4885+
{
4886+
"name": "completable",
4887+
"category": "",
4888+
"defaultText": "",
4889+
"usage": "",
4890+
"required": false,
4891+
"hidden": false,
4892+
"hideDefault": false,
4893+
"local": false,
4894+
"defaultValue": false,
4895+
"aliases": null,
4896+
"takesFileArg": false,
4897+
"config": {
4898+
"Count": null
4899+
},
4900+
"onlyOnce": false,
4901+
"validateDefaults": false
4902+
}
4903+
],
48804904
"hideHelp": false,
48814905
"hideHelpCommand": false,
48824906
"hideVersion": false,

fish.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (cmd *Command) writeFishCompletionTemplate(w io.Writer) error {
3737
// Add commands and their flags
3838
completions = append(
3939
completions,
40-
cmd.prepareFishCommands(cmd.VisibleCommands(), []string{})...,
40+
cmd.prepareFishCommands(cmd.Commands, []string{})...,
4141
)
4242

4343
toplevelCommandNames := []string{}
@@ -55,20 +55,22 @@ func (cmd *Command) writeFishCompletionTemplate(w io.Writer) error {
5555
func (cmd *Command) prepareFishCommands(commands []*Command, previousCommands []string) []string {
5656
completions := []string{}
5757
for _, command := range commands {
58-
var completion strings.Builder
59-
fmt.Fprintf(&completion,
60-
"complete -x -c %s -n '%s' -a '%s'",
61-
cmd.Name,
62-
cmd.fishSubcommandHelper(previousCommands, commands),
63-
strings.Join(command.Names(), " "),
64-
)
65-
66-
if command.Usage != "" {
58+
if !command.Hidden {
59+
var completion strings.Builder
6760
fmt.Fprintf(&completion,
68-
" -d '%s'",
69-
escapeSingleQuotes(command.Usage))
61+
"complete -x -c %s -n '%s' -a '%s'",
62+
cmd.Name,
63+
cmd.fishSubcommandHelper(previousCommands, commands),
64+
strings.Join(command.Names(), " "),
65+
)
66+
67+
if command.Usage != "" {
68+
fmt.Fprintf(&completion,
69+
" -d '%s'",
70+
escapeSingleQuotes(command.Usage))
71+
}
72+
completions = append(completions, completion.String())
7073
}
71-
completions = append(completions, completion.String())
7274
completions = append(
7375
completions,
7476
cmd.prepareFishFlags(command.VisibleFlags(), command.Names())...,

testdata/expected-fish-full.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-fla
2222
complete -c greet -n '__fish_seen_subcommand_from sub-config s ss' -f -l sub-command-flag -s s -d 'some usage text'
2323
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'info i in' -d 'retrieve generic information'
2424
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'some-command'
25+
complete -c greet -n '__fish_seen_subcommand_from hidden-command' -f -l completable
2526
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'usage u' -d 'standard usage text'
2627
complete -c greet -n '__fish_seen_subcommand_from usage u' -l flag -s fl -s f -r
2728
complete -c greet -n '__fish_seen_subcommand_from usage u' -f -l another-flag -s b -d 'another usage text'

0 commit comments

Comments
 (0)