Skip to content

Routes and methods

A route is an address in your CLI, such as / or /pr/list. A method is what the user asks to do at that address: help, version, or execute. This guide shows how to declare routes and choose the methods each one supports.

Two functions build the route tree:

  • route(name(...), ...) declares an address that supports help only.
  • command(name(...), ...) declares an address that supports help and execute.

A child route is passed straight to its parent, next to the parent’s options and other elements. To reuse a group of routes, bundle them with extend(). Before 0.8.0, child routes went inside a routes() wrapper, which no longer exists.

command() is exactly a route() with executable() added, so these two definitions are equivalent:

import {
function command<const N extends string, const E extends readonly Input[]>(start: Definition<N>, ...elements: E & Validate<CommandZero<N>, E>): Materialize<Fold<CommandZero<N>, Normalize<E>>>
command
,
function executable(): MethodElement<"execute">
executable
,
function name<N extends string>(name: N): Definition<N>
name
,
function route<const N extends string, const E extends readonly Input[]>(start: Definition<N>, ...elements: E & Validate<RouteZero<N>, E>): Materialize<Fold<RouteZero<N>, Normalize<E>>>
route
} from "@bomb.sh/router";
const
const a: Route<"browse", "execute" | "help", [Done<{}, []>]>
a
=
command<"browse", readonly []>(start: Definition<"browse">): Route<"browse", "execute" | "help", [Done<{}, []>]>
command
(
name<"browse">(name: "browse"): Definition<"browse">
name
("browse"));
const
const b: Route<"browse", "execute" | "help", [Done<{}, []>]>
b
=
route<"browse", readonly [MethodElement<"execute">]>(start: Definition<"browse">, elements_0: MethodElement<"execute">): Route<"browse", "execute" | "help", [...]>
route
(
name<"browse">(name: "browse"): Definition<"browse">
name
("browse"),
function executable(): MethodElement<"execute">
executable
());

An executable route can still have children. The root of most CLIs is a command() that also holds subcommands:

import {
function command<const N extends string, const E extends readonly Input[]>(start: Definition<N>, ...elements: E & Validate<CommandZero<N>, E>): Materialize<Fold<CommandZero<N>, Normalize<E>>>
command
,
function executable(): MethodElement<"execute">
executable
,
function name<N extends string>(name: N): Definition<N>
name
,
function option<const N extends string, const E extends readonly Unary[]>(named: Definition<N>, ...elements: E & Check<Zero<N>, E>): ElementOf<...>
option
,
function route<const N extends string, const E extends readonly Input[]>(start: Definition<N>, ...elements: E & Validate<RouteZero<N>, E>): Materialize<Fold<RouteZero<N>, Normalize<E>>>
route
,
function schema<S extends Schema>(schema: S): TransformElement<SchemaTransform<OutputOf<S>>>
schema
,
function version(semver: string): MethodElement<"version">
version
,
} from "@bomb.sh/router";
import * as
import z
z
from "zod";
const
const app: Route<"gh", "execute" | "version" | "help", readonly [Done<{}, readonly [Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "execute" | "help", readonly [Done<{
limit: number;
}, []>]>]>]>, Route<...>, Route<...>, Route<...>]>]>
app
=
command<"gh", readonly [MethodElement<"version">, Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "execute" | "help", readonly [Done<{
limit: number;
}, []>]>]>]>, Route<...>, Route<...>, Route<...>]>(start: Definition<...>, elements_0: MethodElement<...>, elements_1: Route<...>, elements_2: Route<...>, elements_3: Route<...>, elements_4: Route<...>): Route<...>
command
(
name<"gh">(name: "gh"): Definition<"gh">
name
("gh"),
function version(semver: string): MethodElement<"version">
version
("2.62.0"),
route<"pr", readonly [Route<"list", "execute" | "help", readonly [Done<{
limit: number;
}, []>]>]>(start: Definition<"pr">, elements_0: Route<"list", "execute" | "help", readonly [Done<{
limit: number;
}, []>]>): Route<...>
route
(
name<"pr">(name: "pr"): Definition<"pr">
name
("pr"),
command<"list", readonly [ModelElement<ParamModel<"limit", number>>]>(start: Definition<"list">, elements_0: ModelElement<ParamModel<"limit", number>>): Route<...>
command
(
name<"list">(name: "list"): Definition<"list">
name
("list"),
option<"limit", readonly [TransformElement<SchemaTransform<number>>]>(named: Definition<"limit">, elements_0: TransformElement<SchemaTransform<number>>): ModelElement<...>
option
(
name<"limit">(name: "limit"): Definition<"limit">
name
("limit"),
schema<z.ZodDefault<z.ZodNumber>>(schema: z.ZodDefault<z.ZodNumber>): TransformElement<SchemaTransform<number>>
schema
(
import z
z
.
function number(params?: z.RawCreateParams & {
coerce?: boolean;
}): z.ZodNumber
export number
number
().
ZodType<number, ZodNumberDef, number>.default(def: number): z.ZodDefault<z.ZodNumber> (+1 overload)
default
(30)),
),
),
),
route<"repo", readonly [Route<"clone", "execute" | "help", [Done<{}, []>]>]>(start: Definition<"repo">, elements_0: Route<"clone", "execute" | "help", [Done<{}, []>]>): Route<...>
route
(
name<"repo">(name: "repo"): Definition<"repo">
name
("repo"),
command<"clone", readonly []>(start: Definition<"clone">): Route<"clone", "execute" | "help", [Done<{}, []>]>
command
(
name<"clone">(name: "clone"): Definition<"clone">
name
("clone"),
),
),
route<"browse", readonly [MethodElement<"execute">]>(start: Definition<"browse">, elements_0: MethodElement<"execute">): Route<"browse", "execute" | "help", [...]>
route
(
name<"browse">(name: "browse"): Definition<"browse">
name
("browse"),
function executable(): MethodElement<"execute">
executable
(),
),
command<"copilot", readonly [MethodElement<"version">]>(start: Definition<"copilot">, elements_0: MethodElement<"version">): Route<"copilot", "execute" | "version" | "help", [...]>
command
(
name<"copilot">(name: "copilot"): Definition<"copilot">
name
("copilot"),
function version(semver: string): MethodElement<"version">
version
("1.2.0"),
),
);

The root name identifies the executable. Route IDs start below it, so gh pr list selects /pr/list.

An option belongs to the route that declares it, and it must come after that route’s name. If --repo were declared on the pr route, gh pr --repo bombshell-dev/docs list would work and gh pr list --repo bombshell-dev/docs would fail. Declare options on the command that uses them, so users can put them at the end, as they expect.

Every route supports help. The other two methods exist only where you add them:

ElementAddsRequest it with
Built inhelp--help or -h
executable()executethe route words alone
version("x.y.z")version--version or -v

version() does not inherit. In the tree above, only / and /copilot declare a version, so gh copilot --version returns VERSION /copilot, while gh pr list --version fails with method-not-allowed.

--help and --version request a method. They do not stop parsing, and they always target the deepest route on the command line, wherever they appear. All three of these return HELP /pr/list:

gh --help pr list
gh pr --help list
gh pr list --help

When the user asks for a method a route does not support, parse() returns a failure with the code method-not-allowed. The failure names the method that was requested and the methods the route allows:

import
var process: NodeJS.Process
process
from "node:process";
import {
function command<const N extends string, const E extends readonly Input[]>(start: Definition<N>, ...elements: E & Validate<CommandZero<N>, E>): Materialize<Fold<CommandZero<N>, Normalize<E>>>
command
,
function name<N extends string>(name: N): Definition<N>
name
,
function parse<const R extends AnyRoute>(route: R, input: Input): Parse<R>
parse
,
function printErrors(result: MethodNotAllowed | UnprocessableContent): string
printErrors
,
function route<const N extends string, const E extends readonly Input[]>(start: Definition<N>, ...elements: E & Validate<RouteZero<N>, E>): Materialize<Fold<RouteZero<N>, Normalize<E>>>
route
,
} from "@bomb.sh/router";
const
const app: Route<"gh", "help" | "execute", readonly [Done<{}, readonly [Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "help" | "execute", [Done<{}, []>]>]>]>]>]>
app
=
command<"gh", readonly [Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "help" | "execute", [Done<{}, []>]>]>]>]>(start: Definition<"gh">, elements_0: Route<"pr", "help", readonly [...]>): Route<...>
command
(
name<"gh">(name: "gh"): Definition<"gh">
name
("gh"),
route<"pr", readonly [Route<"list", "help" | "execute", [Done<{}, []>]>]>(start: Definition<"pr">, elements_0: Route<"list", "help" | "execute", [Done<{}, []>]>): Route<...>
route
(
name<"pr">(name: "pr"): Definition<"pr">
name
("pr"),
command<"list", readonly []>(start: Definition<"list">): Route<"list", "help" | "execute", [Done<{}, []>]>
command
(
name<"list">(name: "list"): Definition<"list">
name
("list"))),
);
const
const result: Parse<Route<"gh", "help" | "execute", readonly [Done<{}, readonly [Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "help" | "execute", [Done<{}, []>]>]>]>]>]>>
result
=
parse<Route<"gh", "help" | "execute", readonly [Done<{}, readonly [Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "help" | "execute", [Done<{}, []>]>]>]>]>]>>(route: Route<...>, input: Input): Parse<...>
parse
(
const app: Route<"gh", "help" | "execute", readonly [Done<{}, readonly [Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "help" | "execute", [Done<{}, []>]>]>]>]>]>
app
, {
argv: string[]
argv
:
var process: NodeJS.Process
process
.
NodeJS.Process.argv: string[]

The process.argv property returns an array containing the command-line arguments passed when the Node.js process was launched. The first element will be

execPath

. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command-line arguments.

For example, assuming the following script for process-args.js:

import { argv } from 'node:process';
// print process.argv
argv.forEach((val, index) => {
console.log(`${index}: ${val}`);
});

Launching the Node.js process as:

Terminal window
node process-args.js one two=three four

Would generate the output:

0: /usr/local/bin/node
1: /Users/mjr/work/node/process-args.js
2: one
3: two=three
4: four

@since ― v0.1.27

argv
.
Array<string>.slice(start?: number, end?: number): string[]

Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

@param ― start The beginning index of the specified portion of the array. If start is undefined, then the slice begins at index 0.

@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the array.

slice
(2) });
if (!
const result: Parse<Route<"gh", "help" | "execute", readonly [Done<{}, readonly [Route<"pr", "help", readonly [Done<{}, readonly [Route<"list", "help" | "execute", [Done<{}, []>]>]>]>]>]>>
result
.
ok: boolean
ok
&&
const result: MethodNotAllowed | UnprocessableContent
result
.
Failure<C extends Status>.code: "method-not-allowed" | "unprocessable-content"
code
=== "method-not-allowed") {
const result: MethodNotAllowed
result
.
MethodNotAllowed.method: Method
method
; // the method that was requested
const result: MethodNotAllowed
result
.
MethodNotAllowed.allowed: readonly Method[]
allowed
; // the methods this route supports
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@see ― source

console
.
Console.error(message?: any, ...optionalParams: any[]): void

Prints to stderr with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated. See util.format() for more information.

@since ― v0.1.100

error
(
function printErrors(result: MethodNotAllowed | UnprocessableContent): string
printErrors
(
const result: MethodNotAllowed
result
));
}

Running gh pr prints:

pr does not support EXECUTE
Available methods:
HELP

A word that matches no route is not a “route not found” error. The router treats it as input that nothing claimed, so gh bogus fails with unprocessable-content at / and the message unexpected: `bogus` .

Nothing after -- takes part in routing, methods, or options. gh pr list -- --limit 5 returns EXECUTE /pr/list with the default { limit: 30 }.