clap-rs / clap

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
14.07k stars 1.03k forks source link

Allow the completed command to be an alias #1764

Open d-e-s-o opened 4 years ago

d-e-s-o commented 4 years ago

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.

pksunkara commented 4 years ago

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

d-e-s-o commented 4 years ago

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...

pickfire commented 4 years ago

I believe this is fixed.

d-e-s-o commented 4 years ago

I believe this is fixed.

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

pksunkara commented 4 years ago

@pickfire How is this fixed?

pickfire commented 4 years ago

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

pksunkara commented 4 years ago

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