Intuitive CLI framework using argparse, rich, and pydantic
Project description
Treeparse
CLI framework that makes any tool immediately usable as an LLM skill.
Overview
Treeparse is a Python CLI framework built on argparse, rich, and pydantic. Its primary purpose is to let an LLM discover and use any CLI tool without additional wiring: one call to --json returns the full command structure in a machine-readable format, and the LLM can invoke commands from there.
Every treeparse CLI supports two built-in help modes:
--help/-h: Rich tree output, human-readable, with branch pruning for subcommands.--json/-j: Structured JSON of the entire CLI tree — commands, arguments, options, types, defaults, choices.
An LLM receives the JSON in a single call and has everything it needs to use the tool as a skill: command names, argument types, defaults, and valid choices. No documentation, no prompt engineering, no extra integration layer.
Key Features
- Object-Oriented Structure: Define CLIs 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 full CLI structure for LLM parsing. - Parameter Validation: Automatic matching of callback params to CLI definitions (names, types).
- Folding: Collapse deep subgroups in help output with
group.fold = True(shows asgroup [...]). - Pluggability & Composition: Full CLIs can be nested and reused as subgroups; supports YAML config and color themes.
- Sorting:
sort_keyfor ordering elements in help output. - Type and Default Display:
show_typesandshow_defaultsflags (both on by default). - Nargs Support: Handles variable arguments/options (e.g.,
*,+for lists). - Boolean Handling: Supports bool types with string-to-bool conversion and
store_trueflags.
Usage Example
From examples/basic.py:
from treeparse import cli, command
def hello():
print("Hello, world!")
app = cli(
name="basic",
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
LLM Skill Integration
Call --json once to get the full CLI structure:
python mytool.py --json
{
"name": "basic",
"help": "A basic CLI example.",
"type": "cli",
"options": [],
"arguments": [],
"subgroups": [
{
"name": "example",
"type": "group",
"commands": [
{
"name": "hello",
"help": "Print hello world from the group.",
"type": "command",
"arguments": [
{
"name": "name",
"arg_type": "str",
"default": null
}
]
}
]
}
],
"commands": [
{
"name": "hello",
"help": "Print hello world.",
"type": "command"
}
]
}
The LLM sees the full tree: groups, commands, argument names, types, defaults, and choices. No additional documentation or integration code is needed.
Advanced Features
Folding
Keep large CLIs readable by folding groups:
user_group = group(
name="user",
help="User management commands",
fold=True, # Shows as "user [...]" in help tree
commands=[greet_cmd, list_cmd],
)
Users and LLMs can still explore the subtree with myapp user --help or myapp user --json.
Pluggability & Composition
Embed entire CLIs as subgroups:
from treeparse import cli
from basic import app as basic_app
from demo import app as demo_app
super_app = cli(
name="supertool",
help="Combined CLI",
subgroups=[basic_app, demo_app], # Full CLIs reused
)
See examples/basic_super.py for a live example.
Other pluggable features:
- Color Themes:
theme="github"(ormonokai,mononeon,monochrome,red_white_blue) - YAML Configuration:
yml_config=Path("config.yml")to override defaults - Shell Completions: Optional
shtabsupport via[completions]extra - Testing:
CliRunnerfor clean pytest integration
Developer Validation
Run validation_error_demo.py to explore rich error messages for name mismatches, type mismatches, and invalid defaults.
Structure
cli: Root (also reusable as a sub-CLI)group: Nested containers with optionalfoldcommand/chain: Executable actionsargument&option: Parameters
Models are modularized; initialization in __init__.py handles forward references.
Current Status
- Implemented: Model definitions, tree/JSON help, validation, nargs, bool support, folding, pluggability (composition, themes, YAML),
CliRunner. - Examples:
basic.py,demo.py(complex),list_demo.py,basic_super.py(composition),validation_error_demo.py,github_theme_demo.py,monokai_theme_demo.py. - Tests: Extensive pytest coverage (>99%) with dedicated runner tests and help-output snapshot tests.
- Tools: Ruff, pytest-cov.
Recent Improvements (v0.2.x)
- Correctness fixes: inherited options now included in default-vs-choices validation;
option(required=True, default=X)is caught at construction time; callback validation usesinspect.unwrap()so decorated functions are inspected correctly, and async callbacks are rejected with a clear error. - Enum type support:
arg_type=MyEnumautomatically populateschoicesfrom enum member names and converts input strings viaMyEnum[s]. - Help renderer extracted: The 440-line help renderer now lives in
treeparse.utils.help_rendereras an isolated, testable class.cli.pyis ~375 lines (down from ~950). - Parser caching:
build_parser()andget_max_depth()cache their results after the first call; do not mutate acliafter firstrun(). - YAML config scope fixed:
yml_configoverrides now apply to options on all subgroups and commands (not only root-level options); unrecognized config keys emitwarnings.warn. - New color themes:
github(GitHub dark palette — purple/blue/orange/red-pink) andmonokai(classic Sublime Text palette — green/cyan/purple/pink). Seeexamples/github_theme_demo.pyandexamples/monokai_theme_demo.py.
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.2.5.tar.gz.
File metadata
- Download URL: treeparse-0.2.5.tar.gz
- Upload date:
- Size: 1.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66280ed8a41b9884c71e00d0da959547f9274461c9d70491c024ad7eef45784a
|
|
| MD5 |
345c380d9c9ccba8e9e33f4fe4306782
|
|
| BLAKE2b-256 |
ade3ce074c1a3480f2263e97fc7b774cdabc90aac30abfece52d612b6a07fc1c
|
Provenance
The following attestation bundles were made for treeparse-0.2.5.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.2.5.tar.gz -
Subject digest:
66280ed8a41b9884c71e00d0da959547f9274461c9d70491c024ad7eef45784a - Sigstore transparency entry: 1247820135
- Sigstore integration time:
-
Permalink:
wr1/treeparse@156f802d248696ff62d432bc20cefa86c414f096 -
Branch / Tag:
refs/tags/v0.2.5 - Owner: https://github.com/wr1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@156f802d248696ff62d432bc20cefa86c414f096 -
Trigger Event:
release
-
Statement type:
File details
Details for the file treeparse-0.2.5-py3-none-any.whl.
File metadata
- Download URL: treeparse-0.2.5-py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a453d815b951c47eafb11b09323ac876543ac2adf040dd46430456f8fe5b0fa1
|
|
| MD5 |
a0729e7e734715817fcf8a73e2273344
|
|
| BLAKE2b-256 |
90d87b384528e61286b549aaf8636c1796f49d679e63df277231ff5f9d86baec
|
Provenance
The following attestation bundles were made for treeparse-0.2.5-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.2.5-py3-none-any.whl -
Subject digest:
a453d815b951c47eafb11b09323ac876543ac2adf040dd46430456f8fe5b0fa1 - Sigstore transparency entry: 1247820148
- Sigstore integration time:
-
Permalink:
wr1/treeparse@156f802d248696ff62d432bc20cefa86c414f096 -
Branch / Tag:
refs/tags/v0.2.5 - Owner: https://github.com/wr1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@156f802d248696ff62d432bc20cefa86c414f096 -
Trigger Event:
release
-
Statement type: