Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 28, 2020 01:23 am GMT

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 treecheckout-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 shtabprint(shtab.complete(parser, shell="bash"))

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

% dvc <TAB>Completing dvc commandsadd         -- 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.


Original Link: https://dev.to/shcheklein/shtab-provide-autocomplete-for-your-python-app-3npd

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To