- Python Snacks
- Posts
- Introduction to Python's argparse library
Introduction to Python's argparse library
Understanding Command-Line Arguments in Python


Being comfortable using the command line is essential for any software developer, especially when building tools and utilities that need to be run repeatedly, automated, and/or integrated into larger systems.
While graphical interfaces can be helpful, they’re not always practical, and sometimes they’re hard to develop for internal tooling. Instead, we can resort to the command line and leverage a built-in package: argparse.
It’s a Python library that:
Accepts and parses command-line arguments.
Automatically generates help messages (-h / --help).
Converts arguments into Python variables for you.
» argparse does rival click, which is an alternative package for command line application creation. Check out the differences here.
Building a command line application using argparse is straight-forward:
import argparse
# Create parser
parser = argparse.ArgumentParser(
description="Area Calculator"
)
# Add arguments (positional)
parser.add_argument(
"length", type=float, help="Length in cm"
)
parser.add_argument(
"width", type=float, help="Width in cm"
)
# Parse arguments
args = parser.parse_args()
# Perform the calculations
area = args.length * args.width
print(f"The area is: {area} cm²")
First, we’ll instantiate an argparse.ArgumentParser class, which allows us to add arguments (add_argument) and also parse them (parse_args).
Here, we’re adding 2 arguments: length and width. These correlate to where we’ll read them in on the command line. then we’ll parse them using parse_args, calculate the area, and print it.
In the command line, you’ll run this script using the command:
[user]$ python area.py 5.0 3.2
# Output: The area is: 16.0 cm²
One advantage of the package is that it comes built-in with a help menu. To do so, pass in the -h flag:
[user]$ python area.py -h
'''
Output:
Area Calculator
positional arguments:
a Length in cm
b Width in cm
'''
Understanding the parameters in add_argument
In our example above, we have the following line:
parser.add_argument(
"width", type=float, help="Width in cm"
)
The first parameter is the name of the argument - here, it’s width. This will be used as a variable after parsing and for users to see when they pull up the help menu.
The second command is type. It’s important to know that argparse by default will be a string, unless this parameter is set. You can pass in int, float, str, bool, complex or pass any callable/custom function that does the conversion.
The third command is help, which is used to show what belongs in the help menu.
Other keyword arguments
Here are some of the keyword arguments you can use with the add_argument function:
Keyword Args | Purpose | Usage Example |
---|---|---|
type | Converts the input into the given type | parser.add_argument("a", type=float) |
help | Help text shown in -h | parser.add_argument("a", type=float, help="Length in cm") |
default | Sets a fallback if no value is given | parser.add_argument("a", type=float, default=1) |
required | Makes an optional arg mandatory | parser.add_argument("a", type=float, required=True) |
choices | Restrict values to a set | parser.add_argument("--mode", choices=["fast", "slow"]) |
nargs | Number of arguments to accept (?, *, +, or int) | parser.add_argument("files", nargs="+") |
action | Special behavior: store, store_true, append, count, etc. | parser.add_argument("--debug", action="store_true") |
Summary
argparse allows you to pass input via the CLI and convert it into your desirable data type. With argparse, you can:
View the help message with ‘-h’
Pass any callable or custom function in type which converts a string into any type
Leverage variety of built-in parameters like default, action, choices to make your code readable.
Final thoughts: Along with being a beginner-friendly way to build command-line interfaces in Python, argparse also validates input, handles types, and generates helpful usage messages automatically, making it a solid, production-ready solution.
Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.
Reply