Skip to content
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

Custom value hint #2396

Closed
2 tasks done
tailhook opened this issue Mar 9, 2021 · 4 comments
Closed
2 tasks done

Custom value hint #2396

tailhook opened this issue Mar 9, 2021 · 4 comments

Comments

@tailhook
Copy link

tailhook commented Mar 9, 2021

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Describe your use case

Let's imagine I have a make-like utility. And I want:

make <tab>

To complete to commands listed in the Makefile in the current directory. Most current shells can handle this, but I don't think there is a way to generate completions for that by clap.

Describe the solution you'd like

I propose something like this:

    Arg::new("target")
        .about("target to execute")
        .takes_value(true)
        .value_hint(ValueHint::RunCommand(["--list-targets"]))

This means that shell runs $binname --list-targets to fetch the names of the targets for completion.

The complication is that some shells (namely zsh) may use a description in completion data but others can't so there are two options:

  1. Add --shell=zsh for command (this is nice if command has subcommands: mymake list-targets --shell=zsh but is weird if mymake --list-targets --shell=zsh, because basically --shell= will be accepted but not used in any invocation)
  2. Standardize the output format and adapt it to specific shell by our shell function (i.e. stripping descriptions in bash)

Alternatives, if applicable

Alternative solution is to have ValueHint::ShellFunction("_my_make_targets") and manually append function _my_make_targets to the generated file. This is more flexible, but is harder to use since the shell function will have to be implemented for every shell manually.


What do you think?

@pksunkara
Copy link
Member

Duplicate of #1232

I assume you saw that issue when searching? Why did you decide to create a new one?

@tailhook
Copy link
Author

Duplicate of #1232

I assume you saw that issue when searching? Why did you decide to create a new one?

No I didn't see that. Sorry.

@chisui
Copy link

chisui commented Sep 6, 2021

#1232 seems to be about delegating completion to clap itself. By extending ValueHint you would enable the completion script to call any other program. It also doesn't have to handle the completion itself but simply enumerate possible values. Would it be possible to reopen this issue @pksunkara?

@pksunkara
Copy link
Member

Since the design for #1232 is not yet done and the thing being requested here is so close what's being discussed in that issue, they might actually end up being the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants