How to Build a Simple Python CLI with argparse
Create a friendly command-line greeting tool with argparse that accepts a positional name and optional flags for custom greetings and uppercase output.
Python code
30 linesimport argparse
def greet(name, greeting="Hello", uppercase=False):
message = f"{greeting}, {name}!"
return message.upper() if uppercase else message
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="A simple greeting tool to demonstrate argparse basics."
)
parser.add_argument(
"name",
type=str,
help="The person's name to greet."
)
parser.add_argument(
"-g", "--greeting",
type=str,
default="Hello",
help="Custom greeting word (default: Hello)."
)
parser.add_argument(
"-u", "--uppercase",
action="store_true",
help="Print the greeting in uppercase."
)
args = parser.parse_args()
result = greet(args.name, args.greeting, args.uppercase)
print(result)
Output
$ python greet.py Ada
Hello, Ada!
$ python greet.py Ada -g Hi
Hi, Ada!
$ python greet.py Ada --uppercase
HELLO, ADA!
$ python greet.py --help
usage: greet.py [-h] [-g GREETING] [-u] name
A simple greeting tool to demonstrate argparse basics.
positional arguments:
name The person's name to greet.
options:
-h, --help show this help message and exit
-g GREETING, --greeting GREETING
Custom greeting word (default: Hello).
-u, --uppercase Print the greeting in uppercase.
How it works
The argparse module parses command-line arguments automatically, converting them into a namespace object. The parser.add_argument calls define the expected inputs: the first is a positional argument (required), while the others are optional flags with defaults. The store_true action sets args.uppercase to True when the flag is present, otherwise False. This pattern separates argument parsing from the greet function, keeping the logic testable and reusable. Using if __name__ == "__main__" guards the execution so the script can be imported without side effects.
Common mistakes
- Omitting `args = parser.parse_args()` before accessing arguments
- Confusing the order of positional vs. optional arguments in the help output
- Forgetting to import argparse at the top of the script
- Assuming a flag variable holds a string instead of a boolean when using `store_true`
Variations
- Use `required=True` on an optional argument to force it, or rely on a positional.
- Add a `--version` flag with `action="version"` for a quick version check.
Real-world use cases
- A CI/CD script that takes a service name as input and optionally flags a dry-run mode.
- A data pipeline CLI that accepts a file path and a `--verbose` flag to toggle logging.
- A small internal tool that greets users with a custom message and optional uppercase for branding.
Sponsored
More from Automation & scripting
- Aggregate Log Errors Count by Hour in Python easy
- Automate Tweeting New Blog Posts in Python easy
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
Keep learning
Related tutorials and quizzes for this topic.