Sub topic 16
When argparse is enough and when to look elsewhere
This chapter assumes you can build a parser with positional arguments, options and subcommands, and that you know how main(argv) and exit codes work. It gives you criteria for deciding whether argparse is the right tool for a project.
What argparse gives you
argparse ships with Python, so a tool that uses it needs no extra install and adds no dependency to keep up to date. It handles the core jobs:
- Positional arguments, options, flags and subcommands.
- Type conversion and
choices. - Generated
--helpand usage text. - Exit status 2 on invalid input.
For scripts, internal tools and small utilities, that is usually all you need. A tool that must run on a machine where you cannot install packages is a strong case for argparse.
Where argparse starts to hurt
These are the usual signs that you have outgrown it:
- Boilerplate grows. Every subcommand needs a parser,
add_argumentcalls and aset_defaultsline. Large tools end up with long parser-building functions. - Interactive input. argparse cannot prompt for a missing value or ask for a hidden password. You write that yourself.
- Shell completion. argparse has no completion support. The third-party
argcompletepackage adds it. - Rich output. Colored help, tables and progress bars are not part of the standard library, so you add them separately.
- Nested command trees. Subparsers can nest, but deep trees and plugin-style commands get awkward.
Common alternatives
| Library | Style | Good fit |
|---|---|---|
| argparse | Imperative `add_argument` calls | Scripts, small tools, no dependencies |
| Click | Decorators on functions | Larger tools with many subcommands, prompts, testing helpers |
| Typer | Type hints on function parameters (built on Click) | Tools where you want the signature to define the interface |
| Fire | Generates a CLI from existing objects | Quick internal wrappers, not polished interfaces |
The same greeting tool in Click looks like this:
import click
@click.command()
@click.argument("name")
@click.option("-c", "--count", default=1, show_default=True,
help="Number of times to greet.")
def main(name, count):
for _ in range(count):
click.echo(f"Hello, {name}!")
if __name__ == "__main__":
main()Click infers the integer type from the default value. The function parameters take the place of args.name and args.count. The cost is a third-party dependency.
A decision path
Moving later is cheap
If you keep your logic in plain functions and use main(argv) only as a thin layer, switching libraries means rewriting the parser. The functions that do the real work stay the same. This is another reason to keep argument parsing apart from the work your tool does.
Exercise
A teammate needs a one-file script that runs on locked-down servers where pip installs are not allowed. It takes a filename and a --dry-run flag. Which library do you pick, and why?
Show answer
argparse. It is in the standard library, so it needs no installation, and a positional argument plus a store_true flag is well within what it handles easily.
Exercise
Your tool has grown to twelve subcommands, and users ask for tab completion and prompts for missing passwords. What are your options?
Show answer
Either stay with argparse and add argcomplete for completion and write the prompts yourself, or move to Click or Typer, which support prompts directly. Because the logic lives in plain functions, only the parser layer needs rewriting.