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); + }); +});