import { z } from 'zod'; import { HelpConfig } from './help'; import { MassargOption, MassargFlag, TypedOptionConfig, Prefixes, FlagConfig } from './option'; import { DeepRequired } from './utils'; import { MassargExample, ExampleConfig } from './example'; export declare const CommandConfig: (_args: z.ZodType) => z.ZodObject<{ name: z.ZodString; description: z.ZodString; aliases: z.ZodOptional>; run: z.ZodType>; optionPrefix: z.ZodOptional>; aliasPrefix: z.ZodOptional>; }, z.core.$strip>; export type CommandConfig = z.infer>>; /** * An object with string keys and any values. */ export type ArgsObject = Record; export type Runner = (options: Args, instance: MassargCommand) => Promise | void; /** * Error handler callback type. * Called when an error occurs during parsing or command execution. */ export type ErrorHandler = (error: Error) => void; /** * A command is a named function that can be invoked with a set of options. * * Commands can have sub-commands, which can have their own sub-commands, and so on. * * Options are not inherited by sub-commands, but their parsed values are passed down when * invoking a sub-command. This works recursively. * * @example * ```ts * massarg(options).command({ * name: 'foo', * description: 'foo command', * run: (options, instance) => { * console.log(options, instance) * }, * }) * ``` */ export declare class MassargCommand implements Omit, 'run'> { name: string; description: string; aliases: string[]; private _run?; commands: MassargCommand[]; options: MassargOption[]; examples: MassargExample[]; args: Partial; private _helpConfig; private _errorHandler?; parent?: MassargCommand; optionPrefix: string; aliasPrefix: string; constructor(options: CommandConfig, parent?: MassargCommand); get optionPrefixes(): Prefixes; get helpConfig(): DeepRequired; /** * Add a sub-command to this command. * * The sub-command will inherit all help configuration from the parent commands, * all the way up to the top-level command. * * While options are not inherited, they will be passed from any parent commands * to the sub-command when invoked. */ command(config: CommandConfig): MassargCommand; command(config: MassargCommand): MassargCommand; /** * Adds a flag to this command. * * A flag is an option that is either present or not. It can be used to toggle * a boolean value, or to indicate that a command should be run in a different * mode. * * A flag can be negated by using `negatable: true`. By default, the negated name is the same * as the option name, prefixed by `no-`, and each of the aliases will be uppercased. * For example, `--verbose` and `--no-verbose`, or `-v` and `-V`. * This behavior can be overridden by the `negatedName` and `negatedAliases` options. */ flag(config: FlagConfig): MassargCommand; flag(config: MassargFlag): MassargCommand; /** * Adds an option to this command. * * An option is a named value that can be passed to a command. It can be * required or optional, and can be of any type. * * You can specify a default value for an option, which will be used if the * option is not passed to the command. * * You can also specify a parse function, which will be used to parse the * value passed to the command. This is useful if you want to parse a string * into a more complex type, or if you want to validate the value. */ option(config: MassargOption): MassargCommand; option(config: TypedOptionConfig): MassargCommand; private assertNotDuplicate; private assertOnlyOneDefault; /** * Adds an example to this command. * * An example is a description of how to use the command, with an example input and output. * * At least one of `description`, `input` or `output` must be provided, but neither alone is * required. */ example(config: ExampleConfig): MassargCommand; /** * Configure the help output for this (and all child) commands. * * You can automatically bind the help command to this command, and/or bind the help option * to this command. * * If you don't opt-in to this behavior with `bindCommand` or `bindOption`, you can still * access the help output via `this.helpString()` and `this.printHelp()`. */ help(config: HelpConfig): MassargCommand; /** * Configure the main function for this command. This command will run when no sub-commands * are provided. * * If none is provided, help will be printed. */ main(run: Runner): MassargCommand; /** * Configure a custom error handler for this command. * * By default, errors are caught and logged to stderr with a red color. * Use this method to override the default error handling behavior. * * Note: The process will always exit with code 1 after an error, regardless of the handler. * * @example * ```ts * massarg(options) * .onError((error) => { * console.error('Custom error:', error.message) * // Log to external service, show custom UI, etc. * }) * .parse() * ``` */ onError(handler: ErrorHandler): MassargCommand; /** Get the error handler, checking parent chain if not set locally */ private get errorHandler(); /** Default error handler that logs red error message to stderr */ private defaultErrorHandler; /** * Parse the given arguments and run the command or sub-commands along with the given options * and flags. * * To parse the arguments without running any commands and only get the output args, * use `getArgs` instead. */ parse(argv?: string[], args?: Partial, parent?: MassargCommand): Promise | void; /** Handle an error using the configured error handler, then exit */ private handleError; private printError; private parseOption; /** Parse the given arguments and return the output args. */ getArgs(argv: string[], __args?: Partial, parent?: MassargCommand, parseCommands?: false): Promise | void; getArgs(argv: string[], __args?: Partial, parent?: MassargCommand, parseCommands?: true): Args; private assertRequired; /** * Generate the help output for this command, and return it as a string. */ helpString(): string; /** * Print the help output for this command. */ printHelp(): void; } /** * A command that prints help for this command, or a sub-command if specified. * * This command is automatically added to the top-level command if you use `bindCommand: true` in `help()`. * You can also add it manually to any command. */ export declare class MassargHelpCommand extends MassargCommand { constructor(config?: Partial, 'run'>>); } //# sourceMappingURL=command.d.ts.map