Click

Click is a Command Line Interface Creation Kit, it helps in arbitrary nesting of commands, automatic help page generation, supports lazy loading of subcommands at runtime. It aims to make the process of writing command-line tools quick and fun while also preventing any frustration caused by the inability to implement an intended CLI API. It comes with useful common helpers (getting terminal dimensions, ANSI colors, fetching direct keyboard input, screen clearing, finding config paths, launching apps and editors, etc.)
 

Installation:

To install this module type the below command in the terminal.

$pip install click

Example: 

Python3




# click_example.py
 
import click
 
# initialize result to 0
result=0
 
@click.command()
@click.option('--num1',
              default=1,
              help='Enter a float value',
              type=float)
 
@click.option('--num2',
              default=1,
              help='Enter a float value',
              type=float)
 
@click.option('--op',
              default='+',
              help='Enter the operator')
 
# Calculator function
def calculator(num1,num2,op):
    if op=='+':
        result=num1+num2
    if op=='*':
        result=num1*num2
    if op=='-':
        result=num1-num2
    if op=='/':
        result=num1/num2
 
    # print the result   
    click.echo("Result is %f" %result)
 
if __name__ =='__main__':
    calculator()


Output:

 

Argparse VS Docopt VS Click – Comparing Python Command-Line Parsing Libraries

Before knowing about the Python parsing libraries we must have prior knowledge about Command Line User Interface. A Command Line Interface (CLI) provides a user-friendly interface for the Command-line programs, which is most commonly favored by the developers or the programmers who prefer keyboard programming, instead of using the mouse. By building Command-line interfaces user can interact through the consoles, shells or terminals more efficiently.

There are plenty of Python libraries to build command line applications such as Argparse, Docopt, Click, Client and many more. Now, let us know more on frequently used Python libraries – Argparse, Docopt and Click.

Similar Reads

Argparse

Argparse is a user-friendly command line interface. The argparse module parses the command-line arguments and options/flags....

Docopt

...

Click

Docopt creates command line interface for the command line app, it automatically generates a parser for it. The main idea of docopt is to describe the interface literally with text, as in docstring....

Conclusion

...