[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

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

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

argv = optparser.parse!(ARGV)
./test.rb --*-completion-bash=-

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:

1 Like