2 minutes
Writing command-line tools in Python: argument parsing
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:
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:
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
: