How-To Guide

How to Build Shell Completion for a CLI

Add shell completion: generate the script for each shell, complete the subcommands and the flags, and test the generated words.

On this page

  1. Introduction
  2. Generate a completion script per shell
  3. Complete the subcommands and the flags
  4. Complete the dynamic values
  5. Test the generated completion
  6. Keep the completion in sync with the command
  7. Common pitfalls
  8. Conclusion

Introduction

Shell completion is what lets a user press Tab and see the subcommands and the flags of a CLI. It discovers the command’s surface, which the user might not know, and it reduces the typos in a long command. A framework that knows the command’s structure can generate the completion for each shell, so adding it is a small feature rather than a manual script per shell.

This tutorial covers generating the per-shell scripts, completing the subcommands and the flags, completing the dynamic values, and testing the generated words. The examples use a CLI framework, and the design applies to any framework with a completion feature.

Generate a completion script per shell

Provide a completion command that emits the completion script for a shell, so the user sources it or installs it. The common shells are bash, zsh, and fish, each with its own completion mechanism, and the framework generates the script for each from the command’s structure. The user runs mycli completion bash and sources the output.

Document the install step, so the user knows how to enable it, and make the command idempotent, so re-running it is safe. The completion is a static script derived from the command’s structure, so it is regenerated when the structure changes, which the framework does at build time or at the completion command.

# the user installs the completion for their shell
mycli completion bash > /etc/bash_completion.d/mycli   # bash
mycli completion zsh  > ~/.zsh/completions/_mycli       # zsh
mycli completion fish > ~/.config/fish/completions/mycli.fish   # fish

Complete the subcommands and the flags

The completion suggests the subcommand names and the flag names at the right position. After the command, it suggests the subcommands; after a dash, it suggests the flags. The framework knows the structure, so the suggestions match the command’s actual surface, and a new subcommand or flag is in the completion automatically.

The context matters: the flags of the subcommand, not the root, and the position in the argument list. The framework handles it, and a hand-written completion must track the context, which is why the generated one is preferred. The generated completion also handles the flag values, such as --format <TAB> suggesting the formats.

Complete the dynamic values

For an argument whose values the command can produce, such as a profile, a branch, or a container name, the completion runs the command to list them. The shell calls the completion function, the function runs mycli list-profiles, and the output becomes the candidates. This is the dynamic completion, and it makes the completion reflect the current state, not a fixed list.

The dynamic completion must be fast, because the shell calls it on each Tab, and it must fail quietly, so an error does not break the user’s shell. Cache the values for a short time where they are slow, and handle the case where the command fails by returning no candidates rather than an error. The dynamic values are the completion’s value beyond the static structure.

# dynamic completion: the function runs the command to list the values
_mycli_profiles() {
  local profiles
  profiles=$(mycli list-profiles 2>/dev/null)   # fail quietly
  COMPREPLY=($(compgen -W "$profiles" -- "${COMP_WORDS[COMP_CWORD]}"))
}
complete -F _mycli_profiles mycli use-profile

Test the generated completion

Test the completion by invoking the completion function with a partial input and asserting the candidates. For a framework, the completion is derived from the structure, so the test asserts the structure’s surface is complete. For a dynamic completion, the test stubs the command’s output and asserts the candidates, so the completion is checked without the shell.

Test the edge cases: the completion after a subcommand suggests that subcommand’s flags, a partial word is completed, and the dynamic completion returns the current values. Test that the completion does not break when the command fails, by stubbing a failure and asserting no candidates rather than an error. These tests keep the completion matching the command.

# invoke the completion function with a partial input
COMP_WORDS=(mycli dep) COMP_CWORD=1
_mycli
# assert the candidates include "deploy", the subcommand starting with "dep"

Keep the completion in sync with the command

The completion is derived from the command’s structure, so it stays in sync when the structure changes, if it is generated rather than hand-written. A hand-written completion drifts: a new flag is not suggested, and a removed command is still offered. Prefer the generated completion, and regenerate it as part of the release.

For the dynamic values, keep the command they call fast and stable, so the completion does not lag or fail. A completion that calls a slow or a failing command is worse than a static one. Keep the dynamic command to a quick lookup, and cache where the values change slowly, so the Tab is responsive.

Common pitfalls

No completion so the user must remember the surface, a hand-written completion that drifts from the command, a dynamic completion that calls a slow or a failing command so the Tab lags or errors, a completion that does not handle the context so the wrong flags are suggested, and no test so a change silently breaks it are the recurring mistakes. A generated per-shell script, the structure-derived completion, a fast dynamic command, the context handling, and the tests address them.

Conclusion

Shell completion lets a user press Tab and see the command’s surface. Generate the per-shell script from the structure, complete the subcommands and the flags, complete the dynamic values with a fast command, and test the generated words. Self-check by invoking the completion function with a partial input and asserting the candidates include the expected subcommand. Then the command is discoverable from the shell.

Sources: Cobra: Shell completion, Click: Shell completion, Bash: Programmable completion.

FAQ

Why add shell completion?
It lets the user press Tab and get the subcommands and the flags, which discovers the command's surface and reduces typos and memorisation. It is a mark of a polished CLI and it costs little when the framework generates it.
Static or dynamic completion?
Static completion lists the subcommands and the flags, which the framework can generate from the structure. Dynamic completion runs the command to list the values, such as the git branches, which needs a completion function that the shell calls. Use both: static for the structure, dynamic for the values.

Related Examples