qrush / sub

a delicious way to organize programs
http://37signals.com/svn/posts/3264-automating-with-convention-introducing-sub
MIT License
1.74k stars 148 forks source link

Autocomplete on 3rd level argument? #7

Open esampaio opened 11 years ago

esampaio commented 11 years ago

First thanks a lot for this, besides being a well thought out framework, it gives us that little push to organize our scripts and share them with the team.

Now for my issue, I'm struggling with the autocompletion, I can make it work for the first level like:

❯ sub mage <tab>
foo        bar 

But I cant make it work on the second level, this is what happens:

❯ sub mage foo <tab>
foo        bar 

When I expected:

❯ sub mage foo <tab>
baz        fubar 

I've organised the files like:

libexec/sub-mage
libexec/sub-mage-foo
libexec/sub-mage-bar
libexec/sub-mage-foo-baz
libexec/sub-mage-foo-fubar

And put the --complete like this:

# Provide sub completions
if [ "$1" = "--complete" ]; then
    { echo foo; echo bar; } | sort | uniq
    exit;
fi

Thanks in advance for any help

sstephenson commented 11 years ago

When you type sub mage foo and hit Tab, you're running the command sub-mage --complete foo. You can check the value of "$2" inside your completion conditional and output a different set of completions there.

qrush commented 11 years ago

We should probably have this as a documented example in the README :)

esampaio commented 11 years ago

Great, thanks @sstephenson =] And +1 on adding this to the README

esampaio commented 11 years ago

I attempted that, and it still didn't work, I don't want to be a bother here, but is it possible to do this? Here is my code:

# Provide sub completions
if [ "$1" = "--complete" ]; then  
  if [ "$2" = "foo" ] || [ "$2" = "bar" ]; then
    { echo baz; echo bla; } | sort | uniq
  else
    { echo foo; echo bar; } | sort | uniq
  fi
  exit;
fi

I even tried adding:

echo $#

But it always shows 1 no matter how many parameters before the <tab>

Thanks in advance, again.

bradical commented 11 years ago

Confirmed. Seems like params beyond the first are not being passed to the sub-commands during auto-complete. Verified by dumping:

echo "Params: $1 $2 $3" >&2

in the first line of my auto-complete conditional and then trying it to do auto-complete with multiple args:

sub command <TAB>
sub command arg1 <TAB>
sub command arg1 arg2 <TAB>

The params only ever contain --complete.

capotej commented 11 years ago

Yep, can confirm also.