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:
 

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

...