Overview of Cliffy CLI library
command module for building CLI structures (commands, options, and arguments) and the prompt module for creating interactive user experiences (input, select, toggle, etc.).website·Indexed 32 days ago
https://cliffy.io/A powerful and versatile library for creating interactive command-line tools, featuring a type-safe command module with auto-generated help and shell completions, a prompt module for interactive inputs, a flags parser with built-in validations, and a parser-agnostic self-upgrade API supporting providers like JSR, npm, Deno Land, GitHub, and GitLab. Documentation covers version v1.3.1.
command module for building CLI structures (commands, options, and arguments) and the prompt module for creating interactive user experiences (input, select, toggle, etc.).$ deno run examples/command/demo.ts --barr
error: Unknown option "--barr". Did you mean option "--bar"?KeyCode interface is used to represent a parsed ANSI sequence resulting from a keypress. It provides detailed information about the key pressed, including its name, the raw ANSI sequence, and the state of modifier keys.{
"name": "up",
"sequence": "\x1b[A",
"code": "[A",
"ctrl": false,
"meta": false,
"shift": false
}A provider is a registry adapter used by the upgrade() function and the UpgradeCommand to identify available versions of a CLI and their download locations.
upgrade() accepts a single provider.UpgradeCommand accepts an array of providers: the first is the default, and others can be selected by the user via the --registry flag.Available providers include:
JsrProvider (JSR), NpmProvider (npm).DenoLandProvider (deno.land/x), NestLandProvider (nest.land).GithubProvider (GitHub), GitlabProvider (GitLab).UrlProvider (any URL).Note: GithubProvider, GitlabProvider, and UrlProvider support upgrading standalone executables by downloading release assets.
@cliffy/upgrade is a parser-agnostic API that allows CLI tools to download a newer version of themselves and replace the current installation. It supports both script-based installs (via runtime package managers) and standalone binaries (e.g., deno compile, bun build --compile, or Node single executable applications). It is independent of @cliffy/command and @cliffy/flags and can be used with any argument parser.KeyPressEvent represents a keypress event in Cliffy. It inherits most properties from the KeyCode interface and provides detailed information about the key pressed, including its name, ANSI sequence, and modifier states.{
"name": "up",
"sequence": "\x1b[A",
"code": "[A",
"ctrlKey": false,
"metaKey": false,
"shiftKey": false,
"altKey": false
}ValidationError is thrown (due to invalid options, arguments, or environment variables), Cliffy prints the auto-generated help text, the error message, and calls Deno.exit(validationError.exitCode ?? 1). Other runtime errors are thrown by default. You can modify this behavior using .throwErrors(), .noExit(), or by registering a custom error handler via .error().-h or --help flags. The -h flag prints a condensed version (first line of descriptions), while --help prints the full description.import { Command } from "@cliffy/command";
await new Command()
.name("help-option-and-command")
.version("0.1.0")
.description("Sample description ...")
.env(
"EXAMPLE_ENVIRONMENT_VARIABLE=<value:boolean>",
"Environment variable description ...",
)
.example(
"Some example",
"Example content ...\n\nSome more example content ...",
)
.parse();HelpCommand prints auto-generated help documentation. It functions similarly to the --help option but allows users to pass a specific child command name as an argument to view help for that sub-command. Using the .global() method makes the help command available across all child commands.import { Command } from "@cliffy/command";
import { HelpCommand } from "@cliffy/command/help";
await new Command()
.version("0.1.0")
.description("Sample description ...")
.env(
"EXAMPLE_ENVIRONMENT_VARIABLE=<value:boolean>",
"Environment variable description ...",
)
.command("help", new HelpCommand().global())
.parse();By default, Cliffy calls Deno.exit(0) when a user presses Ctrl+C. To implement a custom signal handler (e.g., using Deno.addSignalListener), you must enable the cbreak option in your prompt configuration. This allows OS signals to pass through to Deno.
Constraints and Caveats:
cbreak option is currently only supported on Linux and macOS; it is not supported in Bun.cbreak with interactive prompts like Select or Toggle, you must manually show the cursor and clear the stdout before calling Deno.exit(), otherwise the terminal state may remain corrupted.import { tty } from "@cliffy/ansi/tty";
import { Toggle } from "@cliffy/prompt/toggle";
// Register a custom handler for SIGINT (Ctrl+C)
Deno.addSignalListener("SIGINT", () => {
// Manually restore terminal state before exiting
tty.cursorLeft.eraseDown.cursorShow();
console.log("interrupted!");
Deno.exit(1);
});
const confirmed = await Toggle.prompt({
message: "Please confirm",
cbreak: true, // Enable pass-through of OS signals
});
console.log({ confirmed });upgrade() function is silent by default. To report progress, pass a logger object containing log, info, warn, and error methods (e.g., the console object). Use verbose: true to include the output of the runtime's install command.await upgrade({
name: "my-cli",
from: "1.0.0",
to: "latest",
provider: new GithubProvider({ repository: "user/my-cli" }),
logger: console,
verbose: true,
});