Skip to content
Related Articles
Open in App
Not now

Related Articles

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

Improve Article
Save Article
  • Last Updated : 11 Aug, 2021
Improve Article
Save Article

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.


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


There many ways to install argparse module, the easy way is by using pip

$ pip install argparse

Initialize Argparse

import argparse
parser=argparse.ArgumentParser(description="Program description")

Adding positional/optional arguments: Using add_argument() we can add positional/optional parameters to the parser.


Here, -parameterName is a short hand notation. 
–optionalName is the optional parameter. 
-h –help monitors the help.

Command Line Usage

$ python [file].py [command] [options] name



import argparse
if __name__=='__main__':
    parser=argparse.ArgumentParser(description="Simple calculator")
    #Adding optional parameters
                        help="Number 1",
                        help="Number 2",
    #Parsing the argument
    #Initialize result to None
    result =None
    #Simple calculator operations
    if args.operation == '+':
    if args.operation == '-':
    if args.operation == '/':
    if args.operation == '*':
    if args.operation == 'pow':
    #Print the result  
    print("Result = " ,result)




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


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

$pip install docopt 

It is most commonly used to display the help messages and it is invoked with -h or –help option. docopt gives you strong control over your help page and it consists of the usage keyword which is case-insensitive that is followed by the program name. A simple usage pattern is as follows: 

Usage : my_program command --option  


#usage pattern
Usage: command --option <argument> <argument> <repeating-argument> --version
  -h, --help     Display help
  -o, --option   Display options
  -l, --all      List all
  -q, --quit     exit
  --version      Version 3.6.1
from docopt import docopt



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


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

$pip install click



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




After seeing the implementations of the above library we can conclude that: 

  • Argparse: It is a standard library (included with Python) and very simple to use because of the work that happens behind the scenes. For example, this library can differentiate between both the arguments and options that are defined using add_arguments() method automatically.
  • Docopt: This library is used for when writing documentation. Moreover, this library is used in multiple languages i.e. you can learn this library and use it in multiple languages.
  • Click: This library provides the decorator style implementation which is very useful as you can decorate the function you want to use. This also makes reading of code very easy. 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!