Friday, December 27, 2024
Google search engine
HomeLanguagesClick Module in Python | Making awesome Command Line Utilities

Click Module in Python | Making awesome Command Line Utilities

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 :




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




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




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.




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)

RELATED ARTICLES

Most Popular

Recent Comments