Member-only story

5 Tips to Customise Python Command-Line Interfaces

Build better command-line tools

Alexandre Couëdelo
Better Programming
6 min readAug 12, 2021

--

Photo by James Kovin on Unsplash

One very common usage of Python is to create a toolbox, to help you complete repetitive tasks.

If you are reading this article I presume that you already created automation scripts in Python.

Then maybe, at some point, you started refactoring your script to make it a proper command-line tool. Adding argument, configuration, prompt, and instructions. Over time I found that there is always a common set of features I like to add to my CLIs.

I believe it must be the same for you. So, I decided to compile and share the best tricks I tend to add to my command lines whenever I create a new one.

1. Choose the Right Tool To Parse Your Arguments

The first upgrade from an automation script to a CLIs is to allow arguments. Arguments declare values for your program and make it customizable.

One thing is sure I hate to use the native argparse package. This package is extremely verbose, meaning that it requires a lot of code to achieve simple tasks. Here is a simple example from the documentation of argparse for reference:

--

--

Alexandre Couëdelo
Alexandre Couëdelo

Written by Alexandre Couëdelo

Software Supply Chain and Automation Specialist (aka. DevOps).

No responses yet

Write a response