8000 Fish completion now supports dynamic completions by bittrance · Pull Request #2144 · urfave/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fish completion now supports dynamic completions #2144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions command_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ func (cmd *Command) setupDefaults(osArgs []string) {
isRoot := cmd.parent == nil
tracef("isRoot? %[1]v (cmd=%[2]q)", isRoot, cmd.Name)

if cmd.ShellComplete == nil {
tracef("setting default ShellComplete (cmd=%[1]q)", cmd.Name)
cmd.ShellComplete = DefaultCompleteWithFlags
}

if cmd.Name == "" && isRoot {
name := filepath.Base(osArgs[0])
tracef("setting cmd.Name from first arg basename (cmd=%[1]q)", name)
Expand Down
34 changes: 34 additions & 0 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func buildExtendedTestCommand() *Command {
Usage: "retrieve generic information",
}, {
Name: "some-command",
}, {
Name: "custom",
ShellComplete: func(ctx context.Context, c *Command) {},
HideHelp: true,
}, {
Name: "hidden-command",
Hidden: true,
Expand Down Expand Up @@ -4870,6 +4874,36 @@ func TestJSONExportCommand(t *testing.T) {
"arguments": null,
"readArgsFromStdin": false
},
{
"name": "custom",
"aliases": null,
"usage": "",
"usageText": "",
"argsUsage": "",
"version": "",
"description": "",
"defaultCommand": "",
"category": "",
"commands": null,
"flags": null,
"hideHelp": true,
"hideHelpCommand": false,
"hideVersion": false,
"hidden": false,
"authors": null,
"copyright": "",
"metadata": null,
"sliceFlagSeparator": "",
"disableSliceFlagSeparator": false,
"useShortOptionHandling": false,
"suggest": false,
"allowExtFlags": false,
"skipFlagParsing": false,
"prefixMatchCommands": false,
"mutuallyExclusiveFlags": null,
"arguments": null,
"readArgsFromStdin": false
},
{
"name": "hidden-command",
"aliases": null,
Expand Down
15 changes: 14 additions & 1 deletion fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,21 @@ func (cmd *Command) writeFishCompletionTemplate(w io.Writer) error {
}

func prepareFishCommands(binary string, parent *Command) []string {
commands := parent.Commands
completions := []string{}
if parent.ShellComplete != nil {
var completion strings.Builder
fmt.Fprintf(&completion,
"complete -x -c %s -n '%s' -a '%s'",
binary,
commandAncestry(parent),
"(eval command (commandline -pc) "+completionFlag+")",
)
completions = append(
completions,
completion.String(),
)
}
commands := parent.Commands
for _, command := range commands {
if !command.Hidden {
var completion strings.Builder
Expand Down
3 changes: 3 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ func checkCompletions(ctx context.Context, cmd *Command) bool {
if cmd.ShellComplete != nil {
tracef("running shell completion func for command %[1]q", cmd.Name)
cmd.ShellComplete(ctx, cmd)
} else {
tracef("running default shell completion func for command %[1]q", cmd.Name)
DefaultCompleteWithFlags(ctx, cmd)
}

return true
Expand Down
4 changes: 3 additions & 1 deletion testdata/expected-fish-full.fish
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function __fish_greet_no_subcommand --description 'Test if there has been any subcommand yet'
for i in (commandline -opc)
if contains -- $i config c info i in some-command hidden-command usage u
if contains -- $i config c info i in some-command custom hidden-command usage u
return 1
end
end
Expand Down Expand Up @@ -30,6 +30,8 @@ complete -x -c greet -n '__fish_seen_subcommand_from info i in; and not __fish_s
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'some-command'
complete -c greet -n '__fish_seen_subcommand_from some-command' -f -l help -s h -d 'show help'
complete -x -c greet -n '__fish_seen_subcommand_from some-command; and not __fish_seen_subcommand_from help h' -a 'help' -d 'Shows a list of commands or help for one command'
complete -x -c greet -n '__fish_greet_no_subcommand' -a 'custom'
complete -x -c greet -n '__fish_seen_subcommand_from custom' -a '(eval command (commandline -pc) --generate-shell-completion)'
complete -c greet -n '__fish_seen_subcommand_from hidden-command' -f -l completable
complete -c greet -n '__fish_seen_subcommand_from hidden-command' -f -l help -s h -d 'show help'
complete -x -c greet -n '__fish_seen_subcommand_from hidden-command; and not __fish_seen_subcommand_from help h' -a 'help' -d 'Shows a list of commands or help for one command'
Expand Down
0