DEV Community

qing
qing

Posted on

2-Minute Guide: argparse — CLI Arguments for Python Scripts (2026)

2-Minute Guide: argparse — CLI Arguments for Python Scripts

When writing Python scripts, it's often necessary to pass command-line arguments to customize the script's behavior. The argparse library provides a powerful and flexible way to handle CLI arguments. In this guide, we'll explore a minimal example of using argparse to parse command-line arguments.

Minimal Example

Here's a simple example of using argparse to parse --name and --count flags:


python
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--name', help='Your name')
parser.add_argument('--count', type=int, help='Number of times to greet')

args = parser.parse_args

---

*Follow me on Dev.to for daily Python tips and quick guides!*

---

*💡 Related: **Content Creator Ultimate Bundle (Save 33%)** — $29.99*

---

## 🔗 Recommended Resources

- [Python Crash Course](https://www.amazon.com/Python-Crash-Course-2nd-Edition/dp/1593279280?tag=automoney-20) — *3-10%*

*Note: Some links are affiliate links. Using them supports this blog at no extra cost to you.*
Enter fullscreen mode Exit fullscreen mode

Top comments (0)