2-Minute Guide: argparse — CLI Arguments for Python Scripts
When writing Python scripts, it's often necessary to accept command-line arguments to make them more flexible and user-friendly. The argparse module is a built-in Python library that makes it easy to parse command-line arguments.
Minimal Example
Here's a minimal example of using argparse to accept 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'Hello, {args.name}!'
---
*Follow me on Dev.to for daily Python tips and quick guides!*
Top comments (0)