argparse-patterns

Create Python CLIs with argparse subparsers, choices, and validation.

Updated Nov 11, 2025
One-click install
npx skills add https://github.com/vanman2024/cli-builder --skill argparse-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: argparse-patterns
Source: https://github.com/vanman2024/cli-builder/tree/main/plugins/cli-builder/skills/argparse-patterns
Command: npx skills add https://github.com/vanman2024/cli-builder --skill argparse-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) components.

What problem does it solve?

This Skill demonstrates comprehensive Python argparse patterns using only the standard library, including subparsers, choices, actions, and validation.

Core Features & Use Cases

  • Basic Parser & Subparsers: Multi-level commands with nested subparsers.
  • Choices & Validation: Built-in choices, custom validators, and type coercion.
  • Actions & Groups: Mutually exclusive groups and argument grouping.
  • Templates & Examples: Ready-to-adapt Python templates and use cases.

Quick Start

Build a robust CLI with argparse, add nested commands, and validate inputs with custom validators.

Frequently Asked Questions about argparse-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I build a Python CLI with subparsers and nested commands without external dependencies?

Python's argparse module handles multi-level subparsers natively. Define a parent parser, add subparsers with `add_subparsers()`, then attach commands to each subparser. This creates robust nested command structures using only the standard library, no external packages required.

What's the best way to validate command-line arguments in Python argparse?

Argparse supports validation through built-in `choices` for predefined options, custom `type` functions for coercion, and `action` classes for complex logic. Combine these with argument groups and mutually exclusive groups to enforce constraints before your script runs.

Can I use argparse for mutually exclusive options and argument groups?

Yes. Argparse provides `add_mutually_exclusive_group()` to enforce one-of-many constraints and `add_argument_group()` to organize related arguments in help text. Both methods integrate seamlessly with subparsers for hierarchical CLI structures.

How do I handle version information and custom help generation in argparse?

Use the built-in `version` action with `add_argument('--version')` for version output. Customize help formatting by passing `formatter_class` (e.g., `RawDescriptionHelpFormatter`) to the parser, and override help text per argument for control over generated documentation.

Do I need external libraries to create production-grade Python CLIs with argparse?

No. Argparse alone satisfies parser initialization, version handling, help generation, and validation. Custom actions and type functions handle complex scenarios. External dependencies are optional—argparse patterns cover nested commands, choices, and template-driven code generation without them.