Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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?

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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.


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 :

# importing click
import click
def main():
    click.echo("This cli is built with click. ")
if __name__=="__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.

def greeting(name):
    click.echo("Hello, {}".format(name))
if __name__=="__main__":

>>> python Gifoyle

Hello, Gilfoyle

Optional arguments: Click gives the option to include optional parameters in the form of flags.

import click
@click.option('--string', default ='World',
        help ='This is a greeting')
def hello(string):
    click.echo("Hello, {}".format(string))
if __name__=="__main__":

>>> python

Hello, World

>>> python --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.

import click
def cli(greeting):
    This is the default CLI method.
            greeting: {string}
    click.echo ("This is a simple cli.")
if __name__=="__main":

>>> python --help
This is the default CLI method.

greeting: {string}

  --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
This is a simple cli.

>>> python hello

Usage: [OPTIONS]
Try " --help" for help.

Error: Got unexpected extra argument (hello)

My Personal Notes arrow_drop_up
Recommended Articles
Page :