算法艺术创作
algorithmic-art
通过代码和可调参数创作生成艺术与交互式视觉效果。
scaffold-cli-command【设计与创意】Scaffold a new CLI command using Commander.js with options, action handler, three output modes (human-readable, quiet, JSON), and optional ceremony variant. Covers command naming, option design, shared context patterns, error handling, and integration testing. Use when adding a command to an existing Commander.js CLI, designing a new CLI tool from sc…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
於 Commander.js CLI 應用中新增命令,附一致之選項處理、三種輸出模式與整合測試。
gather、audit、sync)cli/index.js)<name> 或 [names...])擇一動詞傳達命令動作。將命令分類:
| 類別 | 動詞 | 模式 |
|---|---|---|
| CRUD | install、uninstall、list、search | 操作於內容上 |
| 生命週期 | init、sync、audit | 管理項目狀態 |
| 儀式 | gather、scatter、tend、campfire | 溫暖敘事輸出 |
命名慣例:
install-skill——讓選項指定何物)<required> 或 [optional] 或 [variadic...]program
.command('gather <name>')
.description('Gather a team around the campfire')
預期: 命令名、描述與位置引數已定義。
失敗時: 若動詞與既有命令重疊,組合之(為既有命令加選項)或於描述中清楚區分。
每命令應支援標準共享選項加命令特定選項。
標準選項(依需納入):
.option('-n, --dry-run', 'Preview without making changes')
.option('-q, --quiet', 'Suppress human-readable output')
.option('--json', 'Output as JSON')
.option('-f, --framework <id>', 'Target specific framework')
.option('-g, --global', 'Use global scope')
.option('--scope <scope>', 'Scope: project, workspace, global', 'project')
.option('--source <path>', 'Path to tool root directory')
命令特定選項——僅加命令所需者:
.option('--ceremonial', 'Show each item arriving individually')
.option('--only <items>', 'Comma-separated subset to include')
.option('-y, --yes', 'Skip confirmation prompts')
設計規則:
-n)用於常用選項--dry-run)用於清晰預期: 完整之選項鏈,含標準與自定選項。
失敗時: 若選項累積過多(>8),考慮分為子命令或將相關選項分組。
動作處理器遵循一致模式:
.action(async (name, options) => {
// 1. Get shared context (registries, adapters, paths)
const ctx = getContext(options);
// 2. Resolve what to operate on
const items = resolveItems(ctx, name, options);
if (!items || items.length === 0) {
reporter.error('Nothing found.');
process.exit(1);
}
// 3. Preview if dry-run
if (options.dryRun) reporter.printDryRun();
// 4. Execute the operation
const results = await executeOperation(items, ctx, options);
// 5. Output results (3 modes)
if (options.json) {
console.log(JSON.stringify(results, null, 2));
} else if (options.quiet) {
reporter.printResults(results);
} else {
printHumanOutput(results, options);
}
})
getContext() 共享輔助函數集中:
預期: 動作處理器遵循 5 步模式:context → resolve → preview → execute → output。
失敗時: 若命令不符合解析-然後執行模式(如純資訊性如 detect),簡化為:context → compute → output。
每命令應支援三種輸出模式:
預設(人類可讀):
Installing 3 item(s) to Claude Code...
+ create-skill [claude-code] .claude/skills/create-skill
+ write-tests [claude-code] .claude/skills/write-tests
= commit-changes [claude-code] (skipped)
2 installed, 1 skipped
安靜(--quiet):
標準回報器輸出——簡潔行附狀態圖示(+、-、=、!),無儀式、無裝飾。
JSON(--json):
{
"command": "install",
"items": 3,
"installed": 2,
"skipped": 1,
"failed": 0
}
實作模式:
if (options.json) {
console.log(JSON.stringify(data, null, 2));
return;
}
if (options.quiet) {
reporter.printResults(results);
return;
}
// Default: human-readable output
printHumanReadable(results, options);
預期: 三模式皆產生有用輸出。JSON 可解析。Quiet 簡潔。預設提供資訊。
失敗時: 若命令無有意義之 JSON 表示(如 detect),跳過 JSON 模式並記錄理由。
對受益於溫暖敘事輸出(而非交易報告)之命令:
if (options.json) {
ceremonyReporter.printJson(data);
} else if (options.quiet) {
reporter.printResults(results);
} else {
ceremonyReporter.printArrival({
teamId: name,
agents,
results: { installed, skipped, failed },
ceremonial: options.ceremonial || false,
});
}
儀式輸出遵循聲音規則:
詳細終端輸出模式見 design-cli-output 技能。
預期: 儀式輸出遵循所有聲音規則並產生溫暖、提供資訊之敘事。
失敗時: 若儀式輸出感覺勉強或未在標準輸出之外增加資訊,跳過之。非每命令皆需儀式變體。
// Unknown item
if (!item) {
reporter.error(`Unknown: ${name}. Use 'tool list' to browse.`);
process.exit(1);
}
// Confirmation for destructive actions
if (!options.yes && !options.quiet && !options.dryRun) {
const answer = await askYesNo('Proceed?');
if (!answer) {
console.log(' Cancelled.');
return;
}
}
// State validation
if (!state.fires[name]) {
reporter.error(`Not active. Nothing to remove.`);
process.exit(1);
}
錯誤設計原則:
process.exit(1)--yes 繞過)預期: 所有錯誤路徑產生有助益之訊息。破壞性操作要求確認。
失敗時: 若確認提示干擾腳本化,確保 --yes 與 --quiet 皆繞過之。
import { describe, it, after } from 'node:test';
import assert from 'node:assert/strict';
import { execSync } from 'child_process';
const CLI = 'node cli/index.js';
function run(args) {
return execSync(`${CLI} ${args}`, { encoding: 'utf8', timeout: 10000 });
}
describe('new-command', () => {
after(() => { /* cleanup created files/state */ });
it('dry-run shows preview', () => {
const out = run('new-command arg --dry-run');
assert.match(out, /DRY RUN/);
});
it('--json outputs valid JSON', () => {
const out = run('new-command arg --json');
const start = out.indexOf('{');
const data = JSON.parse(out.slice(start));
assert.equal(data.command, 'new-command');
});
it('rejects unknown input', () => {
assert.throws(() => run('new-command nonexistent'), /Unknown/);
});
});
全面 CLI 測試模式詳見 test-cli-application 技能。
預期: 至少 3 測試:dry-run、JSON 輸出、錯誤案例。複雜命令需更多。
失敗時: 若 execSync 超時,提高超時或檢查互動式提示是否阻塞命令。
--help--dry-run、--quiet、--json)正確運作--yes 繞過)--json。--yes,並確保 --quiet 抑制提示。process.exit(1)。解析 CLI 輸出之工具先檢查退出碼。--scope 之選項應有合理預設,俾用戶無需每次指定。--quiet 旗標意指「給機器之最小輸出」。若儀式文字洩入安靜模式,腳本將因非預期輸出而崩潰。build-cli-plugin — 建構命令所操作之適配器/外掛test-cli-application — 步驟七基礎之外之全面 CLI 測試模式design-cli-output — 各冗長等級之終端輸出設計install-almanac-content — 結構良好之 CLI 命令技能範例