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
# argparse_example.py import argparse if __name__ = = '__main__' : #Initialize parser = argparse.ArgumentParser(description = "Simple calculator" ) #Adding optional parameters 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 = "*" ) #Parsing the argument args = parser.parse_args() print (args) #Initialize result to None result = None #Simple calculator operations 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 the result 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
#docopt_example.py #usage pattern usage = ''' Usage: docopt_example.py command --option <argument> docopt_example.py <argument> <repeating-argument> docopt_example.py --version Options: -h, --help Display help -o, --option Display options -l, --all List all -q, --quit exit --version Version 3.6.1 ''' #Initialization 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
# 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:
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.