Argparse VS Docopt VS Click – Comparing Python Command-Line Parsing Libraries
Last Updated :
11 Aug, 2021
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
Argparse is a user-friendly command line interface. The argparse module parses the command-line arguments and options/flags.
Installation:
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.
parser.add_argument(‘-parameterName’,’–optionalName’,help=”message”)
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
Example:
Python3
import argparse
if __name__ = = '__main__' :
parser = argparse.ArgumentParser(description = "Simple calculator" )
parser.add_argument( '-n1' ,
'--num1' ,
help = "Number 1" ,
type = float )
parser.add_argument( '-n2' ,
'--num2' ,
help = "Number 2" ,
type = float )
parser.add_argument( '-op' ,
'--operation' ,
help = "operator" ,
default = "*" )
args = parser.parse_args()
print (args)
result = None
if args.operation = = '+' :
result = args.num1 + args.num2
if args.operation = = '-' :
result = args.num1 - args.num2
if args.operation = = '/' :
result = args.num1 / args.num2
if args.operation = = '*' :
result = args.num1 * args.num2
if args.operation = = 'pow' :
result = pow (args.num1,args.num2)
print ( "Result = " ,result)
|
Output:

Docopt
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.
Installation:
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
Example:
Python3
usage =
from docopt import docopt
args = docopt(usage)
print (args)
|
Output:

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
import click
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' )
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
click.echo( "Result is %f" % result)
if __name__ = = '__main__' :
calculator()
|
Output:

Conclusion
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.
Similar Reads
Command-Line Option and Argument Parsing using argparse in Python
Command line arguments are those values that are passed during the calling of the program along with the calling statement. Usually, python uses sys.argv array to deal with such arguments but here we describe how it can be made more resourceful and user-friendly by employing argparse module. Python
8 min read
Command Line Interface Programming in Python
This article discusses how you can create a CLI for your python programs using an example in which we make a basic "text file manager". Let us discuss some basics first. What is a Command Line Interface(CLI)? A command-line interface or command language interpreter (CLI), also known as command-line
7 min read
How to parse boolean values with `argparse` in Python
Command-line arguments are a powerful feature of many programming languages, including Python. They allow developers to specify options or parameters when running a script, making it more flexible and customizable. However, the process of parsing these arguments can be a tedious and error-prone task
5 min read
Mastering Python Libraries for Effective data processing
Python has become the go-to programming language for data science and data processing due to its simplicity, readability, and extensive library support. In this article, we will explore some of the most effective Python libraries for data processing, highlighting their key features and applications.
7 min read
Python | How to Parse Command-Line Options
In this article, we will discuss how to write a Python program to parse options supplied on the command line (found in sys.argv). Parsing command line arguments using Python argparse module The argparse module can be used to parse command-line options. This module provides a very user-friendly synta
3 min read
Python2 vs Python3 | Syntax and performance Comparison
Python 2.x has been the most popular version for over a decade and a half. But now more and more people are switching to Python 3.x. Python3 is a lot better than Python2 and comes with many additional features. Also, Python 2.x is becoming obsolete this year. So, it is now recommended to start using
4 min read
Python | Execute and parse Linux commands
Prerequisite: Introduction to Linux Shell and Shell Scripting Linux is one of the most popular operating systems and is a common choice for developers. It is popular because it is open source, it's free and customizable, it is very robust and adaptable. An operating system mainly consists of two par
6 min read
How to pass a list as a command-line argument with argparse?
Argparse is a Python library used to parse command-line arguments in a user-friendly manner. It makes it easy to write user-friendly command-line interfaces, and it is widely used in Python applications. In this tutorial, we will discuss how to pass a list as a command-line argument using the Argpar
5 min read
Choosing the Right IDE for Python Development: A Comparison
Python, being one of the most popular programming languages, has a rich ecosystem of development environments. Selecting the right Integrated Development Environment (IDE) for Python development can greatly enhance your productivity, streamline coding workflows, and improve debugging processes. Diff
5 min read
Command Line Arguments in Python
The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are: Table of Content Using sys.argvUsing getopt moduleUsing
5 min read