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 creating command-line interfaces (CLI) for Python scripts, parsing arguments is crucial. The argparse module makes it easy to write user-friendly CLI tools. In this guide, we'll explore a minimal example to get you started.

Basic Example

Here's a simple example that demonstrates how to use argparse to parse two command-line arguments: --name and --count.


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()

print(f"

---

*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)