injekt/slop — Issue #44: Completion thoughts
Author: jaylevitt
Created: 2011-12-27 16:24:59+00:00
State: open

OptParser has a horrible API, but one of the great things about it - at least in theory - is its built-in support for command completion. (I say "in theory" because it looks like you have to know to install `rb_optparse.bash` in your profile, and then manually define each command that might use it, and there's no documentation that would tell you that, so I'm not sure anyone's ever used it.)

I took a look at optparse.rb to see how they do it; there's a lot of code there, but it's mostly around ruby/ruby@ 644f0445e86034dde399d6db8261c82cf34b8e07 and ruby/ruby@91c0ff4f7d28856645b72d09f58dbabfa5e862ad. Looks like they add two secret options, `--*-completion-bash=X` and `--*-completion-zsh=X`, where `X` is the phrase being tab-completed, and then `rb_optparse.bash` sets up bash to use that option to generate the completion file.

Kinda hackish, but doable. Have you given any thought to doing that in slop eventually? I think you could/should go one further, and skip the shim. Allow me to say `Slop.parse :auto_complete => true`, and have slop itself install the `compgen`/`compsys` completion (if it's not present). The first time I run the script anywhere, boom - magic auto-completion.

It's a lot of work, but it'd be a killer feature ☺