From d1dcdc631e2ae2a2a26d5fd81e090febfa8a6fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CSebastian?= <64795732+slegarraga@users.noreply.github.com> Date: Thu, 6 Aug 2026 05:23:24 -0400 Subject: [PATCH] fix: honor --version without a command --- src/cli.ts | 8 ++++---- test/cli.test.ts | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 test/cli.test.ts diff --git a/src/cli.ts b/src/cli.ts index 9be84c1..914c846 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -158,14 +158,14 @@ export async function main(argv: string[]): Promise { const args = parseArgs(argv); const { command, flags } = args; - if (flags["help"] || command === "help" || command === undefined) { - process.stdout.write(usage()); - return command === undefined && !flags["help"] ? 2 : 0; - } if (flags["version"]) { process.stdout.write(`${VERSION}\n`); return 0; } + if (flags["help"] || command === "help" || command === undefined) { + process.stdout.write(usage()); + return command === undefined && !flags["help"] ? 2 : 0; + } if (command === "rules") { printRules(); return 0; diff --git a/test/cli.test.ts b/test/cli.test.ts new file mode 100644 index 0000000..491023a --- /dev/null +++ b/test/cli.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { main } from "../src/cli.js"; + +describe("cli", () => { + it("prints the version and exits 0 for --version", async () => { + expect(await main(["--version"])).toBe(0); + }); + + it("prints usage and exits 2 with no command", async () => { + expect(await main([])).toBe(2); + }); + + it("prints usage and exits 0 for --help", async () => { + expect(await main(["--help"])).toBe(0); + }); +});