A simple guide to building command-line interfaces in Python

Image for post

Metasploit welcome shell

Life at the terminal is sweet. It’s fast powering up tasks, makes multitasking feel remarkably easy and allows you to keep resources that would otherwise be used by a fancy, but unnecessary, GUI.

Think about having to load up a graphical interface Version Control tool every time you want to track a change in your file — it’s scary how time-wasting that is. That’s our need for command-line interfaces on applications. When you get the itch to create a CLI for an application for the use of you or your team, you scratch it, of course, by building one!

In this article, well explore Python Click for creating command-line interfaces. We’ll familiarize ourselves with Click’s most useful features and see them at play. To do so, we have to get our hands dirty by creating a media file convertor CLI application — a little dirt never hurt!

If you would like to jump right into the complete project first, the code is here.

In the first part of our Python click tutorial, we cover a general but pretty cool Command Line Interface features undirectly related to click. If you’re about to create your first CLI app, you’ll find this bit useful. But for a direct dive, go straight to the section Bringing Click into the Picture.


So, Why a Command-Line Library?

Click is a Python library. It’s possible to create your own CLI from scratch. Take a look at this prompt:

$ >>> Make the world go round?
       A little [default]
       Yes
       No

It’s a simple prompt that asks the user to select from three available choices. We can simply have this with the command below:

user_choice = input('Make the world go round?\n' +
      'A little [default]\n' + 'Yes\n' + 'No\n')

Just like that, we can interact with the user at the command line. We’re taking a leap of faith and hoping that the user gives us a response from the available choices — which may not happen. This takes us to the issue of parsing user inputs.

We have to be ready for the user who replies“spin it” or something fancier like “I’m not sure”.

our_choices = ['a little', 'yes', 'no']
	user_choice = ''

	while user_choice not in our_choices:
	    user_choice = input('Make the world go round?\n' +
	                        'A little [default]\n' + 'Yes\n' + 'No').lower()

	# do something with user_choice

By looping with that condition, we restrict our user to giving us one of the three options. That should do it.

But…remember we wanted “A little”to be our default option if the user doesn’t specify anything. We tweak our code a bit to accommodate this. We should end up with something along these lines:

our_choices = ['a little', 'yes', 'no']
	prompt = 'Make the world go round?\n' +
	        'A little [default]\n' + \
	        'Yes\n' + 'No\n: '

	# Get user input
	user_choice = input(prompt).lower()

	while user_choice and user_choice not in our_choices:
	    user_choice = input(prompt).lower()

	# Incase user Input was blank, Assign default option
	user_choice = our_choices[0] if not user_choice else user_choice

	# Do something with user_choice

It might lessen the excitement, noticing how much patience it takes to parse single user input. Most of the effort has gone into handling side cases of users who don’t like to follow instructions.

It would also be better if the app could specify the error in their input, rather than just repeat the same line — another consideration.

It’s possible to do this for a complete CLI app — parse each prompt, have descriptive error messages, give the user frequent feedback (we hate guessing — remember the last time you used dd). Importantly, we should be asked if we’re sure when we’re about to do something that seems silly.

We’re getting our hands dirty. We should be focussing on feature delivery. Command-line libraries are here to take away this pain so we can focus on the cool part. Remember, dirt doesn’t hurt — as long as it’s just a little dirt!

Image for post

#python #programming #command-line #python3 #cli

Create Your First CLI Application With Python Click
5.10 GEEK