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

Add argument chain to --complete to make multiple level compeltion possible #12

Closed
Nattle opened this issue Oct 8, 2012 · 4 comments · May be fixed by #37
Closed

Add argument chain to --complete to make multiple level compeltion possible #12

Nattle opened this issue Oct 8, 2012 · 4 comments · May be fixed by #37

Comments

@Nattle
Copy link

Nattle commented Oct 8, 2012

I want to be able to tab complete the list of servers in this example:
sub setup setupServers (foo, bar baz)

I dont see any easy way to do this, but I cold be wrong. If the list of completed arguments was passed to --complete then I would be able to parse down them and return completions to an arbitrary depth.

:)

@andreaja
Copy link

andreaja commented Oct 9, 2012

The following patch solves the issue:

diff --git a/completions/sub.bash b/completions/sub.bash
index 40eac81..37c210c 100644
--- a/completions/sub.bash
+++ b/completions/sub.bash
@@ -6,7 +6,7 @@ _sub() {
     COMPREPLY=( $(compgen -W "$(sub commands)" -- "$word") )
   else
     local command="${COMP_WORDS[1]}"
-    local completions="$(sub completions "$command")"
+    local completions="$(sub completions "$command" ${COMP_WORDS[@]:2})"
     COMPREPLY=( $(compgen -W "$completions" -- "$word") )
   fi
 }

@Nattle
Copy link
Author

Nattle commented Oct 9, 2012

This is awesome and very powerful. Thanks so much. I would have done it myself by I got lost after the 2nd or 3rd line of bash scripting :)

Is there any way the script could more gracefully handle a tree like structure? Like if sub-server supplied (foo, bar, baz) and tab completing "sub server bar" automatically looked at sub-server-bar first for completions. That certianly requires some careful thought the effects of that change, but it would make building a robust command structure much easier.

@Nattle Nattle closed this as completed Oct 9, 2012
@qrush qrush reopened this Oct 9, 2012
@bradical
Copy link

bradical commented Nov 8, 2012

This might be similar but I'm looking to provide multiple levels of auto-completion for a server name for the first argument and a user for the second:

sub ssh host user

Currently trying to accomplish with:

if [ "$1" = "--complete" ]; then
  exec echo "server1 server2 server3"
fi

if [ "$2" = "--complete" ]; then
  exec echo "ec2-user root"
fi

Is this the same idea?

@matschaffer
Copy link

This seems to do the trick for zsh. Though I'm still getting stumped on how to write a script that handles both partial completions (e.g., first letter) and multi level completions.

Seems like we'll need something at the bash/zsh level to know if it's a full word coming in but I haven't figured it out yet.

diff --git a/completions/nf.zsh b/completions/nf.zsh
index 01aef31..0b16dbf 100644
--- a/completions/nf.zsh
+++ b/completions/nf.zsh
@@ -12,7 +12,7 @@ _nf() {
   if [ "${#words}" -eq 2 ]; then
     completions="$(nf commands)"
   else
-    completions="$(nf completions "${word}")"
+    completions="$(nf completions "${word}" ${words[3,-1]})"
   fi

   reply=("${(ps:\n:)completions}")

@Nattle Nattle closed this as completed Jul 26, 2023
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

Successfully merging a pull request may close this issue.

5 participants