Parse sys.argv Manually
Intermediate · Write the code · from Build Command-Line Tools with argparse and sys.argv
From the lesson: Build Command-Line Tools with argparse and sys.argv
Most Python scripts you've written so far run the same way every time. But what if you want the user to choose what your script does? That's where command-line arguments come in. Instead of hardcoding values, you let users pass them when they run your script.
Read the lessonSimulate a command-line greeting script:
1. Set sys.argv = ['greet.py', 'Alice', 'Bob', 'Charlie']
2. Extract the script name and the list of names (everything after index 0)
3. Print the script name on one line
4. Print a greeting for each name in the format Hello, <name>!
import sys
# Set sys.argv and process the arguments
More argparse exercises
- Build a Basic argparse CLIIntermediate
- Type Validation and ChoicesIntermediate
- Fix the argparse BugIntermediate
- Build a Subcommand CLIIntermediate
All Python exercises · 50 practice problems · Advanced Python quiz