Skip to main content

Intuitive CLI framework using argparse, rich, and pydantic

Project description

ci Version

Treeparse

CLI framework to make command line 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).

Demo scripts

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 / -j provides syntax-highlighted JSON of the CLI structure for LLM parsing.
  • Parameter Validation: Automatic matching of callback params to CLI definitions (names, types).
  • Sorting: sort_key for ordering elements in help outputs.
  • Type and Default Display: Optional show_types and show_defaults flags 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.
  • 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, 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 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treeparse-0.1.6.tar.gz (1.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

treeparse-0.1.6-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file treeparse-0.1.6.tar.gz.

File metadata

  • Download URL: treeparse-0.1.6.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for treeparse-0.1.6.tar.gz
Algorithm Hash digest
SHA256 cccf60bc3caa69ad0349de50478a8c923a182f7066fabcd94cb05c0d6e64d211
MD5 4238e082158b8611b2ba95a0ee73d040
BLAKE2b-256 dee5f7b11a02032ec78f962c9c660eaffd5eb3abb3807ec5828140394c3171b9

See more details on using hashes here.

Provenance

The following attestation bundles were made for treeparse-0.1.6.tar.gz:

Publisher: publish.yml on wr1/treeparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file treeparse-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: treeparse-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 15.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for treeparse-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 2e867eed36676c1bae9f9721aaa923128611a150ebac3f2ff872fc251817bbbb
MD5 b5defea11d8ba9217dc3f0b510c25ad6
BLAKE2b-256 1c52b139a1a66319ca0937466de4b61a093657479e3b92c4ffb84eda9de72afd

See more details on using hashes here.

Provenance

The following attestation bundles were made for treeparse-0.1.6-py3-none-any.whl:

Publisher: publish.yml on wr1/treeparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page