Files
Chromy/chromy/cli_parser.py
T

117 lines
3.4 KiB
Python
Raw Normal View History

2026-04-21 17:42:37 +02:00
from __future__ import annotations
2026-04-21 14:32:10 +02:00
import argparse
2026-04-21 17:42:37 +02:00
from dataclasses import dataclass
2026-04-21 14:32:10 +02:00
2026-04-21 17:42:37 +02:00
@dataclass(frozen=True, slots=True)
class ArgumentSpec:
name: str
help: str
2026-04-21 21:26:40 +02:00
required: bool = False
metavar: str | None = None
2026-04-21 14:32:10 +02:00
2026-04-21 17:42:37 +02:00
@dataclass(frozen=True, slots=True)
class CommandSpec:
name: str
aliases: tuple[str, ...]
help: str
arguments: tuple[ArgumentSpec, ...] = ()
2026-04-21 14:32:10 +02:00
2026-04-21 17:42:37 +02:00
COMMAND_SPECS: tuple[CommandSpec, ...] = (
CommandSpec(
name="list-collections",
aliases=("lc",),
help="List all collections stored in the local Chroma database.",
),
CommandSpec(
name="create-collection",
aliases=("cc",),
help="Create a collection in the local Chroma database.",
2026-04-21 17:55:11 +02:00
arguments=(ArgumentSpec("collection", "Name of the collection to create."),),
2026-04-21 17:42:37 +02:00
),
CommandSpec(
name="delete-collection",
aliases=("dc",),
help="Delete a collection from the local Chroma database.",
2026-04-21 17:55:11 +02:00
arguments=(ArgumentSpec("collection", "Name of the collection to delete."),),
2026-04-21 17:42:37 +02:00
),
CommandSpec(
name="count",
aliases=("co",),
2026-04-21 14:45:01 +02:00
help="Count records in a collection from the local Chroma database.",
2026-04-21 17:55:11 +02:00
arguments=(ArgumentSpec("collection", "Name of the collection to count."),),
2026-04-21 17:42:37 +02:00
),
CommandSpec(
name="add-data",
aliases=("ad",),
2026-04-21 15:28:20 +02:00
help="Chunk, embed, and add a file to a collection in the local Chroma database.",
2026-04-21 17:42:37 +02:00
arguments=(
ArgumentSpec("collection", "Name of the target collection."),
2026-04-21 17:55:11 +02:00
ArgumentSpec(
"file", "Path to the file to chunk and add to the collection."
),
2026-04-21 17:42:37 +02:00
),
),
CommandSpec(
name="query",
aliases=("q",),
help="Query a collection with the provided text.",
arguments=(
ArgumentSpec("collection", "Name of the target collection."),
ArgumentSpec("query_text", "The text to query."),
),
),
2026-04-21 21:26:40 +02:00
CommandSpec(
name="delete",
aliases=("del",),
help="Delete records from a collection using a metadata filter.",
arguments=(
ArgumentSpec("collection", "Name of the target collection."),
ArgumentSpec(
"--where",
"Metadata filter in the format <condition>=<value>.",
required=True,
metavar="CONDITION=VALUE",
),
),
),
2026-04-21 17:42:37 +02:00
)
2026-04-21 17:13:43 +02:00
2026-04-21 17:42:37 +02:00
def _add_command(
subparsers: argparse._SubParsersAction[argparse.ArgumentParser],
command: CommandSpec,
) -> None:
subparser = subparsers.add_parser(
command.name,
aliases=list(command.aliases),
help=command.help,
description=command.help,
2026-04-21 17:13:43 +02:00
)
2026-04-21 17:42:37 +02:00
for argument in command.arguments:
2026-04-21 21:26:40 +02:00
argument_kwargs: dict[str, object] = {"help": argument.help}
if argument.metavar is not None:
argument_kwargs["metavar"] = argument.metavar
if argument.name.startswith("-"):
argument_kwargs["required"] = argument.required
subparser.add_argument(argument.name, **argument_kwargs)
2026-04-21 17:42:37 +02:00
subparser.set_defaults(command=command.name)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Inspect local Chroma collections.")
subparsers = parser.add_subparsers(dest="command", required=True)
for command in COMMAND_SPECS:
_add_command(subparsers, command)
2026-04-21 15:28:20 +02:00
2026-04-21 14:32:10 +02:00
return parser