CLI Patterns
Advanced patterns and techniques for building CLIs with params-proto.
Grouped Options with Prefixes
Use @proto.prefix to organize related parameters into logical groups:
Generated help:
This groups options visually and makes help text more readable for complex CLIs with many parameters.
Benefits
- Organization - Related parameters grouped together
- Readability - Help text clearly shows which parameters belong together
- Discoverability - Users can find related options easily
- Scalability - Works well with many parameters
See Advanced Patterns for more on prefixes and singleton behavior.
Custom Program Name
Override the program name displayed in help text:
Generated help:
Without prog, uses the script filename from sys.argv[0].
Use Cases
- Wrapped scripts - When your script is called via a wrapper or alias
- Clarity - Set a clearer name than the actual filename
- Documentation - Use a name that matches your documentation
Testing Help Generation
Access generated help text programmatically for testing and documentation:
Use Cases
- Unit tests - Verify help text is generated correctly
- Documentation generation - Extract help text for docs
- Debugging - Inspect what users will see
- CI/CD - Validate help output in pipelines
Example Test
Edge Cases
Long Parameter Names
Long parameter names are automatically wrapped in help text:
Generated help (wraps nicely):
Numbers in Names
Numbers are preserved in parameter names and converted properly:
Numbers remain in both the parameter name and the CLI argument, making it easy to reference models by version or variant.
Nested Dataclass Configuration
Override fields in nested dataclasses using dot notation:
CLI usage:
Output:
Benefits
- Deep nesting - Works with arbitrary nesting depth (
--model.encoder.layers) - Type conversion - Nested fields are converted to their annotated types
- Kebab-case - Use
--model.hidden-size(kebab) forhidden_size(snake)
Quick Pattern Reference
| Pattern | When to Use | Example |
|---|---|---|
| Basic CLI | Simple scripts with few parameters | @proto.cli def train(lr: float = 0.001) |
| Grouped Options | Many related parameters | Use @proto.prefix classes for organization |
| Nested Configs | Hierarchical configuration | Use nested dataclasses with dot notation |
| Custom Program Name | Wrapped scripts or clarity | @proto.cli(prog="my-tool") |
| Testing | Verify help output | Access __help_str__ attribute |
Related
- CLI Fundamentals - Basic CLI features
- Naming Conventions - How names convert to CLI arguments
- Help Generation - Documentation extraction
Advanced Patterns- Prefixes and singletons- Type System - Type handling and conversion