Creating a Command-Line Interface Program With Argparse
What is Argparse?
Argparse is a module that comes pre-installed when you install Python. It allows for the creation of user-friendly command-line interfaces.
Creating a CLI Program to Find the Area of a Rectangle
To start using argparse to recreate the example above, you will need to import the pacakge:
1
2
3
4
5
# the following code will be contained within a file called area.py
import argparse
Now we will create a simple method to multiply and return two numbers, like so:
1
2
3
4
5
def area_of_rectangle(length, width):
return length * width
This method will be called when the program is executed. The parameters length
and width
will be extracted from the command line arguments.
Now we must create a parser to handle the parsing of command-line arguments along with two positional arguments that we want to extract, i.e. the length and the width of the rectangle.
1
2
3
4
5
6
7
8
9
parser = argparse.ArgumentParser(description='Calculates the area of a rectangle')
parser.add_argument('length', default=0, type=int, help='An integer for the length of the rectangle')
parser.add_argument('width', default=0, type=int, help='An integer for the width of the rectangle')
We can use the values of these arguments by calling upon parser.parse_args()
.
1
2
3
4
5
6
7
args = parser.parse_args()
area = area_of_rectangle(args.length, args.width)
print('The area of your rectangle is: ', area, ' units squared.')
Putting everything together should look like this:
# area.py
import argparse
def area_of_rectangle(length, width):
return length * width
parser = argparse.ArgumentParser(description='Calculates the area of a rectangle')
parser.add_argument('length', default=0, type=int, help='An integer for the length of the rectangle')
parser.add_argument('width', default=0, type=int, help='An integer for the width of the rectangle')
args = parser.parse_args()
area = area_of_rectangle(args.length, args.width)
print('The area of your rectangle is: ', area, ' units squared.')
Thats about it! Argparse will now parse your command-line agruments for integers to be used in our method. Every parser will have a -h
optional argument by default; this option will display the help message along with the parser’s description.