Click-params: a little companion to your click project

Kevin Tewouda
2 min readMar 15, 2022

Click-params enhances your click project by adding a lot of new parameters.

An image showing a terminal loading a software
Photo by Jake Walker on Unsplash

This tutorial is intended for those who know click, a python library for building command line interface (CLI) applications. If you don’t know it, I have an excellent introduction here.

I created click-params because there is no parameter in click supporting a list of values without repeating the option or specifying the exact number of values you want. This is not really convenient when you prompt the user for input and wants him to assign a list of values in a row. I also took the opportunity to add several other types of parameters for the network, math, etc…

The minimum python version supported by click-params is 3.7.

I won’t go into details on how to install a CLI in a python project, I assume you already know that. Here is a first example:

Example usage of click-params type

Usage:

$ cream -f passion,mint,strawberry
- passion
- mint
- strawberry
$ cream
Flavors: passion,mint
- passion
- mint

We just need to instantiate the click-params type and we are done. By default, the separator between values is a comma but we can change it, let’s see with another example.

Example of click-params type instantiated with a space

Usage:

$ ip -d "facebook.com google.com"
facebook.com -> 157.240.21.35
google.com -> 142.250.179.110
$ ip
Domains: facebook.com google
Error: These items are not domain names: ['google']
Domains: facebook.com google.com
facebook.com -> 157.240.21.35
google.com -> 142.250.178.142

Notes:

  • Here we instantiate the parameter with a single space as an argument which means that the values must be separated by a space.
  • If you look at the second usage, I deliberately added a wrong domain to show that the validation takes place.

There are many types defined in click-params, take the time to look at them in the documentation.

I also added some utility functions to deal with testing. Indeed, when we test a CLI application, we often check the return code and the message printed in the terminal.

Testing example with click-params utility functions

Here we use assert_equals_output to check that the return code of the CLI is 0 and the output is “Your name is Kevin\n”. There are also functions assert_in_output and assert_list_in_output. Feel free the check this section of the documentation.

It’s all for this tutorial, hope you enjoy it. Take care of yourself and see you soon! 😁

If you like my article and want to continue learning with me, don’t hesitate to follow me here and subscribe to my newsletter on substack 😉

--

--

Kevin Tewouda

Déserteur camerounais résidant désormais en France. Passionné de programmation, sport, de cinéma et mangas. J’écris en français et en anglais dû à mes origines.