DEV Community

qing
qing

Posted 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自动化内容!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)