Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

More flexible shell completion #146

Open
epage opened this issue Dec 6, 2021 · 7 comments
Open

More flexible shell completion #146

epage opened this issue Dec 6, 2021 · 7 comments

Comments

@epage
Copy link
Owner

epage commented Dec 6, 2021

Issue by d-e-s-o
Thursday Mar 26, 2020 at 21:05 GMT
Originally opened as clap-rs/clap#1764


It would be great if the shell completion was more flexible in what commands it applies to. Let's say I have a program foo and a completion script foo.bash as generated by clap. If I source foo.bash I now have completion for foo's arguments. Fine.

However, what if I have aliased f to foo? Naturally, I'd want to have completion for f as well. Usually that's not a problem. The completion script just registers the completion functionality for a certain command. E.g.,

complete -F _foo -o bashdefault -o default foo

can be found in foo.bash.

Unfortunately, though, just changing that to

complete -F _foo -o bashdefault -o default f

doesn't work, because, well, the script itself has the command/name coded into its logic.

That doesn't have to be the case, however, as git for example shows. Here all I have to do is register my alias as follows:

__git_complete g __git_main

and bam, g has completion.

Would be great to have such a more flexible completion script.

@epage epage added this to the 3.1 milestone Dec 6, 2021
@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pksunkara
Thursday Mar 26, 2020 at 21:09 GMT


We are always open to PRs. I am not exactly good at the completion scripts. I couldn't even get zsh running locally 😞

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by d-e-s-o
Thursday Mar 26, 2020 at 21:23 GMT


My bash completion days are also behind me (and I happily flushed 90% I knew from memory). That being said, this patch seems to be doing the trick:

--- src/completions/bash.rs
+++ src/completions/bash.rs
@@ -33,8 +33,8 @@ impl<'a, 'b> BashGen<'a, 'b> {
     for i in ${{COMP_WORDS[@]}}
     do
         case "${{i}}" in
-            {name})
-                cmd="{name}"
+            "${{1}}")
+                cmd="${{1}}"
                 ;;
             {subcmds}
             *)
@@ -43,7 +43,7 @@ impl<'a, 'b> BashGen<'a, 'b> {
     done

     case "${{cmd}}" in
-        {name})
+        "${{1}}")
             opts="{name_opts}"
             if [[ ${{cur}} == -* || ${{COMP_CWORD}} -eq 1 ]] ; then
                 COMPREPLY=( $(compgen -W "${{opts}}" -- "${{cur}}") )

I'll probably open a PR unless I find an issue...

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pickfire
Saturday Apr 04, 2020 at 03:32 GMT


I believe this is fixed.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by d-e-s-o
Saturday Apr 04, 2020 at 15:11 GMT


I believe this is fixed.

Mind sharing details as to why you believe that to be the case?

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pksunkara
Saturday Apr 04, 2020 at 15:27 GMT


@pickfire How is this fixed?

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pickfire
Sunday Apr 05, 2020 at 14:16 GMT


Because there was a pull request for it? Ah, I didn't notice it was closed instead of merged.

@epage
Copy link
Owner Author

epage commented Dec 6, 2021

Comment by pksunkara
Sunday Apr 05, 2020 at 16:40 GMT


And it was closed without merging because it didn't actually fix the issue.

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

No branches or pull requests

1 participant