DEV Community

Cover image for ✨ Shtab: Provide Autocomplete for your Python App
Ivan Shcheklein
Ivan Shcheklein

Posted on

✨ Shtab: Provide Autocomplete for your Python App

I believe most folks have written a bash or Python app/tool that runs from the terminal command line to do some useful stuff. These programs usually accept some parameters and options. And it's really cool when you can press the TAB key and get the list of options the command expects:

$ git ch<TAB>
checkout         -- checkout branch or paths to working tree
checkout-index   -- copy files from index to working directory

🦾 It seems to be a minor thing but it boosts your productivity significantly! Enough for a search query productivity terminal autocomplete to generate almost a million results.

So, how can you do the same magic for your Python app?

The simplest answer is Shtab - a Python library that can analyze your code and generate necessary files for bash or zsh shell to give your tools the same powerful functionality💥 .

This tool was created by Casper to automate the maintenance of the DVC project:

😱 Every time a new feature is added, maintainers and contributors have to update tab completion scripts for multiple supported shells. At best, it's a pain, and at worst, error-prone.

Okay, enough talk, let's see it in action 🎥.

For Python CLI application using argparse, docopt, or argopt simply hand your parser object to shtab (either via the CLI or the Python API), and a tab completion script will be generated for your preferred shell. It's as easy as:

$ shtab --shell=bash myprogram.main.parser

or in Python:

import shtab
print(shtab.complete(parser, shell="bash"))

That's how Git for Data's in terminal looks like when completion is installed:

% dvc <TAB>
Completing dvc commands
add         -- Track data files or directories with DVC.
cache       -- Manage cache settings.
checkout    -- Checkout data files from cache.
commit      -- Save changed data to cache and update DVC-files.
completion  -- Prints out shell tab completion scripts.
At Top: Hit TAB for more, or the character to insert

I hope you'll find it useful and this lib will save you some time! As usual, give the project a ⭐️ to thank the author and welcome to the issue tracker or comments 👇 if you have any questions.

Top comments (2)

waylonwalker profile image
Waylon Walker • Edited


If you want to try out shtab

pip install shtab

For Click Applications

As I have never actually dug into this I was curious what it would take to get autocomplete on one of my click based applications.

I found the docs have a nice bashcomplete guide that covers all of the shells and worked flawlessly for me.

shcheklein profile image
Ivan Shcheklein

This looks cool! Thanks for sharing. And looks like it also has an option to generate a "native" SHELL script which means that performance is not impacted. I've seen some similar tools, but there were effectively running python every time you run shell or even every time you press TAB.