Click Module in Python | Making awesome Command Line Utilities
Last Updated :
14 Mar, 2019
Since the dawn of the computer age and before the internet outburst, programmers have been using command line tools in an interactive shell as a means to communicate with the computers. It is kind of strange that with all the advancements in UI/UX technologies, very few people know that there are to create beautiful command line interfaces too.
Ever wanted to create a command line tool yourself that is actually very user-friendly, efficient and easy to use? Well,
click is a Python Package that does just that. There are many Python packages that we could use instead of
click such as argparse, docopt, etc., so we will first look at why we are using
click.
Why Click?
There are a couple of reasons why
click is the better tool for the job.
- Click is lazily composable without restrictions.
- It is fully nested.
- Click has strong information available for all parameters and commands so that it can generate unified help pages for the full CLI and to assist the user in converting the input data as necessary.
- Click has a strong understanding of what types are and can give the user consistent error messages if something goes wrong.
Installation:
pip install click
Basics of a Command Line Interface:
Depending on the type and purpose of the CLI, it can have a variety of functionalities. You probably would already have used pip which is also a CLI. Some basics functions which all CLIs have are:
- An argument.
- An option, which is an optional parameter
- A flag, this is a special option which enables or disables a certain function. One of the most common flags is --help.
Simple program using click :
Python3 1==
# importing click
import click
@click.command()
def main():
click.echo("This cli is built with click. ")
if __name__=="__main__":
main()
So let’s build a command line tool that will print a greeting for the name provided in the arguments.
Argument Parsing: Click uses the python decorators for parsing arguments related to a function.
Python3 1==
@click.command()
@click.argument('name')
def greeting(name):
click.echo("Hello, {}".format(name))
if __name__=="__main__":
greeting()
>>>
python greet.py Gifoyle
Hello, Gilfoyle
Optional arguments: Click gives the option to include optional parameters in the form of flags.
Python3 1==
import click
@click.command()
@click.option('--string', default ='World',
help ='This is a greeting')
def hello(string):
click.echo("Hello, {}".format(string))
if __name__=="__main__":
hello()
>>>
python hello.py
Hello, World
>>>
python hello.py --string Dinesh
Hello, Dinesh
Help: The most important and final step towards making the perfect CLI is to provide documentation to our code. Click provides a nice and formatted help text in the command line when used the optional argument
--help
. It uses the docstring specified in the function.
Python3 1==
import click
@click.command()
@click.argument(‘greeting’)
def cli(greeting):
'''
This is the default CLI method.
Arguments:
greeting: {string}
'''
click.echo(greeting)
click.echo ("This is a simple cli.")
if __name__=="__main":
cli()
>>>
python cli.py --help
This is the default CLI method.
Arguments:
greeting: {string}
Options:
--string TEXT Hello
--help Show this message and exit.
>>>
Error Handling: Error handling is an important part of a CLI. How your script handles and manages the errors matters a lot and also helps the user to better understand the mistake.
>>>
python cli.py
This is a simple cli.
>>>
python cli.py hello
Usage: greet.py [OPTIONS]
Try "greet.py --help" for help.
Error: Got unexpected extra argument (hello)
Similar Reads
How Can I Make One Python File Run Another File?
In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Ma
2 min read
Command Line File Downloader in Python
Python is one of the most popular general-purpose programming languages with a wide range of use cases from general coding to complex fields like AI. One of the reasons for such popularity of python as a programming language is the availability of many built-in as well as third-party libraries and p
4 min read
How Use Linux Command In Python Using System.Os
Using the system module from the os library in Python allows you to interact with the Linux command line directly from your Python script. This module provides a way to execute system commands, enabling you to automate various tasks by integrating Linux commands seamlessly into your Python code. Whe
3 min read
How to Build a Password Manager in Python
We have a task to create a Password Manager in Python. In this article, we will see how to create a Password Manager in Python. What is a Password Manager?A Password Manager in Python is a tool in which we can add the username and password. Additionally, it allows us to retrieve the username and pas
7 min read
Installing and Using Rich Package in Python
In this article, We are going to learn how to install and use rich packages in Python. RIch is a python package for creating some awesome terminal formatting and logging. It has several features and functions that can make your application look nicer and even add a new look to your CLI application.
10 min read
Install Poetry to Manage Python Dependencies
Poetry is a modern and user-friendly dependency management tool for Python. It simplifies the process of managing project dependencies, packaging, and publishing. In this article, we will see how to install poetry in Python in Windows. What is Python Poetry?Python Poetry is a modern and comprehensiv
2 min read
Python for Kids - Fun Tutorial to Learn Python Programming
Python for Kids - Python is an easy-to-understand and good-to-start programming language. In this Python tutorial for kids or beginners, you will learn Python and know why it is a perfect fit for kids to start. Whether the child is interested in building simple games, creating art, or solving puzzle
15+ min read
Disappearing Text Desktop Application in Python
In this article, we will build an exciting desktop application using Python and its GUI library, Tkinter. Disappearing Text Desktop Application in PythonThe app is simple to use. The user will start writing whatever he wants to write. If he takes a pause for 5 seconds, the text he has written is del
12 min read
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Competitive Coding Setup for C++ and Python in VS Code using Python Script
Most of us struggle with using heavy software to run C++ and python code and things become more complicated when we have too many files in a folder. In this blog, we are going to create a python script with VS-code-editor that works out to do all your works. It creates the folder + numbers of files
3 min read