Add CLI dispatcher and bin entry

Top-level run(argv,{out,err}) accepts an injectable output sink so
handlers can be unit-tested without spawning a subprocess. Handler
map is empty for now; subcommands are wired in as they land.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Axel Meyer
2026-05-23 13:44:10 +02:00
parent 2949a96e11
commit b1743ad19f
3 changed files with 94 additions and 0 deletions

46
tests/cli.test.js Normal file
View File

@@ -0,0 +1,46 @@
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const { run } = require('../src/cli');
class Sink {
constructor() { this.chunks = []; }
write(s) { this.chunks.push(s); }
get text() { return this.chunks.join(''); }
}
test('cli: no args prints usage to stderr, exit 1', () => {
const out = new Sink(); const err = new Sink();
const code = run([], { out, err });
assert.equal(code, 1);
assert.match(err.text, /usage/i);
assert.match(err.text, /encode/);
assert.match(err.text, /decode/);
assert.match(err.text, /build/);
assert.match(err.text, /inspect/);
assert.equal(out.text, '');
});
test('cli: --help prints usage to stdout, exit 0', () => {
const out = new Sink(); const err = new Sink();
const code = run(['--help'], { out, err });
assert.equal(code, 0);
assert.match(out.text, /usage/i);
assert.equal(err.text, '');
});
test('cli: -h alias for --help', () => {
const out = new Sink(); const err = new Sink();
const code = run(['-h'], { out, err });
assert.equal(code, 0);
assert.match(out.text, /usage/i);
});
test('cli: unknown subcommand goes to stderr, exit 1', () => {
const out = new Sink(); const err = new Sink();
const code = run(['frobnicate'], { out, err });
assert.equal(code, 1);
assert.match(err.text, /unknown subcommand/i);
assert.match(err.text, /frobnicate/);
});