Writing command-line tools in Python: argument parsing
This article covers:
Python is a great language to build command-line tools in as it’s very expressive and concise.
You want to have the ability to parse arguments in your scripts as you don’t want to hard-code the relevant variable values to make the tool useful.
So how do we go about this in Python? It’s easily done using the argparse
module.
With argparse
, you define the different arguments which you expect, their default values and their shortcuts to call them.
Let’s say we are building a tool which creates a folder and downloads some file from the internet to that folder. It could look like this:
import requests
import os
import shutil
folder = 'new_folder'
url = 'https://www.paepper.com/LogoPaepperTransparentSmall.png'
target = 'logo.png'
os.makedirs(folder, exist_ok=True)
os.chdir(folder)
result = requests.get(url, stream=True)
with open(target, 'wb') as f:
result.raw.decode_content = True
shutil.copyfileobj(result.raw, f)
We can run it by calling python create_download.py
and it will create the folder new_folder
and inside an image called logo.png
.
Now let’s make it more useful by being able to specify the different parts (folder, url, filename) using arguments to our tool:
import argparse, requests, os, shutil
parser = argparse.ArgumentParser(description='Create a folder and download a file to it')
parser.add_argument('-f', '--folder', type=str, help='The folder to create', default='new_folder')
parser.add_argument('-u', '--url', type=str, help='The URL to download from', default='https://www.paepper.com/LogoPaepperTransparentSmall.png')
parser.add_argument('-t', '--target', type=str, help='Target name to which the file should be saved', default='logo.png')
args = parser.parse_args()
os.makedirs(args.folder, exist_ok=True)
os.chdir(args.folder)
result = requests.get(args.url, stream=True)
with open(args.target, 'wb') as f:
result.raw.decode_content = True
shutil.copyfileobj(result.raw, f)
Using argparse
, we are now able to change all the parts without touching the code again by passing the proper arguments, e.g.:
python -f other_folder -t download.png create_download.py
This will create the folder other_folder
and inside the image download.png
.
Alternatively, you could use the long, more-detailed arguments:
python --folder other_folder --target download.png create_download.py
Another cool thing about argparse
is that it automatically generates the help info if you call your program using -h
or --help
:
comments powered by Disqus