DEV Community

qing
qing

Posted on • Edited on

Master CLI Args in 2 Minutes

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 module makes it easy to define and parse these arguments. In this guide, we'll create a minimal example that demonstrates how to use argparse to accept CLI arguments.

Minimal Example

Here's an example script that uses argparse to accept --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 greetings')

args = parser.parse

---

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

---
喜欢这篇文章?关注获取更多Python自动化内容!

---

If you found this useful, you might like **[Python Automation Scripts Pack (10 Ready-to-Use Tools)](https://qssec.gumroad.com/l/python-automation-pack)** — a practical resource that takes things a step further. At $14.99 it's a solid investment for your toolkit.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)