@optique/core
Version:
Type-safe combinatorial command-line interface parser
178 lines (176 loc) • 6.05 kB
TypeScript
import { Message } from "./message.js";
import { ShowDefaultOptions } from "./doc.js";
import { InferValue, Parser } from "./parser.js";
//#region src/facade.d.ts
/**
* Configuration options for the {@link run} function.
*
* @template THelp The return type when help is shown.
* @template TError The return type when an error occurs.
*/
interface RunOptions<THelp, TError> {
/**
* Enable colored output in help and error messages.
*
* @default `false`
*/
readonly colors?: boolean;
/**
* Maximum width for output formatting. Text will be wrapped to fit within
* this width. If not specified, text will not be wrapped.
*/
readonly maxWidth?: number;
/**
* Whether and how to display default values for options and arguments.
*
* - `boolean`: When `true`, displays defaults using format `[value]`
* - `ShowDefaultOptions`: Custom formatting with configurable prefix and suffix
*
* Default values are automatically dimmed when `colors` is enabled.
*
* @default `false`
* @since 0.4.0
*/
readonly showDefault?: boolean | ShowDefaultOptions;
/**
* Help configuration. When provided, enables help functionality.
*/
readonly help?: {
/**
* Determines how help is made available:
*
* - `"command"`: Only the `help` subcommand is available
* - `"option"`: Only the `--help` option is available
* - `"both"`: Both `help` subcommand and `--help` option are available
*
* @default `"option"`
*/
readonly mode?: "command" | "option" | "both";
/**
* Callback function invoked when help is requested. The function can
* optionally receive an exit code parameter.
*
* You usually want to pass `process.exit` on Node.js or Bun and `Deno.exit`
* on Deno to this option.
*
* @default Returns `void` when help is shown.
*/
readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
};
/**
* Version configuration. When provided, enables version functionality.
*/
readonly version?: {
/**
* Determines how version is made available:
*
* - `"command"`: Only the `version` subcommand is available
* - `"option"`: Only the `--version` option is available
* - `"both"`: Both `version` subcommand and `--version` option are available
*
* @default `"option"`
*/
readonly mode?: "command" | "option" | "both";
/**
* The version string to display when version is requested.
*/
readonly value: string;
/**
* Callback function invoked when version is requested. The function can
* optionally receive an exit code parameter.
*
* You usually want to pass `process.exit` on Node.js or Bun and `Deno.exit`
* on Deno to this option.
*
* @default Returns `void` when version is shown.
*/
readonly onShow?: (() => THelp) | ((exitCode: number) => THelp);
};
/**
* What to display above error messages:
* - `"usage"`: Show usage information
* - `"help"`: Show help text (if available)
* - `"none"`: Show nothing above errors
*
* @default `"usage"`
*/
readonly aboveError?: "usage" | "help" | "none";
/**
* Callback function invoked when parsing fails. The function can
* optionally receive an exit code parameter.
*
* You usually want to pass `process.exit` on Node.js or Bun and `Deno.exit`
* on Deno to this option.
* @default Throws a {@link RunError}.
*/
readonly onError?: (() => TError) | ((exitCode: number) => TError);
/**
* Function used to output error messages.
*
* @default `console.error`
*/
readonly stderr?: (text: string) => void;
/**
* Function used to output help and usage messages.
*
* @default `console.log`
*/
readonly stdout?: (text: string) => void;
/**
* Brief description shown at the top of help text.
*
* @since 0.4.0
*/
readonly brief?: Message;
/**
* Detailed description shown after the usage line.
*
* @since 0.4.0
*/
readonly description?: Message;
/**
* Footer text shown at the bottom of help text.
*
* @since 0.4.0
*/
readonly footer?: Message;
}
/**
* Runs a parser against command-line arguments with built-in help and error
* handling.
*
* This function provides a complete CLI interface by automatically handling
* help commands/options and displaying formatted error messages with usage
* information when parsing fails. It augments the provided parser with help
* functionality based on the configuration options.
*
* The function will:
*
* 1. Add help command/option support (unless disabled)
* 2. Parse the provided arguments
* 3. Display help if requested
* 4. Show formatted error messages with usage/help info on parse failures
* 5. Return the parsed result or invoke the appropriate callback
*
* @template TParser The parser type being run.
* @template THelp Return type when help is shown (defaults to `void`).
* @template TError Return type when an error occurs (defaults to `never`).
* @param parser The parser to run against the command-line arguments.
* @param programName Name of the program used in usage and help output.
* @param args Command-line arguments to parse (typically from
* `process.argv.slice(2)` on Node.js or `Deno.args` on Deno).
* @param options Configuration options for output formatting and callbacks.
* @returns The parsed result value, or the return value of `onHelp`/`onError`
* callbacks.
* @throws {RunError} When parsing fails and no `onError` callback is provided.
*/
declare function run<TParser extends Parser<unknown, unknown>, THelp = void, TError = never>(parser: TParser, programName: string, args: readonly string[], options?: RunOptions<THelp, TError>): InferValue<TParser>;
/**
* An error class used to indicate that the command line arguments
* could not be parsed successfully.
*/
declare class RunError extends Error {
constructor(message: string);
}
//#endregion
export { RunError, RunOptions, run };