diff --git a/bin/tokencut.mjs b/bin/tokencut.mjs index e408a75..fa2e56d 100644 --- a/bin/tokencut.mjs +++ b/bin/tokencut.mjs @@ -7,6 +7,16 @@ const flag = (name, def) => { const i = args.indexOf(name); return i >= 0 ? (arg const has = (name) => args.includes(name); const file = args.find((a) => !a.startsWith("--") && args[args.indexOf(a) - 1] !== "--max" && args[args.indexOf(a) - 1] !== "--max-tool" && args[args.indexOf(a) - 1] !== "--price" && args[args.indexOf(a) - 1] !== "--out"); +if (has("--version") || has("-V")) { + try { + const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); + console.log(pkg.version ?? "0.0.0"); + } catch { + console.log("0.0.0"); + } + process.exit(0); +} + if (!file || has("--help")) { console.log(`tokencut -- measure and cut the token cost of an LLM/agent payload @@ -18,6 +28,7 @@ if (!file || has("--help")) { --out write the compacted payload --price $ per 1M input tokens for the cost estimate (default 3) --json machine-readable output + --version, -V print package version Payload: an array of messages, or { system, messages } (Anthropic or OpenAI style).`); process.exit(file ? 0 : 1); diff --git a/test/cli.test.mjs b/test/cli.test.mjs index f0eba43..a686546 100644 --- a/test/cli.test.mjs +++ b/test/cli.test.mjs @@ -1,5 +1,5 @@ import assert from "node:assert/strict"; -import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { spawnSync } from "node:child_process"; @@ -56,3 +56,14 @@ test("accepts zero as a compact token budget", () => { assert.equal(result.status, 0, result.stderr); assert.doesNotThrow(() => JSON.parse(result.stdout)); }); + + +test("--version prints package version", () => { + const r = spawnSync(process.execPath, ["bin/tokencut.mjs", "--version"], { + cwd: new URL("..", import.meta.url), + encoding: "utf8", + }); + assert.equal(r.status, 0); + const ver = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version; + assert.equal(r.stdout.trim(), ver); +});