算法艺术创作
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——令選項定其何)<必>、[選]、或 [多...]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() 共助集中:
得:行止之手依五步——境 → 解 → 預 → 行 → 出。
敗則:命不合解—行之模(如純信息類 detect),簡為:境 → 算 → 出。
每命宜支三出模:
默(人可讀):
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 可解。靜簡。默有信息。
敗則:命無有義 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 技。
得:至少三試:干跑、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 命技之例