Docopt is a command line interface description module. It helps you define a interface for a command-line application and generates parser for it. The interface message in
docopt is a formalized help message.
Installation
You can install docopt module in various ways, pip is one of the best ways to install docopt.
$pip install docopt
Note: docopt is tested with Python 2.5, 2.6, 2.7, 3.2, 3.3 and PyPy as well.
Initialization
docopt is most commonly used to display the help messages and it is invoked with -h or –help option. You can import and call this module with the following command.
Python3 1==
from docopt import docopt
docopt(doc, argv = None, version = None,
help = True, options = False)
Parameters of the module is as shown below :
- doc : It is a docstring (__doc__) that contains the help message.
- argv : It is an optional argument vector contains list of strings.
- version : It is an optional argument mentioning the version of the program.
- help : Responsible for displaying the help message. It is True by default.
- options_first : This doesn't allow the mixing of positional arguments and optional arguments. By default it is False.
Usage Pattern
docopt gives you strong control over your help page and it consists of the usage keyword which is
case-insensitive that is followed by your program_name. A usage pattern can be described with various elements as mentioned below :
Python3 1==
usage =
'''
Usage :
program_name.py command --option
program_name.py [optional argument]
program_name.py --another-option =<argument>
program_name.py (--option1 | --option2 )
program_name.py <argument>...
'''
< argument > argument : The element starting with "<" and ending with ">" is called a positional argument. It is position sensitive.
--option -o: The element starting with "--" or "-" are called as long or short option. It can either be mentioned as
--option
or
-o
.
-h, --help Display help
-o, --option Display options
-l, --all List all
-q, --quit exit
--version Version 3.6.1
[optional argument] : The element starting with "[" and ending with "]" is called an optional argument. It is considered optionally.
< argument >... : The ellipsis "..." is used when the element present to the left can be repeated more than once.
(required arguments) : The elements starting with "(" and ending with ")" is a required element.
(--option1 | --option2)
says that either of --option1 or --option2 is required.
Example :
Python3 1==
# filename ='docopt_example.py"
usage ='''
Usage:
docopt_example.py [(<name1>|<name2>)] <name3>...
docopt_example.py mov <name1> <name2>
docopt_example.py (--h|--q) [<name1> -l]
docopt_example.py --version
Options:
-l, --all List all.
-q, --quit exit.
--version Version 3.6.1
-h --help Show this screen.
--version Show version.
--speed =<kn> Speed in knots [default: 10].
--moored Moored (anchored) mine.
--drifting Drifting mine.
'''
from docopt import docopt
args = docopt(usage)
print(args)
Output :
Similar Reads
Getopt module in Python The getopt module is a parser for command-line options based on the convention established by the Unix getopt() function. It is in general used for parsing an argument sequence such as sys.argv. In other words, this module helps scripts to parse command-line arguments in sys.argv. It works similar t
2 min read
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
__future__ Module in Python __future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module. I
4 min read
Python Module Index Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Python datetime module In Python, date and time are not data types of their own, but a module named DateTime in Python can be imported to work with the date as well as time. Python Datetime module comes built into Python, so there is no need to install it externally. In this article, we will explore How DateTime in Python
14 min read
Built-in Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include Python built-in modules and ext
9 min read
External Modules in Python Python is one of the most popular programming languages because of its vast collection of modules which make the work of developers easy and save time from writing the code for a particular task for their program. Python provides various types of modules which include built-in modules and external m
5 min read
Python Math Module Math Module consists of mathematical functions and constants. It is a built-in module made for mathematical tasks. The math module provides the math functions to deal with basic operations such as addition(+), subtraction(-), multiplication(*), division(/), and advanced operations like trigonometric
13 min read
Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac
3 min read