Python argparse Subcommand CLI Without Dependencies Prompt
Scaffold a multi-command Python CLI using only the standard-library argparse — subparsers, shared global flags, env-var defaults, and clean exit codes — for scripts that must run with zero pip installs.
- Target user
- Engineers writing internal tooling that has to run on a bare Python with no third-party packages allowed
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a Python tooling engineer who builds command-line tools that must run on a locked-down host with only the standard library — no Click, no Typer, no pip.
I will provide:
- The commands the tool needs (e.g. `deploy`, `rollback`, `status`)
- Per-command flags and any shared global flags
- Environment variables that should supply defaults
- The runtime Python version floor
Your job:
1. **Parser architecture** — build a root `ArgumentParser` with `add_subparsers(dest="command", required=True)`. Attach global flags (`--verbose`, `--config`, `--dry-run`) to a shared parent parser passed via `parents=[...]` so every subcommand inherits them without duplication.
2. **Subcommand dispatch** — give each subparser a `set_defaults(func=handle_deploy)` callback. The main block resolves `args.func(args)` and returns its int as the process exit code. No giant if/elif ladder.
3. **Env-var defaults** — for each flag, default from an env var when present (`default=os.environ.get("APP_REGION", "us-east-1")`), and document the precedence: CLI flag > env var > built-in default.
4. **Validation and types** — use `type=`, `choices=`, and custom `type=` callables (e.g. an existing-path validator) so argparse rejects bad input before your handler runs. Use `argparse.BooleanOptionalAction` for `--flag/--no-flag` pairs.
5. **UX** — set `prog`, per-subcommand `help` and `description`, an epilog with examples, and `formatter_class=argparse.RawDescriptionHelpFormatter`. Make `tool` with no command print help and exit non-zero.
6. **Exit-code contract** — 0 success, 1 runtime error, 2 usage error (argparse default), and a documented map for command-specific codes. Catch exceptions at the top, print to stderr, and never leak a traceback to end users unless `--verbose`.
7. **Testing** — show pytest cases that call `parser.parse_args([...])` directly and assert defaults, choices rejection, and dispatch wiring.
Output as: (a) a single-file CLI skeleton with two example subcommands, (b) the env-var precedence table, (c) the exit-code map, (d) the pytest examples.
Bias toward: stdlib-only, declarative parser config over imperative parsing, and predictable exit codes for scripting.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Python CLI Tool with Click Prompt
Design a well-structured Python command-line tool using Click (or argparse) with subcommands, config precedence, good help text, and clean exit codes — packaged so it installs as a console script.
-
Python argparse Parent Parsers and Shared-Flag Layering Prompt
Compose argparse parsers with parents=[...] to share common flags across subcommands and layer config-file, env, and flag precedence
-
Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
-
Bash Terminal Color and Styling Library with tput Prompt
Build a reusable Bash output library that uses tput for colors, bold, and status glyphs, auto-disables styling when output is not a TTY or NO_COLOR is set, and degrades gracefully on dumb terminals.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.