Building a command-line app in Deno with cac

cac (“Command And Conquer”) is a small JavaScript library for building command-line apps. You declare commands, arguments and options, and it parses argv, dispatches to the matching command and generates --help output. It’s published to npm, and Deno can import it directly with an npm: specifier, so there’s nothing to install up front.
The examples below were tested with cac 7.1.0 on Deno 2.
Install Deno by following the official installation instructions, then check it works:
deno --versionImport cac with an npm: specifier. Deno downloads and caches the package on first run:
import { cac } from "npm:cac@^7.1.0";If you’d rather keep versions in deno.json, run deno add npm:cac and import it as "cac" instead.
A command with an argument and an option
Section titled “A command with an argument and an option”Create cli.ts:
import { cac } from "npm:cac@^7.1.0";
const cli = cac("my-cli");
cli .command("greet [name]", "Greet a user by name") .option("-u, --uppercase", "Greet in uppercase") .action((name: string = "stranger", options: { uppercase?: boolean }) => { const greeting = `Hello, ${name}!`; console.log(options.uppercase ? greeting.toUpperCase() : greeting); });
cli.help();cli.version("0.1.0");
cli.parse(["deno", "cli.ts", ...Deno.args]);A few things that are easy to get wrong:
- Argument order in
action. cac passes positional arguments first and the options object last.[name]in square brackets is optional;<name>would make it required. cli.help()doesn’t print anything by itself. It registers-h, --helpand prints help when that flag is passed.cli.version()does the same for-v, --version.parseexpects a Node-styleargv. It skips the first two entries (runtime and script path), soDeno.argson its own would lose the command name. Prepending two placeholders fixes that. In Deno 2, callingcli.parse()with no arguments also works, because Deno providesprocess.argv.
Run it:
deno run cli.ts greet# Hello, stranger!deno run cli.ts greet John# Hello, John!deno run cli.ts greet Jane --uppercase# HELLO, JANE!deno run cli.ts --help lists the commands and global options, and deno run cli.ts greet --help shows the options for greet. Running with no arguments prints nothing, because no command matched. To run something in that case, register a default command with cli.command("[...args]", "...").
Handling unknown commands
Section titled “Handling unknown commands”When the first argument doesn’t match any command and there’s no default command, cac fires a command:* event. Use it to print an error and the help text:
import { cac } from "npm:cac@^7.1.0";
const cli = cac("my-cli");
cli.addEventListener("command:*", () => { console.error(`Invalid command: ${cli.args.join(" ")}`); cli.outputHelp(); Deno.exit(1);});
cli .command("greet [name]", "Greet a user by name") .option("-u, --uppercase", "Greet in uppercase") .action((name: string = "stranger", options: { uppercase?: boolean }) => { const greeting = `Hello, ${name}!`; console.log(options.uppercase ? greeting.toUpperCase() : greeting); });
cli.help();cli.version("0.1.0");
cli.parse(["deno", "cli.ts", ...Deno.args]);deno run cli.ts invalid-command# Invalid command: invalid-command# (help text follows; exit code 1)In cac 7 the CLI object is an EventTarget, so listeners are added with addEventListener. Examples written for cac 6 use cli.on("command:*", ...), which won’t work with 7.x.
Next steps
Section titled “Next steps”- Add more commands with
cli.command(...); each gets its own--helppage. - Ship the CLI as a single binary with
deno compile cli.ts. - The cac README covers variadic arguments (
[...files]), negated options (--no-color), dot-nested options and option defaults.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)