The world’s leading publication for data science, AI, and ML professionals.

Build command-line interfaces in Python using Argparse

Empower using command-line in Python!

Argparse is an in-built Python library that helps to build command-line interfaces over the Python code. Argparse means parsing the arguments. When I listened to Argparse for the first time I felt intimidated but once I started exploring and learning it became an integral part of my scripts. It provides extra flexible hands to your Python code. You can send your modified parameters, selection of arguments, etc. via command line and Argparse will take care of it. The below figure explains my understanding of Argparse:

Image created by the author
Image created by the author

To explain this in real-time let’s start with a basic example. Let’s assume we have the following script argparsetest.py. The script can perform addition or subtraction of two variables based on the user input.

Here we have defined just two possibilities ‘add’ or ‘sub’. If the user provides neither of those, it will return as an invalid operation(highlighted in yellow) shown in the screenshot below:

The user can pass the argument by just mentioning the below syntax:

More details on the library:

💡 Positional arguments: You need to specify these arguments every time you execute a script. You cannot skip them if the default value is not mentioned which makes them mandatory to provide. For instance, in the example we mentioned before, if we just ask the user to provide the value of b from the Command Line, the script needs to be slightly tweaked as below:

The command in the command line would look something like below:

Command with the positional argument
Command with the positional argument

If we do not specify the "b" input we get an error something like the below:

Command without positional argument
Command without positional argument

if you have multiple positional arguments, it needs to come in specific order of the script. Try it out and see how it works!

💡 Optional arguments: Contradictory to positional, these arguments can be optional and it is ok if not specified. Adding a default value to the positional argument converts into an optional argument as shown in the below script:

As mentioned before if we do not specify the argument, it will take the default value and execute the script without any error

Command without arguments
Command without arguments

💡 Getting help: If you would like to know the required/used arguments in the script you can always get help by just adding "- h". The below screenshot shows how to do it:

💡 Setting number of values: If you would like to provide multiple values as input for a variable you can do that as well. The values passed are read in the form of a list. For example ‘c‘ is added as an argument and define nargs as 3, which means 3 input values are needed.

We pass the values to c through the command line as below:

💡 Setting type of input argument: You can explicitly mention what type of input is expected. For example, let’s just add ‘int’ as the required variable for b. If the user fails to provide input of the required input type the script will throw an error. The script will change something like below:

If I provided string instead of integer to b it will throw an error as below:

In conclusion, argparse library is a perfect way to provide a user-friendly approach via a command-line interface! These are just the basics to get started with using argparse. If there are any other tricks/tips you have please feel free to comment. Thank you for your time!


Follow me on Twitter or LinkedIn. You may also reach out to me via [email protected]


Related Articles