Command resolution
Select commands ahead of execution to build help, log a selection, or implement custom routing. For a regular CLI, start with createTerminalApp() or createCommand().registry(...).
Define a command directly
defineCommand(...) is the direct form of command.define(...). This example puts definitions in a registry for the standalone lookup functions.
import {
createCommands,
defineCommand,
defineCommandRegistry,
type CommandAcceptedPath,
type CommandName
} from 'icore';
type PublicCommandName = CommandName<typeof runJobCommand>;
const listJobsCommand = defineCommand({
path: ['jobs', 'list'],
options: {
status: {
type: 'string',
choices: ['queued', 'running', 'done', 'failed']
}
} as const,
handle({ options }) {
return `status=${options.status ?? 'any'}\n`;
}
});
const runJobCommand = defineCommand({
path: ['jobs', 'run'],
options: {
'job-id': {
type: 'string',
required: true
}
} as const,
handle({ options }) {
return `run ${options['job-id']}\n`;
}
});
const registry = defineCommandRegistry([
listJobsCommand,
runJobCommand
] as const);
PublicCommandName is inferred from the command path.
type PublicCommandName = 'jobs run';
To use commands.prepare(...) and commands.run(...), create a commands object with createCommands(...).
Give a command aliases
Declare alternate paths on the main command. They share its options, prepare handler, description, and command function.
type AcceptedAccountPath =
CommandAcceptedPath<typeof listAccountsCommand>;
const listAccountsCommand = defineCommand({
path: ['account', 'list'],
aliases: [
['users', 'get-accounts'],
['accounts']
],
options: {
format: {
type: 'string',
choices: ['table', 'json'],
default: 'table'
}
} as const,
handle({ options }) {
return `format=${options.format}\n`;
}
});
const accountCommands = createCommands([
listAccountsCommand
] as const);
const canonical = accountCommands.resolve([
'account',
'list'
]);
const aliased = accountCommands.resolve([
'users',
'get-accounts'
]);
name and path always describe the main command. matchedPath holds the path the user typed.
canonical.name;
canonical.path;
canonical.matchedPath; // ['account', 'list']
aliased.name;
aliased.path;
aliased.matchedPath;
With the main name, matchedPath === path. TypeScript infers AcceptedAccountPath as a union of the main path and both aliases. accountCommands.names contains only 'account list', and accountCommands.definitions contains one definition. Resolution picks the longest matching path. An exact collision between definitions raises DUPLICATE_COMMAND.
For an unexpected positional argument the message shows the alias the user typed. The UNEXPECTED_POSITIONAL details keep the main name in command and the typed path in matchedPath.
Build a commands object from definitions
createCommands(...) creates the same commands object as createCommand().registry(...). Pass definitions prepared in another module.
const commands = createCommands([
listJobsCommand,
runJobCommand
] as const);
commands.names;
commands.registry;
Resolve from positional arguments
commands.resolve(...) is useful for help and other cases where the command and subcommands are already known as separate arguments. It looks up the supplied path. Option validation happens during preparation.
const resolved = commands.resolve([
'jobs',
'run'
]);
resolved.name;
resolved.path;
resolved.matchedPath;
resolved.positionals;
The result contains the command name.
jobs run
Resolve from raw arguments
commands.resolveFromArgs(...) and resolveCommandFromArgs(...) use command schemas to parse raw terminal arguments.
import { resolveCommandFromArgs } from 'icore';
const resolved = commands.resolveFromArgs([
'jobs',
'run',
'--job-id',
'job-42'
]);
const sameResolved = resolveCommandFromArgs(commands.registry, [
'jobs',
'run',
'--job-id',
'job-42'
]);
During one non-strict lookup, each main command's options are parsed at most once regardless of its aliases. A parse error in another command's options still allows the lookup to match valid input. Parse errors for the selected command and invalid option definitions are rejected. If no command matches, any captured parse error is reported.
Options can precede the command name by default.
await accountCommands.prepare([
'--format=json',
'users',
'get-accounts'
]);
If help declares --verbose as a boolean flag and accepts positional arguments, --verbose help status passes verbose: true and positionals: ['status'] to its handler. If system status is an alias for status, --verbose help system status passes positionals: ['system', 'status'] to help. The result is the same in either registration order.
With strict: true the main name or alias must come first. Parsing global options preserves the original argument order.
await accountCommands.prepare([
'users',
'get-accounts',
'--format=json'
], {
strict: true
});
Use a standalone function
resolveCommand(...) looks up a name like commands.resolve(...). Pass a registry when you only need the lookup.
import { resolveCommand } from 'icore';
const resolved = resolveCommand(registry, [
'jobs',
'list'
]);
Check a command name
isCommandName(...) checks whether an unknown value is a main name in the registry. Use it to decide between command help and general help. Aliases are available on command definitions and through matchedPath. For validation and execution, see two-step execution.
import { isCommandName } from 'icore';
function renderHelpPage(name: unknown): string {
if (!isCommandName(commands.registry, name)) {
return renderTopLevelHelp();
}
return renderCommandHelp(name);
}