Skip to content

Program ​

See the Program guide for usage.

jinju ​

ts
function jinju<const T extends CommandTree>(config: ProgramConfig<T>): Program;

Creates a program from a tree of commands. Throws a ConfigError if the tree or its commands are invalid.

ProgramConfig ​

ts
interface ProgramConfig<T extends CommandTree = CommandTree> {
	name?: string;
	version?: string;
	description?: string;
	commands: T & CheckTree<T>;
	strict?: boolean;
	env?: Readonly<Record<string, string | undefined>>;
	help?: HelpTemplate;
	error?: ErrorTemplate;
	stdout?: (text: string) => void;
	stderr?: (text: string) => void;
	exit?: (code: number) => void;
}
OptionDefaultDescription
nameThe script's file nameShown in usage.
versionEnables --version.
descriptionShown on the root help page.
commandsThe command tree.
stricttrueReject unknown flags.
envprocess.envWhere flags' env variables are read from.
helpnew HelpTemplate()Renders help pages.
errornew ErrorTemplate()Renders error output.
stdoutWrites to process.stdoutReceives help and version output.
stderrWrites to process.stderrReceives error output.
exitSets process.exitCodeReceives the exit code.

Program ​

ts
class Program {
	readonly name: string;
	constructor(config: ProgramConfig);
	run(argv?: readonly string[]): Promise<number>;
	help(path?: readonly string[]): string;
}

What jinju returns. Define it once, then call its methods:

ts
const app = jinju({ name: 'app', commands });

await app.run();

Use jinju rather than new Program, because jinju checks the command tree's types.

Properties ​

PropertyDescription
nameThe program's name: name from the config, or the script's file name.

run(argv?) ​

ts
run(argv?: readonly string[]): Promise<number>;

Parses argv, routes it to a command and runs it. argv defaults to process.argv.slice(2); pass it yourself to run with other input, e.g. app.run(['build', '--watch']).

Resolves to the exit code, which it also passes to exit (by default, setting process.exitCode). Help, --version and errors are written to stdout or stderr. It rejects only when a run handler throws an error that isn't a JinjuError.

help(path?) ​

ts
help(path?: readonly string[]): string;

Returns the help text for a command path, e.g. app.help(['sync', 'repeat']), without printing it. With no path, it returns the root page. Throws UnknownCommandError for an unknown name.

CommandTree ​

ts
interface CommandTree {
	readonly [key: string]: AnyCommand | CommandTree;
}

Commands keyed by name. Nest objects for subcommands; $ is the command for that level. A :name key is a dynamic segment, which captures one word into the commands' arg named name.

packageVersion ​

ts
function packageVersion(from: string | URL): string;

Reads version from the nearest package.json at or above from, usually the CLI's own import.meta.url. Pass the result as jinju({ version }). Throws a ConfigError when there's no package.json, or it has no version.