Your log tool needs to accept --level ERROR --limit 100 from the command line. Python's standard library has a full-featured flag parser waiting. Which module?
argparse? I've seen the word. Never used it in anger.
That's the one. Three steps: create a parser, register each flag with add_argument, then call parse_args(argv) to get a Namespace:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--level")
parser.add_argument("--limit", type=int)
args = parser.parse_args(argv)The type=int tells argparse to coerce "100" into 100 automatically. Strings are the default.
And args is the Namespace? How do I get a plain dict out?
vars(args) converts a Namespace to a dict. So the whole function body is four lines of setup, one line of parse, one line of vars:
args = parser.parse_args(argv)
return vars(args)You get {"level": "ERROR", "limit": 100} — typed values, clean keys, no -- prefix in the keys.
What happens if the user passes --limit abc? Does argparse crash?
It calls parser.error(), which prints a friendly message and exits the process with code 2. For a library function you usually want parse_args to raise instead — wrap the call in a try/except or subclass ArgumentParser. For a user-facing CLI, the auto-exit is what you want.
So argparse gives me validation, type coercion, and a usage message for free. No walking argv by hand.
That's why it's the standard. You register the shape of the flags once and argparse does the rest — parsing, typing, validating, error messaging. One of the best APIs in the stdlib.
argparse.ArgumentParserTL;DR: register flags, call parse_args, convert the Namespace to a dict.
add_argument("--flag", type=int) — auto-coerce to intparse_args(argv) — returns a Namespacevars(namespace) — Namespace → dictdefault= — fallback value when the flag is omittedadd_argument kwargs| Kwarg | Effect |
|---|---|
type=int | coerce string → int |
default="INFO" | default value |
action="store_true" | boolean flag |
required=True | must be present |
Unspecified optional flags come back as None in the dict.
Your log tool needs to accept --level ERROR --limit 100 from the command line. Python's standard library has a full-featured flag parser waiting. Which module?
argparse? I've seen the word. Never used it in anger.
That's the one. Three steps: create a parser, register each flag with add_argument, then call parse_args(argv) to get a Namespace:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--level")
parser.add_argument("--limit", type=int)
args = parser.parse_args(argv)The type=int tells argparse to coerce "100" into 100 automatically. Strings are the default.
And args is the Namespace? How do I get a plain dict out?
vars(args) converts a Namespace to a dict. So the whole function body is four lines of setup, one line of parse, one line of vars:
args = parser.parse_args(argv)
return vars(args)You get {"level": "ERROR", "limit": 100} — typed values, clean keys, no -- prefix in the keys.
What happens if the user passes --limit abc? Does argparse crash?
It calls parser.error(), which prints a friendly message and exits the process with code 2. For a library function you usually want parse_args to raise instead — wrap the call in a try/except or subclass ArgumentParser. For a user-facing CLI, the auto-exit is what you want.
So argparse gives me validation, type coercion, and a usage message for free. No walking argv by hand.
That's why it's the standard. You register the shape of the flags once and argparse does the rest — parsing, typing, validating, error messaging. One of the best APIs in the stdlib.
argparse.ArgumentParserTL;DR: register flags, call parse_args, convert the Namespace to a dict.
add_argument("--flag", type=int) — auto-coerce to intparse_args(argv) — returns a Namespacevars(namespace) — Namespace → dictdefault= — fallback value when the flag is omittedadd_argument kwargs| Kwarg | Effect |
|---|---|
type=int | coerce string → int |
default="INFO" | default value |
action="store_true" | boolean flag |
required=True | must be present |
Unspecified optional flags come back as None in the dict.
Write `parse_flags(argv)` that uses `argparse` to accept `--level` (string) and `--limit` (int) from a list of command-line args. Return `vars(parser.parse_args(argv))` — a dict with keys `level` and `limit`, each either the value or `None`.
Tap each step for scaffolded hints.
No blank-editor panic.