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 makes it easy to define and parse these arguments. In this guide, we'll explore a minimal example of using argparse to create a script that accepts CLI arguments.

Example Code


python
import argparse

def main():
    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()

    for _ in range(args.count):
        print(f'Hello,

---

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

---

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

Top comments (0)