npx skills add https://github.com/agentlyhq/skills --skill cli-for-agent在以下情况下使用此技能:
代理不是人类。 它们无法看到颜色、导航交互式菜单、理解旋转动画或响应 TTY 提示。为代理构建的 CLI 必须完全通过 stdout 上的结构化文本、stderr 上的清晰错误消息和有意义的退出代码进行通信。如果代理无法解析您的输出或在一次重试中从您的错误中恢复,那么您的 CLI 就失败了。
永远不要使用交互式提示、确认对话框、readline、光标移动或进度指示器。每个命令都必须在脚本、CI 流水线和代理工具调用链中无需终端即可无人值守地运行。
# 错误 — 阻塞等待输入
Are you sure? [y/N]
# 正确 — 由标志驱动,无交互
$ my-cli delete --confirm resource-id
如果命令具有破坏性,请要求显式的 --confirm 或 --yes 标志,而不是提示。
仅限 TTY 的装饰。 颜色、方框(boxen)、表格、旋转器和进度条是可以的——但仅限于 stdout 是 TTY 时。当被管道传输时,输出必须是纯文本。检查 isatty(stdout)(或您所用语言的等效方法),并在返回 false 时去除所有装饰。切勿在非 TTY 模式下使用方框绘制库(如 )——它们会破坏 和 。
Use this skill when:
Agents are not humans. They cannot see colors, navigate interactive menus, interpret spinner animations, or respond to TTY prompts. A CLI built for agents must communicate entirely through structured text on stdout, clear error messages on stderr, and meaningful exit codes. If an agent can't parse your output or recover from your errors in one retry, your CLI has failed.
Never use interactive prompts, confirmation dialogs, readline, cursor movement, or progress spinners. Every command must run unattended in scripts, CI pipelines, and agent tool-call chains without a terminal.
# Bad — blocks waiting for input
Are you sure? [y/N]
# Good — flag-driven, no interaction
$ my-cli delete --confirm resource-id
If a command is destructive, require an explicit --confirm or --yes flag instead of prompting.
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
boxengrephead您的输出将通过 head、tail、grep、jq、awk 和 wc 进行管道传输。每个设计决策都源于此。
每行一条记录。 每一行必须是一个自包含的意义单元。这是最重要的一条规则——它使得每个标准的 Unix 工具都能免费工作。
对于单个对象响应,输出一行紧凑的 JSON:
$ my-cli status --output json
{"name":"my-service","status":"running","uptime_seconds":13320}
打印数组或大型集合时使用 NDJSON。 当命令返回列表、数组或任何可能的大型记录集合时,请使用 NDJSON(换行分隔的 JSON)——每行一个 JSON 对象,不包裹数组。这使得消费者可以流式处理、grep 和切片,而无需缓冲或解析整个输出。
# NDJSON — 每行都可独立解析
$ my-cli list --output json
{"id":"svc-1","name":"api","status":"running"}
{"id":"svc-2","name":"worker","status":"stopped"}
{"id":"svc-3","name":"gateway","status":"running"}
# 标准工具可以直接使用:
$ my-cli list --output json | head -1 # 第一条记录
$ my-cli list --output json | tail -5 # 最后 5 条记录
$ my-cli list --output json | grep '"running"' # 无需 jq 即可过滤
$ my-cli list --output json | wc -l # 统计记录数
避免将集合包裹在 JSON 数组中([{...},{...}])。数组会强制消费者在访问任何单个记录之前加载并解析整个输出。
何时使用 NDJSON 与单个 JSON 对象:
stdout 仅用于数据——不要有横幅、提示、装饰性方框或日志消息。stderr 用于诊断——进度信息、警告和调试消息应发送到 stderr。将两者混合会破坏管道。
一致的形状——同一个命令应始终返回相同的 JSON 模式,对于缺失的字段使用 null 而不是省略键。代理会根据您的模式硬编码解析器。
错误消息必须包含足够的上下文,以便调用者能在下一次尝试中成功。第三次尝试意味着您的错误消息失败了。
每个错误都应包括:
# 错误
Error: invalid argument
# 正确
Error: <uri> must be a URL (e.g. https://api.example.com/agents/echo)
or a registered short name (e.g. echo-agent).
Run 'my-cli list' to see available agents.
CLI 必须无需外部文档即可描述自身:
my-cli、my-cli --help 和 my-cli help 都应打印完整的命令列表--help 输出中应至少包含一个具体的使用示例$ my-cli --help
Usage: my-cli <command> [options]
Lifecycle:
init Initialize configuration
doctor Run health checks
whoami Show current identity
Resources:
list List available resources
get <id> Get a resource by ID
create Create a new resource
Run 'my-cli <command> --help' for details on a specific command.
使用退出代码,以便代理无需解析输出即可检测成功或失败:
| 代码 | 含义 |
|---|---|
0 | 成功 |
1 | 一般错误(错误的输入、操作失败) |
2 | 用法错误(缺少参数、错误的标志) |
失败时始终以非零值退出。切勿将 "error" 打印到 stdout 并以 0 退出。
代理可能会重试命令。设计命令时要确保重新运行是安全的:
create 应该成功,或者如果资源已存在则返回现有资源(或提供 --if-not-exists 选项)delete 即使资源已经消失也应该成功config set 如果值已经设置,应该是一个空操作在所有命令中使用一致的模式:
my-cli <noun> <verb> [arguments] [--flags]
# 或
my-cli <verb> <noun> [arguments] [--flags]
选择一种模式并坚持使用。常见的标志约定:
--output <format> 或 -o <format> —— 输出格式(json、text、csv)--yes 或 --confirm —— 跳过破坏性操作的确认--quiet 或 -q —— 抑制非必要的输出--verbose 或 -v —— 增加日志详细程度(输出到 stderr)集中处理输出格式化,以便每个命令的行为保持一致。创建一个所有命令都调用的共享辅助函数:
--output json 时,向 stdout 输出每行一个 JSON 对象(NDJSON)--output text(默认)时,向 stdout 写入人类可读的键值行优化冷启动时间。代理在一个会话中会调用 CLI 数百次——500 毫秒的启动惩罚会迅速累积。
将错误包装在一致的格式中,以便代理可以解析并采取行动。每个错误路径都应:
# 示例错误输出(在 stderr 上):
Error: "foobar" is not a valid URI.
Hint: Provide a full URL (e.g. https://example.com/api) or a short name
(e.g. my-agent). Run 'my-cli list' to see available options.
切勿因缺少配置而崩溃。返回合理的默认值,以便命令开箱即用:
--help 输出中记录配置文件的位置| 反模式 | 为何会破坏代理 |
|---|---|
| 交互式提示 | 代理无法按需在 stdin 中键入 |
方框绘制(boxen、边框) | 破坏 grep、head、tail——不可解析的装饰 |
| 管道传输时使用颜色/ANSI | 污染解析后的输出;仅当 stdout 是 TTY 时才使用 |
| 旋转器和进度条 | 干扰 stdout 解析 |
分页(less、more) | 阻塞执行,等待按键 |
| stdout 上混合数据和日志 | 代理无法将数据与噪音分开 |
| 集合使用 JSON 数组 | 强制加载整个输出;对于列表/数组使用 NDJSON |
| 多行美化打印的 JSON | 破坏 grep 和 wc -l;一条记录 = 一行 |
| 不一致的 JSON 形状 | 代理根据您的模式硬编码解析器 |
| 错误时退出 0 | 代理假设成功并继续处理错误状态 |
| 模糊的错误消息 | 代理浪费重试次数猜测问题所在 |
| 启动缓慢(重量级导入) | 在数百次代理调用中累积 |
| 必需的 env 变量没有文档 | 代理无法发现隐式依赖 |
审查或测试面向代理的 CLI 时,请验证:
echo "" | my-cli command 不会挂起)my-cli list --output json | head -1 返回一个有效的、自包含的 JSON 对象my-cli list --output json | grep "keyword" 返回有效的 JSON 行--help 包含每个命令至少一个使用示例每周安装数
1
仓库
首次出现
1 天前
安全审计
安装于
amp1
cline1
opencode1
cursor1
kimi-cli1
codex1
TTY-only decoration. Colors, boxes (boxen), tables, spinners, and progress bars are fine — but only when stdout is a TTY. When piped, output must be plain. Check isatty(stdout) (or your language's equivalent) and strip all decoration when it returns false. Never use box-drawing libraries like boxen in non-TTY mode — they break grep and head.
Your output will be piped through head, tail, grep, jq, awk, and wc. Every design decision flows from this.
One record per line. Each line must be a self-contained unit of meaning. This is the single most important rule — it makes every standard Unix tool work for free.
For a single object response, emit one compact JSON line:
$ my-cli status --output json
{"name":"my-service","status":"running","uptime_seconds":13320}
Use NDJSON when printing arrays or large collections. When a command returns a list, array, or any potentially large collection of records, use NDJSON (Newline-Delimited JSON) — one JSON object per line, no wrapping array. This lets consumers stream, grep, and slice without buffering or parsing the entire output.
# NDJSON — each line is independently parseable
$ my-cli list --output json
{"id":"svc-1","name":"api","status":"running"}
{"id":"svc-2","name":"worker","status":"stopped"}
{"id":"svc-3","name":"gateway","status":"running"}
# Standard tools just work:
$ my-cli list --output json | head -1 # first record
$ my-cli list --output json | tail -5 # last 5 records
$ my-cli list --output json | grep '"running"' # filter without jq
$ my-cli list --output json | wc -l # count records
Avoid wrapping collections in a JSON array ([{...},{...}]). An array forces the consumer to load and parse the entire output before accessing any single record.
When to use NDJSON vs a single JSON object:
stdout is for data only — no banners, tips, decorative boxes, or log messages. stderr is for diagnostics — progress info, warnings, and debug messages go to stderr. Mixing the two corrupts pipelines.
Consistent shape — the same command should always return the same JSON schema, using null for missing fields rather than omitting keys. Agents hardcode parsers against your schema.
Error messages must include enough context for the caller to succeed on the next attempt. A third attempt means your error message failed.
Every error should include:
# Bad
Error: invalid argument
# Good
Error: <uri> must be a URL (e.g. https://api.example.com/agents/echo)
or a registered short name (e.g. echo-agent).
Run 'my-cli list' to see available agents.
The CLI must describe itself without external documentation:
my-cli, my-cli --help, and my-cli help should all print the full command listing
Every command should include at least one concrete usage example in its --help output
Group commands by category in help text so agents (and humans) can find what they need
$ my-cli --help Usage: my-cli <command> [options]
Lifecycle: init Initialize configuration doctor Run health checks whoami Show current identity
Resources: list List available resources get <id> Get a resource by ID create Create a new resource
Run 'my-cli <command> --help' for details on a specific command.
Use exit codes so agents can detect success or failure without parsing output:
| Code | Meaning |
|---|---|
0 | Success |
1 | General error (bad input, failed operation) |
2 | Usage error (missing argument, bad flag) |
Always exit non-zero on failure. Never print "error" to stdout and exit 0.
Agents may retry commands. Design commands to be safe to re-run:
create should succeed or return the existing resource if it already exists (or offer --if-not-exists)delete should succeed even if the resource is already goneconfig set should be a no-op if the value is already setUse consistent patterns across all commands:
my-cli <noun> <verb> [arguments] [--flags]
# or
my-cli <verb> <noun> [arguments] [--flags]
Pick one pattern and stick with it. Common flag conventions:
--output <format> or -o <format> — output format (json, text, csv)--yes or --confirm — skip confirmation for destructive actions--quiet or -q — suppress non-essential output--verbose or -v — increase log detail (to stderr)Centralize output formatting so every command behaves consistently. Create a shared helper that all commands call:
--output json, emit one JSON object per line (NDJSON) to stdout--output text (default), write human-readable key-value lines to stdoutOptimize cold-start time. Agents invoke CLIs hundreds of times in a session — a 500ms startup penalty compounds fast.
Wrap errors in a consistent format that agents can parse and act on. Every error path should:
# Example error output (on stderr):
Error: "foobar" is not a valid URI.
Hint: Provide a full URL (e.g. https://example.com/api) or a short name
(e.g. my-agent). Run 'my-cli list' to see available options.
Never crash on missing config. Return sensible defaults so commands work out of the box:
--help output| Anti-Pattern | Why It Breaks Agents |
|---|---|
| Interactive prompts | Agents cannot type into stdin on demand |
Box drawing (boxen, borders) | Breaks grep, head, tail — unparseable decoration |
| Colors/ANSI when piped | Pollutes parsed output; only use when stdout is a TTY |
| Spinners and progress bars | Interfere with stdout parsing |
Paging (less, more) | Blocks execution waiting for keypress |
| Mixed data and logs on stdout | Agents can't separate data from noise |
| JSON arrays for collections | Forces loading entire output; use NDJSON for lists/arrays |
| Multi-line pretty-printed JSON | Breaks grep and wc -l; one record = one line |
| Inconsistent JSON shape | Agents hardcode parsers against your schema |
| Exit 0 on error | Agents assume success and proceed with bad state |
| Vague error messages | Agents waste retries guessing what went wrong |
| Slow startup (heavy imports) | Compounds across hundreds of agent invocations |
| Required env vars without docs | Agents can't discover implicit dependencies |
When reviewing or testing an agent-facing CLI, verify:
echo "" | my-cli command doesn't hang)my-cli list --output json | head -1 returns a valid, self-contained JSON objectmy-cli list --output json | grep "keyword" returns valid JSON lines--help includes at least one usage example per commandWeekly Installs
1
Repository
First Seen
1 day ago
Security Audits
Installed on
amp1
cline1
opencode1
cursor1
kimi-cli1
codex1
AI Elements:基于shadcn/ui的AI原生应用组件库,快速构建对话界面
60,400 周安装