Skip to main content

ChatStyle

ChatStyle 是从 ChatTool 实践中独立出来的 CLI 交互风格与运行时工具包。它提供 prompt、choice、output、flow、mask、interactive 策略和 CommandSchema runtime,让新的 CLI 项目可以复用统一的缺参补问、-i/-I、TTY 判断、默认值和校验流程。

当前版本是 0.2.1。

能力

  • chatstyle.input:命令输入声明、缺参补问解析、Click -i/-I 接入,以及标准 --tree / --tree-brief 命令树输出。
  • chatstyle.tui:文本、路径、确认、单选、多选 prompt,以及 choice/separator 适配。
  • chatstyle.render:标题、提示、状态、建议命令、表格、列表、摘要和多步骤 flow 展示。
  • chatstyle.security:敏感值脱敏、当前值提示和敏感输入。
  • chatstyle.core:TTY、interactive 三态策略、共享常量和错误 helper。
  • chatstyle.patterns:跨模块组合模式,例如多来源值解析、文本/敏感值补问。

代码结构

ChatStyle 按使用职能组织代码,避免把所有 runtime 文件平铺在顶层。顶层 chatstyle 仍提供常用 API 聚合入口;需要按职能导入时,使用下面的子包。

src/chatstyle/
├── __init__.py          # 常用 public API 聚合入口,例如 CommandSchema、ask_text、render_success
├── input/               # CLI 输入声明、解析和 Click 集成
│   ├── schema.py        # CommandField / CommandSchema / CommandConstraint
│   ├── resolve.py       # resolve_command_inputs:缺参补问、默认值、校验、TTY 策略
│   ├── click.py         # add_interactive_option:统一 -i/-I option
│   └── tree.py          # render_click_tree / add_tree_option:统一 --tree / --tree-brief
├── tui/                 # 终端交互输入原语
│   ├── prompt.py        # text/path/confirm/select/checkbox prompt
│   └── choice.py        # choice、separator、questionary adapter
├── render/              # 业务中立的输出和流程展示
│   ├── output.py        # heading、note、status、table、summary、suggested commands
│   └── flow.py          # stage、plan、dry-run、config priority/source
├── security/            # 敏感值处理
│   └── mask.py          # mask_secret、current secret hint、secret prompt
├── core/                # 底层策略和共享常量
│   ├── constants.py     # -i/-I 文案、BACK_VALUE、checkbox indicator
│   ├── interactive.py   # TTY 检测和 interactive 三态解析
│   └── errors.py        # Click 友好的错误 helper
└── patterns.py          # 跨模块组合模式:多来源值解析、文本/敏感值补问

推荐导入方式:

from chatstyle import CommandField, CommandSchema, resolve_command_inputs
from chatstyle.input import add_interactive_option, add_tree_option
from chatstyle.tui import ask_select
from chatstyle.render import render_success
from chatstyle.security import mask_secret

板块

Command Schema Runtime

schema、resolve、click 和 tree 组成声明式命令输入层。它负责字段声明、默认值、缺参补问、字段校验、跨字段约束、-i/-I 接入,以及从 Click 注册面渲染标准 --tree / --tree-brief。

交互是逐命令按需接入的可复用代码,不要求所有命令都带提示。CommandField(..., param_type=click.IntRange(1, 10)) 可复用Click范围、选择、路径等类型,让命令行值、默认值和人工输入接受相同校验,不必在每个包重写。转换失败只报告字段,不保留可能含敏感值的原异常链。-I只禁止提问,不是dry-run;是否写入仍由业务动作与独立的--dry-run决定。

Prompt And Choice

prompt 和 choice 提供文本输入、路径输入、确认、单选、多选、全选控制和 choice/separator 构造。questionary、prompt_toolkit 延迟导入,不安装时 fallback 到 Click。

Output And Flow

output 负责通用标题、提示、状态、建议命令、表格、列表、摘要和优先级链展示,Rich 可用时使用 Rich,不可用时 fallback 到 Click。flow 负责多步骤 CLI 流程的阶段、成功、警告、失败、计划和 dry-run 展示。setup 类需求通过通用 flow/output 组合实现,不单独作为核心模块。

Mask And Interactive Policy

mask 负责敏感值脱敏和敏感输入。interactive、errors、constants 负责 TTY 判断、interactive 状态、共享文案和错误展示。CommandSchema 自动模式统一遵守 CHATARCH_AUTO_PROMPT;值为 0、false、no 或 off 时不自动补问,显式 -i 仍优先。

安装

本地开发:

pip install -e ".[dev]"

项目依赖:

dependencies = ["chatstyle"]

可选 TUI 增强依赖:

dependencies = ["chatstyle[tui]"]

核心包只强依赖 Click。rich、questionary、prompt_toolkit 用于增强展示和选择体验;未安装时会 fallback 到 Click 文本交互。

最小示例

import click

from chatstyle import (
    CommandField,
    CommandSchema,
    add_interactive_option,
    resolve_command_inputs,
)


DEMO_SCHEMA = CommandSchema(
    name="demo",
    fields=(
        CommandField("name", prompt="name", required=True),
        CommandField("output", prompt="output path", kind="path", default="./out.txt"),
        CommandField("token", prompt="token", sensitive=True, prompt_if_missing=True),
    ),
)


@click.command()
@click.option("--name", required=False)
@click.option("--output", required=False)
@click.option("--token", required=False)
@add_interactive_option
def demo(name, output, token, interactive):
    values = resolve_command_inputs(
        schema=DEMO_SCHEMA,
        provided={"name": name, "output": output, "token": token},
        interactive=interactive,
        usage="Usage: demo [--name TEXT] [--output PATH] [--token TEXT] [-i|-I]",
    )
    click.echo(f"run demo for {values['name']} -> {values['output']}")

文档

正式文档:https://arch.gh.wzhecnu.cn/ChatStyle/

pip install -e ".[docs]"
mkdocs serve

文档使用 mkdocs-static-i18n 的 suffix 模式:

  • 中文默认站点使用 docs/*.md。
  • 英文站点使用 docs/*.en.md,构建后位于 /en/。
  • Material 语言切换由 i18n plugin 生成。

更多内容:

  • docs/quickstart.md:添加新 CLI 和封装新交互接口的快速开始。
  • docs/modules.md:模块板块和职责边界。
  • docs/conventions.md:交互约定和行为规范。
  • docs/development.md:开发规范和维护规则。
  • docs/interaction-runtime.md:runtime 边界与下游用法。

本地检查

python -m pytest -q
python -m build
mkdocs build --strict

Metadata

Release files for chatstyle 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for chatstyle 0.2.1
File Size Uploaded Provenance Actions
chatstyle-0.2.1.tar.gz 59.2 kB With provenance Details for chatstyle-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for chatstyle 0.2.1
File Interpreter ABI Platform Actions
chatstyle-0.2.1-py3-none-any.whl Python 3 none any Details for chatstyle-0.2.1-py3-none-any.whl

Total release size: 84.2 kB

Release files / chatstyle-0.2.1.tar.gz

Download URL chatstyle-0.2.1.tar.gz
Size 59.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7d008dcc9ec933d33d7725a857d0a9e69261bd11e51911696356cac0d2a81530
BLAKE2b-256 checksum
How to use checksums
98e8ab705279ed7dcf12dd50c7a5bc7907616912eb09ce139983482b6da10790
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / chatstyle-0.2.1-py3-none-any.whl

Download URL chatstyle-0.2.1-py3-none-any.whl
Size 25.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e6bfc4bdad0ac00d44920597c61c8ad00aa6721b0b3dfb9cc11157f5701af7c3
BLAKE2b-256 checksum
How to use checksums
d64d26be668154d3b15fce83c16b82c6b345b93f02613f049051e6481e5dd146
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for chatstyle 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for chatstyle-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page