Intuitive CLI framework using argparse, rich, and pydantic
Project description
Treeparse
CLI framework to make tools maximally transparent to LLMs.
Overview
Treeparse is an intuitive CLI framework leveraging argparse, rich, and pydantic to create structured, testable command-line interfaces. It mirrors the tree-like help output of treeclick but uses argparse for parsing and pydantic for model-based definitions.
Key goals include speed, LLM transparency (JSON and rich tree help formats), ease of authoring (especially for AI-generated code).
Key Features
- Object-Oriented Structure: Define CLI using Pydantic models:
cli,group,command,argument,option. - Rich Tree Help: Tree-structured help with branch pruning for subcommands, including higher levels.
- JSON Output:
--json/-jprovides syntax-highlighted JSON of the CLI structure for LLM parsing. - Parameter Validation: Automatic matching of callback params to CLI definitions (names, types).
- Sorting:
sort_keyfor ordering elements in help outputs. - Type and Default Display: Optional
show_typesandshow_defaultsflags to include types and defaults in help. - Nargs Support: Handles variable arguments/options (e.g.,
*,+for lists). - Boolean Handling: Supports bool types with string-to-bool conversion; flag options (
store_true). - Argparse Abstraction: Users work with models; parsing logic is hidden.
- Dynamic Alignment: Help text aligns vertically, adjusting for type/default info.
Structure
- cli: Root with subgroups, commands, options, configs (e.g.,
show_types,show_defaults). - group: Nested groups with subgroups, commands, options.
- command: Commands with callbacks, arguments, options.
- argument: Positional args (type, nargs, default, etc.).
- option: Flags/options (type, nargs, is_flag, default, etc.).
Models are modularized into files: argument.py, option.py, command.py, group.py, color_config.py, cli.py.
Initialization in __init__.py rebuilds models to handle forward references.
Usage Example
From examples/basic.py:
from treeparse import cli, command
def hello():
print("Hello, world!")
app = cli(
name="basic.py",
help="A basic CLI example.",
commands=[
command(
name="hello",
help="Print hello world.",
callback=hello,
)
],
)
def main():
app.run()
if __name__ == "__main__":
main()
Run: python examples/basic.py hello
Help Output
Tree-structured help prunes irrelevant branches for subcommands while retaining context. Supports long help text wrapping.
LLM Transparency
- Rich Tree Format: Visual, human/LLM-readable tree.
- JSON Format: Structured, parseable output for LLMs.
Current Status
- Implemented: Model definitions, tree/JSON help, validation, nargs, bool/flag support, type/default display.
- Examples:
basic.py,demo.py(complex),list_demo.py(nargs). - Tests: Extensive pytest coverage (validation, execution, outputs) with cov reporting.
- Tools: Ruff for lint/format, pytest-cov for coverage.
Plan
- [active], make cli (inherit from) a group, so that a higher level cli can be constructed that can add an existing CLI as a sub-cli without any additional code
- when this happens, use the layout options (colors, show_types etc) from the new top level
Planned: Config overriding.
License
MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file treeparse-0.1.3.tar.gz.
File metadata
- Download URL: treeparse-0.1.3.tar.gz
- Upload date:
- Size: 1.8 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11d4e7a9977bdd9da1d4aa49978944b762e53ea37eb87e3b81471c15733b5ddc
|
|
| MD5 |
d8516a156d0368fc4889c034902dc1b8
|
|
| BLAKE2b-256 |
210b478997bb9cc4a7e5dc6e019fa244bd0008569ee3f37de32f51161e25ac2c
|
Provenance
The following attestation bundles were made for treeparse-0.1.3.tar.gz:
Publisher:
publish.yml on wr1/treeparse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
treeparse-0.1.3.tar.gz -
Subject digest:
11d4e7a9977bdd9da1d4aa49978944b762e53ea37eb87e3b81471c15733b5ddc - Sigstore transparency entry: 458324525
- Sigstore integration time:
-
Permalink:
wr1/treeparse@4499ae6a116bab52eb26d0690144ed7982057cd4 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/wr1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4499ae6a116bab52eb26d0690144ed7982057cd4 -
Trigger Event:
release
-
Statement type:
File details
Details for the file treeparse-0.1.3-py3-none-any.whl.
File metadata
- Download URL: treeparse-0.1.3-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f5c25579b86d96135e35b040d0055cf6502c3d3cd4b03fe178006d33bcdadba
|
|
| MD5 |
338bcb2031768ae11cb86fdcf769c876
|
|
| BLAKE2b-256 |
d7107049e45df452c3915858b0d3ff4fffc05f450bbe0779a84567c2ec1e6880
|
Provenance
The following attestation bundles were made for treeparse-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on wr1/treeparse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
treeparse-0.1.3-py3-none-any.whl -
Subject digest:
9f5c25579b86d96135e35b040d0055cf6502c3d3cd4b03fe178006d33bcdadba - Sigstore transparency entry: 458324534
- Sigstore integration time:
-
Permalink:
wr1/treeparse@4499ae6a116bab52eb26d0690144ed7982057cd4 -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/wr1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4499ae6a116bab52eb26d0690144ed7982057cd4 -
Trigger Event:
release
-
Statement type: