Flags & Options: -help Me Help You
 
            Welcome to Part 2 of the argparse mastery series — written from real CLI-building experience.
Let’s start with the essential tools: boolean flags, default values, type safety, and clear help messages.
What You’ll Learn
- Boolean flags like --debug,--verbose, etc.
- Setting default values
- Type conversion (int,float,str)
- Writing helpful --helpmessages
- Making your CLI user-friendly & safe
Why This Matters
A command-line tool without options is like a vending machine with one button.
Flags and options turn your CLI into a full menu — letting users control behavior without touching the code.
- Want more output? Use --verbose
- Need troubleshooting info? Use --debug
- Tweak behavior? Use --rateor--level
Welcome to the pro zone.
What Is a Flag?
A flag is an optional switch that toggles something on or off. It doesn't require a value.
Examples:
- --verbose: show more output
- --debug: print internal details for developers
- --dry-run: simulate action without doing it
They’re usually False by default. If the user includes the flag, it becomes True.
What Is an Option with a Value?
These require extra data, such as:
- --level 3→ sets difficulty
- --rate 0.5→ adjusts speed
You can define:
- The expected data type (type=int,type=float, etc.)
- A fallback value with default=
Sample Code
import argparse
parser = argparse.ArgumentParser(description="Demo: Flags and Options in argparse")
# Boolean flags
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
parser.add_argument("--verbose", action="store_true", help="Enable verbose output")
# Options with values
parser.add_argument("--level", type=int, default=1, help="Set difficulty level (default: 1)")
parser.add_argument("--rate", type=float, default=0.5, help="Set processing rate (default: 0.5)")
args = parser.parse_args()
# Logic based on flags
if args.debug:
    print("[DEBUG] Debug mode is ON")
if args.verbose:
    print("[VERBOSE] Verbose output is ON")
# Always show these
print("Level:", args.level)
print("Rate:", args.rate)
Try It Out
$ python3 tool.py --verbose --level 3 --rate 1.2
[VERBOSE] Verbose output is ON
Level: 3
Rate: 1.2
$ python3 tool.py
Level: 1
Rate: 0.5
$ python3 tool.py --debug
[DEBUG] Debug mode is ON
Level: 1
Rate: 0.5
Quick Summary of Each Option
- --debug: Boolean, prints debug details if enabled
- --verbose: Boolean, prints extra output
- --level: Integer, sets difficulty (default = 1)
- --rate: Float, controls speed (default = 0.5)
Pro Tips
- ✅ Use action="store_true"for flags you want OFF by default
- ✅ Use type=to force input to match the expected type
- ✅ Use default=to keep your app safe when users forget values
- ✅ Always write help=text — that’s your CLI’s first impression
💻 CLI Help Output
$ python3 tool.py --help
usage: tool.py [options]
Demo: Flags and Options in argparse
optional arguments:
  --debug         Enable debug mode
  --verbose       Enable verbose output
  --level LEVEL   Set difficulty level (default: 1)
  --rate RATE     Set processing rate (default: 0.5)
Real-Life Use Case
This kind of base setup is great for:
- Batch processing files
- Transforming log entries
- Controlling automation scripts
In short: any script can become a CLI tool with just these building blocks.
Coming Up Next: Part 3 — Validation & Errors
We’ll cover:
- Type enforcement
- Input validation
- Safe defaults
- Defensive CLI design
Because broken input shouldn’t break your script.
