[dry-cli] Option for tab completion

Hey hey
are there any ideas/support for tab completion in dry-cli? Basically the exposure of a bash/zsh/* completion script which the user has to source in their shell .*rc on login?

Thx, Cheers Tobi

Because dry-cli using ruby’s optparse under the hood, and OptionParser does include hidden options for dumping out completion values, it should be possible to pass in these secret option flags to a dry-cli app:

#!/usr/bin/env ruby

require 'optparse'

optparser = OptionParser.new do |opts|
  opts.banner = 'usage: test.rb [options]'

  opts.on('-t', '--test TEST', 'Sets @test') do |test|
    @test = test
  end

  opts.on('-o', '--opt [OPT]', 'Sets @opt') do |opt|
    @opt = opt
  end

  opts.on_head('-h', '--help', 'Prints this help') do
    puts opts
    exit
  end
end

argv = optparser.parse!(ARGV)
./test.rb --*-completion-bash=-
--help
-h
--test
-t
--opt
-o

Although, I haven’t seen an example of how to connect those hidden options to actual bash/zsh completion rules that would be loaded into your shell.

Thanks, this got me a bit closer. I figured it indeed works when I add it to a command. I get the list of options returned - like:

my-cli my-command --*-completion-bash=-

But I’m still missing the overall list of commands and list of arguments… Unfortunately this doesn’t work and just displays the overall usage/help page

my-cli --*-completion-bash=-

Ok, this took a while, but I’m happy to announce a dry-cli extension or command. Just drop it into your existing cli and allow generating the completion script for bash or zsh. Have a look here:

Huge shout out to GitHub - DannyBen/completely: Generate bash completion scripts using a simple configuration file which allows me to generate the script very conveniently…

p.s. Extension for hanami-cli is in the making :wink:

2 Likes

I saw your comment and recently made command_kit-completion which also uses the completely gem. However, I discovered some limitations with it. It only generates Bash shell completion files, and recommends that Zsh users enable it’s bash-complete comparability layer; this makes it difficult to automatically installing shell files if the user uses zsh instead of bash, since you would either need to modify their ~/.zshrc file to enable bash-completions or print instructions. It also does not support generating Fish shell completion files, since Fish is non-POSIX compatible and has it’s own quirky syntax and completion API; Fish shell is becoming increasingly popular among developers who are often early adopters of new things.

Do you know of a better ruby shell completion library? Or is there any interest in working on a PR to add support for Zsh and Fish shells to completely?

OK, I figured out how to correctly install bash completions into zsh.

  1. Add autoload -Uz +X compinit && compinit and autoload -Uz +X bashcompinit && bashcompinit to ~/.zshrc.
  2. The completion files must be installed into /usr/local/share/zsh/site-functions/.
  3. The completion file name must start with a _ character.
  4. The completion file must start with the lines: #compdef command-name-here to hint to zsh to auto-load the completion file.

Hope that helps anyone wanting to install dry-cli-completion files into their zsh shell.

1 Like