# \[dry-cli\] Option for tab completion

**URL:** <https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394>\
**Category:** Support\
**Created:** [December 4, 2021, 7:30pm UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394 "2021-12-04T19:30:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rngtng](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/rngtng/32/727_2.png) [@rngtng](https://discourse.dry-rb.org/u/rngtng)\
**Post date:** [December 4, 2021, 7:30pm UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394/1 "2021-12-04T19:30:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [December 17, 2021, 3:42am UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394/2 "2021-12-17T03:42:14Z")

</div>

Because `dry-cli` using ruby’s `optparse` under the hood, and `OptionParser` does include [hidden options](https://github.com/ruby/optparse/blob/faf8e5c8fba85ae2dda8d9d882de4c7016b31ec5/lib/optparse.rb#L1007-L1027) for dumping out completion values, it should be possible to pass in these secret option flags to a `dry-cli` app:

```ruby
#!/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)

```

```auto
./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.

---

<div class="post-metadata">

**Author:** ![rngtng](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/rngtng/32/727_2.png) [@rngtng](https://discourse.dry-rb.org/u/rngtng)\
**Post date:** [December 20, 2021, 10:39pm UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394/3 "2021-12-20T22:39:09Z")

</div>

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:

```auto
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

```auto
my-cli --*-completion-bash=-

```

---

<div class="post-metadata">

**Author:** ![rngtng](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/rngtng/32/727_2.png) [@rngtng](https://discourse.dry-rb.org/u/rngtng)\
**Post date:** [December 8, 2022, 9:20pm UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394/4 "2022-12-08T21:20:43Z")

</div>

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:

> **[GitHub - rngtng/dry-cli-completion: Dry::CLI Command to generate a completion...](https://github.com/rngtng/dry-cli-completion)**
>
> Dry::CLI Command to generate a completion script for bash/zsh - GitHub - rngtng/dry-cli-completion: Dry::CLI Command to generate a completion script for bash/zsh

Huge shout out to [GitHub - DannyBen/completely: Generate bash completion scripts using a simple configuration file](https://github.com/DannyBen/completely) which allows me to generate the script very conveniently…

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

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [December 30, 2023, 3:35am UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394/5 "2023-12-30T03:35:11Z")

</div>

I saw your comment and recently made [command\_kit-completion](https://github.com/postmodern/command_kit-completion#readme) which also uses the [completely](https://github.com/DannyBen/completely#readme) 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](https://github.com/DannyBen/completely#readme)?

---

<div class="post-metadata">

**Author:** ![postmodern](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/postmodern/32/696_2.png) [@postmodern](https://discourse.dry-rb.org/u/postmodern)\
**Post date:** [December 31, 2023, 7:44am UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394/6 "2023-12-31T07:44:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kigster](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/kigster/32/719_2.png) [@kigster](https://discourse.dry-rb.org/u/kigster)\
**Post date:** [September 14, 2026, 6:05pm UTC](https://discourse.dry-rb.org/t/dry-cli-option-for-tab-completion/1394/7 "2026-09-14T18:05:03Z")

</div>

A recently published gem [dry-cli-autocomplete | RubyGems.org | your community gem host](https://rubygems.org/gems/dry-cli-autocomplete) provides fast auto-completion for dry-cli gems that do not have dynamically generated commands. If the command and subcommand is static, this gem provides auto-completion for BASH and ZSH with almost no delay.

It was build while pairing with Claude Code, reviewed by human, and offers 100% rspec coverage.
