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*
Top comments (0)