/** * @file Command * @module kronk/lib/Command */ import optionValueSource from '#enums/option-value-source'; import CommandEvent from '#events/command.event'; import Argument from '#lib/argument'; import Help from '#lib/help'; import Helpable from '#lib/helpable.abstract'; import Option from '#lib/option'; import type { Action, ArgumentData, ArgumentInfo, ArgumentSyntax, ArgumentValueSource, ArgumentValueSources, Awaitable, CommandData, CommandErrorInfo, CommandEventName, CommandInfo, CommandMetadata, CommandName, CommandSnapshot, ExampleInfo, ExamplesData, Exit, Flags, HelpCommandData, HelpOptionData, HelpTextOptions, HooksData, HooksInfo, KronkEventListener, KronkHookMap, KronkHookName, List, Numeric, OptionData, OptionInfo, OptionPriority, OptionValues, OptionValueSource, OptionValueSources, ParseOptions, ParseUnknownResult, Process, RawOptionValue, SubcommandInfo, SubcommandsInfo, UnknownStrategy, UsageData, UsageInfo, Version, VersionOptionData } from '@flex-development/kronk'; import { CommandError, KronkError } from '@flex-development/kronk/errors'; import { KronkEvent, OptionEvent } from '@flex-development/kronk/events'; import EventEmitter, { type OnOptions } from 'eventemitter2'; /** * A command. * * Commands use the {@linkcode initialCommand} construct to tokenize arguments, * delimiters, operands, option flags, and subcommand names. * * @see {@linkcode Helpable} * * @class * @extends {Helpable} */ declare class Command extends Helpable { /** * The parsed command arguments. * * @public * @instance * @member {any[]} args */ args: any[]; /** * List, where each index is a the position of parsed command-argument * and each value is the source of the argument. * * @see {@linkcode ArgumentValueSources} * * @protected * @instance * @member {ArgumentValueSources} argumentValueSources */ protected argumentValueSources: ArgumentValueSources; /** * The raw command-line arguments. * * @public * @instance * @member {string[]} argv */ argv: string[]; /** * The default command. * * @public * @instance * @member {Command | null | undefined} defaultCommand */ defaultCommand: Command | null | undefined; /** * Command event emitter. * * @see {@linkcode EventEmitter} * * @protected * @readonly * @instance * @member {EventEmitter} events */ protected readonly events: EventEmitter; /** * Whether command help was requested. * * @protected * @instance * @member {boolean} helpRequested */ protected helpRequested: boolean; /** * Command metadata. * * @see {@linkcode CommandMetadata} * * @protected * @instance * @override * @member {CommandMetadata} info */ protected info: CommandMetadata; /** * The event whose listener takes precedence over any parsing checks * and the command {@linkcode action}. * * @see {@linkcode KronkEvent} * * @protected * @instance * @member {KronkEvent | null | undefined} interrupter */ protected interrupter: KronkEvent | null | undefined; /** * Record, where each key is an option key and each value is the source of an * option value. * * @see {@linkcode OptionValueSources} * * @protected * @instance * @member {OptionValueSources} optionValueSources */ protected optionValueSources: OptionValueSources; /** * Record, where each key is an option key * and each value is a parsed option value. * * @see {@linkcode OptionValues} * * @protected * @instance * @member {OptionValues} optionValues */ protected optionValues: OptionValues; /** * Information about the current process. * * @see {@linkcode Process} * * @public * @instance * @member {Process} process */ process: Process; /** * Whether the command version was requested. * * @protected * @instance * @member {boolean} versionRequested */ protected versionRequested: boolean; /** * Create a new parent command or subcommand. * * @see {@linkcode CommandInfo} * @see {@linkcode SubcommandInfo} * * @param {CommandInfo | SubcommandInfo} info * The command info or name */ constructor(info: CommandInfo | SubcommandInfo | string); /** * Create a new command. * * @see {@linkcode CommandInfo} * * @param {CommandInfo | string | null | undefined} [info] * The command info or name */ constructor(info?: CommandInfo | string | null | undefined); /** * Create a new command. * * @see {@linkcode CommandData} * * @param {string | null | undefined} [name] * The command name * @param {CommandData | null | undefined} [info] * Additional command info */ constructor(name: string | null | undefined, info?: CommandData | null | undefined); /** * Whether the command is the default subcommand of its {@linkcode parent}. * * @public * @instance * * @return {boolean} * `true` if command is default subcommand, `false` otherwise */ get default(): boolean; /** * The event name for the command. * * @see {@linkcode CommandEventName} * * @public * @instance * * @return {CommandEventName} * Command event name */ get event(): CommandEventName; /** * Get a function that returns `this` command. * * @protected * @instance * * @return {() => this} * This context function */ protected get self(): () => this; /** * Whether the command has subcommands, but no {@linkcode action}. * * @public * @instance * * @return {boolean} * `true` if command is structural, `false` otherwise */ get structural(): boolean; /** * The strategy for handling unknown command-line arguments. * * @see {@linkcode UnknownStrategy} * * @public * @instance * * @return {UnknownStrategy} * Unknown command-line argument strategy */ get unknown(): UnknownStrategy; /** * Set the action callback. * * @see {@linkcode Action} * * @public * @instance * * @param {Action | null | undefined} action * The callback to fire when the command is ran * @return {this} * `this` command */ action(action: Action | null | undefined): this; /** * Get the action callback. * * For structural commands, and commands where help was requested (via option * or subcommand), the action callback prints the help text. * * @see {@linkcode Action} * @see {@linkcode OptionValues} * * @public * @instance * * @template {OptionValues} [Opts=OptionValues] * The parsed command options * @template {any[]} [Args=any[]] * The parsed command arguments * * @return {Action} * The callback to fire when the command is ran */ action(): Action; /** * Add a prepared `argument`. * * @see {@linkcode Argument} * @see {@linkcode KronkError} * * @public * @instance * * @param {Argument} argument * The argument instance to add * @return {never | this} * `this` command * @throws {KronkError} * If the last registered argument is variadic */ addArgument(argument: Argument): never | this; /** * Add a prepared `subcommand`. * * > 👉 **Note**: See {@linkcode command} for creating an attached subcommand * > that inherits settings from its {@linkcode parent}. * * @see {@linkcode Command} * @see {@linkcode KronkError} * * @public * @instance * * @param {Command} subcommand * The command instance to add * @return {never | this} * `this` command * @throws {KronkError} * If `subcommand` does not have a valid name * or a subcommand with the same name or alias as `subcommand` already exists */ addCommand(subcommand: Command): never | this; /** * Add a prepared `option`. * * @see {@linkcode Option} * * @public * @instance * * @param {Option} option * The option instance to add * @return {never | this} * `this` command * @throws {KronkError} * If an option with the same long or short flag as `option` already exists */ addOption(option: Option): never | this; /** * Add a command alias. * * > 👉 **Note**: This method can be called more than once * > to add multiple aliases. * * @public * @instance * * @param {string} alias * An alias for the command * @return {this} * `this` command */ alias(alias: string): this; /** * Get an alias for the command. * * @see {@linkcode CommandName} * * @public * @instance * * @return {CommandName} * Command alias */ alias(): CommandName; /** * Add aliases for the command. * * @see {@linkcode List} * * @public * @instance * * @param {List | string | null | undefined} aliases * An alias, or list of aliases, for the command * @return {this} * `this` command */ aliases(aliases: List | string | null | undefined): this; /** * Get a list of command aliases. * * @public * @instance * * @return {Set} * List of command aliases */ aliases(): Set; /** * Define an argument for the command. * * @see {@linkcode ArgumentInfo} * * @public * @instance * * @param {ArgumentInfo | string} info * Argument info or syntax * @return {this} * `this` command */ argument(info: ArgumentInfo | string): this; /** * Define an argument for the command. * * @see {@linkcode ArgumentData} * * @public * @instance * * @param {string} syntax * Argument syntax * @param {ArgumentData | null | undefined} [info] * Additional argument info * @return {this} * `this` command */ argument(syntax: string, info?: ArgumentData | null | undefined): this; /** * Set an argument value. * * @see {@linkcode ArgumentValueSource} * @see {@linkcode Numeric} * * @public * @instance * * @param {Numeric | number} index * The position of the argument * @param {unknown} value * The parsed argument value * @param {ArgumentValueSource | null | undefined} [source] * The source of the argument value * @return {this} * `this` command */ argumentValue(index: Numeric | number, value: unknown, source?: ArgumentValueSource | null | undefined): this; /** * Get an argument value. * * @see {@linkcode Numeric} * * @public * @instance * * @template {any} T * The parsed argument value * * @param {Numeric | number} index * The position of the argument. * A negative index will count back from the last argument * @return {T} * The parsed argument value */ argumentValue(index: Numeric | number): T; /** * Set an argument value source. * * @see {@linkcode ArgumentValueSource} * @see {@linkcode Numeric} * * @public * @instance * * @param {Numeric | number} index * The position of the argument * @param {ArgumentValueSource | null | undefined} source * The source of the argument value * @return {this} * `this` command */ argumentValueSource(index: Numeric | number, source: ArgumentValueSource | null | undefined): this; /** * Get an argument value source. * * @see {@linkcode ArgumentValueSource} * @see {@linkcode Numeric} * * @public * @instance * * @param {Numeric | number} index * The position of the argument. * A negative index will count back from the last argument * @return {ArgumentValueSource | undefined} * The argument value source */ argumentValueSource(index: Numeric | number): ArgumentValueSource | undefined; /** * Batch define arguments for the command. * * @see {@linkcode ArgumentInfo} * @see {@linkcode List} * * @public * @instance * * @param {List | string} infos * List of argument info and/or syntaxes, * or a string containing argument syntaxes * @return {this} * `this` command */ arguments(infos: List | string): this; /** * Get a list of command arguments. * * @see {@linkcode Argument} * * @public * @instance * * @return {Argument[]} * List of command arguments */ arguments(): Argument[]; /** * Chain a callback, calling the function after `promise` is resolved, * otherwise synchronously call `fn`. * * @see {@linkcode Awaitable} * * @protected * @instance * * @template {any} T * The resolved value of `fn` * @template {Awaitable} [Return=Awaitable] * The return value of `fn` * @template {(...args: any[]) => Return} [Fn=(...args: any[]) => Return] * The function * * @param {Awaitable} promise * The promise to chain * @param {Fn} fn * The function to call * @param {ThisParameterType | null | undefined} [self] * The `this` context of `fn` * @param {Parameters} params * The arguments to pass to `fn` * @return {Awaitable} * A new promise to resolve or the result of the `fn` */ protected chainOrCall = Awaitable, Fn extends (...args: any[]) => Return = (...args: any[]) => Return>(promise: Awaitable, fn: Fn, self?: ThisParameterType | null | undefined, ...params: Parameters): Awaitable; /** * Chain or call a hook for a command and its ancestors. * * @see {@linkcode KronkHookMap} * @see {@linkcode KronkHookName} * * @protected * @instance * * @template {KronkHookName} H * The hook name * @template {Command} T * The running command * * @param {H} hook * The hook name * @param {Command} command * The running command * @param {ReturnType} [promise] * The promise to chain * @return {ReturnType} * Nothing */ protected chainOrCallHook(hook: H, command: T, promise?: ReturnType): ReturnType; /** * Ensure `choice` is valid choice for `candidate`. * * Fails if `choice` is invalid. * * @see {@linkcode Argument} * @see {@linkcode List} * @see {@linkcode Option} * * @protected * @instance * * @param {List | string} choice * The raw argument or arguments to check * @param {Argument | Option} candidate * The current command argument or option instance * @return {never | this} * `this` command */ protected checkChoices(choice: List | string, candidate: Argument | Option): never | this; /** * Check for excess and missing command-arguments and error if any are found. * * @protected * @instance * * @return {never | this} * `this` command */ protected checkCommandArguments(): never | this; /** * Check dependent options and error if any required options are missing. * * > 👉 **Note**: Local options can depend on global options and other * > local options, but global options cannot depend on local options. * * @protected * @instance * * @return {never | this} * `this` command */ protected checkDependentOptions(): never | this; /** * Check for conflicting options and error if any are found. * * > 👉 **Note**: Local options can conflict with global options and other * > local options, but global options cannot conflict with local options. * * @protected * @instance * * @return {never | this} * `this` command */ protected checkForConflictingOptions(): never | this; /** * Check for missing mandatory options and error if any are found. * * @protected * @instance * * @return {never | this} * `this` command */ protected checkForMissingMandatoryOptions(): never | this; /** * Check for unknown options and error if any are found. * * @see {@linkcode List} * * @protected * @instance * * @param {List} unknown * List of unknown command options * @return {this} * `this` command */ protected checkForUnknownOptions(unknown: List): this; /** * Define a subcommand. * * @see {@linkcode SubcommandInfo} * * @public * @instance * * @param {SubcommandInfo | string} info * Subcommand info or name * @return {Command} * New subcommand instance */ command(info: SubcommandInfo | string): Command; /** * Define a subcommand. * * @see {@linkcode CommandData} * * @public * @instance * * @param {string} syntax * Subcommand name * @param {CommandData | null | undefined} [info] * Additional subcommand info * @return {Command} * New subcommand instance */ command(syntax: string, info?: CommandData | null | undefined): Command; /** * Batch define subcommands for the command. * * @see {@linkcode SubcommandsInfo} * * @public * @instance * * @param {SubcommandsInfo} infos * Subcommands info * @return {this} * `this` command */ commands(infos: SubcommandsInfo): this; /** * Get a subcommands map. * * Each key is a subcommand name or alias and each value is a command. * * @public * @instance * * @return {Map} * The subcommands map */ commands(): Map; /** * Copy settings that are useful to have in common across `parent` and its * subcommands. * * > 👉 **Note**: This method is used internally via {@linkcode command} so * > subcommands can inherit parent settings. * * @public * @instance * * @param {Command} parent * The parent command to copy settings from * @return {this} * `this` command */ copyInheritedSettings(parent: Command): this; /** * Create a new unattached argument. * * @see {@linkcode Argument} * @see {@linkcode ArgumentInfo} * @see {@linkcode ArgumentSyntax} * * @public * @instance * * @param {ArgumentInfo | ArgumentSyntax} info * Argument info or syntax * @return {Argument} * New argument instance */ createArgument(info: ArgumentInfo | ArgumentSyntax): Argument; /** * Create a new unattached argument. * * @see {@linkcode Argument} * @see {@linkcode ArgumentData} * @see {@linkcode ArgumentSyntax} * * @public * @instance * * @param {ArgumentSyntax} syntax * The argument syntax * @param {ArgumentData | null | undefined} [info] * Additional argument info * @return {Argument} * New argument instance */ createArgument(syntax: ArgumentSyntax, info?: ArgumentData | null | undefined): Argument; /** * Create a new unattached command. * * @see {@linkcode CommandInfo} * * @public * @instance * * @param {CommandInfo | string | null | undefined} [info] * Command info or name * @return {Command} * New command instance */ createCommand(info?: CommandInfo | string | null | undefined): Command; /** * Create a new unattached command. * * @see {@linkcode CommandData} * * @public * @instance * * @param {string | null | undefined} [name] * Command name * @param {CommandData | null | undefined} [info] * Additional command info * @return {Command} * New command instance */ createCommand(name: string | null | undefined, info?: CommandData | null | undefined): Command; /** * Create a new unattached option. * * @see {@linkcode Flags} * @see {@linkcode Option} * @see {@linkcode OptionInfo} * * @public * @instance * * @param {Flags | OptionInfo} info * Option info or flags * @return {Option} * New option instance */ createOption(info: Flags | OptionInfo): Option; /** * Create a new unattached option. * * @see {@linkcode Flags} * @see {@linkcode Option} * @see {@linkcode OptionData} * * @public * @instance * * @param {Flags} flags * Option flags * @param {OptionData | null | undefined} [info] * Option info * @return {Option} * New option instance */ createOption(flags: Flags, info?: OptionData | null | undefined): Option; /** * Emit an `event`. * * @see {@linkcode KronkEvent} * * @public * @instance * * @param {KronkEvent} event * The event to emit * @return {boolean} * `true` if event has listeners, `false` otherwise */ emit(event: KronkEvent): boolean; /** * Emit a parsed `command` event. * * @public * @instance * * @template {Command} T * The command instance * * @param {T} command * The command instance representing the parsed command * @return {boolean} * `true` if event has listeners, `false` otherwise */ emitCommand(command: T): boolean; /** * Emit environment-based options. * * > 👉 **Note**: Environment variables are applied if an option value is * > `undefined` or originally comes from an environment variable or default * > value configuration. * * @protected * @instance * * @return {this} * `this` command */ protected emitEnvironmentOptions(): this; /** * Emit implied options. * * > 👉 **Note**: Local options can imply global options and other * > local options, but global options cannot imply local options. * * @protected * @instance * * @return {this} * `this` command */ protected emitImpliedOptions(): this; /** * Emit a parsed `option` event. * * @see {@linkcode Flags} * @see {@linkcode Option} * @see {@linkcode OptionValueSource} * * @public * @instance * * @template {Option} T * The option instance * * @param {T} option * The option instance representing the parsed option * @param {unknown} value * The raw `option` value * @param {OptionValueSource} source * The source of the raw option `value` * @param {Flags | null | undefined} [flag] * The parsed `option` flag * @return {boolean} * `true` if event has listeners, `false` otherwise */ emitOption(option: T, value: RawOptionValue, source: OptionValueSource, flag?: Flags | null | undefined): boolean; /** * Emit a parsed `option` event. * * @see {@linkcode Flags} * @see {@linkcode Option} * @see {@linkcode optionValueSource} * * @public * @instance * * @template {Option} T * The option instance * * @param {T} option * The option instance representing the parsed option * @param {unknown} value * The `option` value * @param {optionValueSource.implied} source * The source of the option `value` * @param {Flags | null | undefined} [flag] * The parsed `option` flag * @return {boolean} * `true` if event has listeners, `false` otherwise */ emitOption(option: T, value: unknown, source: optionValueSource.implied, flag?: Flags | null | undefined): boolean; /** * Display an error message and exit. * * @see {@linkcode CommandErrorInfo} * @see {@linkcode KronkError} * * @public * @instance * * @param {CommandErrorInfo | KronkError} info * Info about the error or the error to display * @return {never} * Never, exits erroneously */ error(info: CommandErrorInfo | KronkError): never; /** * Add an example for the command. * * > 👉 **Note**: This method can be called more than once * > to add multiple examples. * * @see {@linkcode ExampleInfo} * * @public * @instance * * @param {ExampleInfo | ReadonlyArray | string} info * The example info or text * @return {this} * `this` command */ example(info: ExampleInfo | readonly string[] | string): this; /** * Add examples for the command. * * @see {@linkcode ExamplesData} * * @public * @instance * * @param {ExamplesData | null | undefined} examples * The example info, example text, or a list of such * @return {this} * `this` command */ examples(examples: ExamplesData | null | undefined): this; /** * Get a list of command examples. * * @see {@linkcode ExampleInfo} * * @public * @instance * * @return {ExampleInfo[]} * List of examples */ examples(): ExampleInfo[]; /** * Exit the process. * * > 👉 **Note**: The exit code ({@linkcode process.exitCode}) is set, but * > {@linkcode process.exit} is **not** called. To change this behavior, * > override the exit callback using {@linkcode exiter}. * * @see {@linkcode CommandError} * @see {@linkcode KronkError} * * @public * @instance * * @param {CommandError | KronkError} e * The error to handle * @return {never} * Never * @throws {KronkError} * If `e` is an unhandled error after calling the command exit callback */ exit(e: CommandError | KronkError): never; /** * Exit the process. * * > 👉 **Note**: The exit code ({@linkcode process.exitCode}) is set, but * > {@linkcode process.exit} is **not** called. To change this behavior, * > override the exit callback using {@linkcode exiter}. * * @see {@linkcode CommandError} * @see {@linkcode KronkError} * * @public * @instance * * @param {null | undefined} [e] * The error to handle * @return {undefined} */ exit(e?: null | undefined): undefined; /** * Set the exit callback. * * @see {@linkcode Exit} * * @public * @instance * * @param {Exit | null | undefined} exit * The callback to fire on process exit * @return {this} * `this` command */ exiter(exit: Exit | null | undefined): this; /** * Get the exit callback. * * @see {@linkcode Exit} * * @public * @instance * * @return {Exit} * The callback to fire on process exit */ exiter(): Exit; /** * Find a command with a name or alias matching `ref`. * * @see {@linkcode CommandName} * @see {@linkcode List} * * @public * @instance * * @param {CommandName | undefined} ref * A command name or alias * @return {Command | this | undefined} * Command with a name or alias matching `ref` */ findCommand(ref: CommandName | undefined): Command | this | undefined; /** * Find an option with a flag matching `flag`. * * Options known to `this` command and its ({@linkcode defaultCommand}) are * searched by default. Set `direction` to `0` to only search for options * known to the current command. * * @see {@linkcode Option} * * @public * @instance * * @param {string | null | undefined} flag * The option flag to match * @param {0 | null | undefined} [direction] * The direction to search for options * @return {Option | undefined} * Option with the long or short flag `flag` */ findOption(flag: string | null | undefined, direction?: 0 | null | undefined): Option | undefined; /** * Configure the help text. * * @see {@linkcode Help} * @see {@linkcode HelpTextOptions} * * @public * @instance * * @param {Help | HelpTextOptions | null | undefined} help * The help text utility or options for formatting help text * @return {this} * `this` command */ help(help: Help | HelpTextOptions | null | undefined): this; /** * Print the help text. * * @public * @instance * * @param {'1' | 1 | true} help * Whether to print help text * @return {undefined} */ help(help: '1' | 1 | true): undefined; /** * Get the help text utility. * * @see {@linkcode Help} * * @template {Help} T * Help text utility instance * * @public * @instance * * @return {Help} * The help text utility */ help(): T; /** * Configure the help subcommand. * * > 👉 **Note**: No cleanup is performed when this method is called * > with a different name (i.e. `help` as a string or `help.name`). * * @see {@linkcode HelpCommandData} * * @public * @instance * * @param {HelpCommandData | null | undefined} help * Subcommand instance, subcommand info, `false` to disable the help * subcommand, or any other allowed value to use the default configuration * @return {this} * `this` command */ helpCommand(help: HelpCommandData | null | undefined): this; /** * Get the help subcommand. * * @see {@linkcode Command} * * @template {Command} T * The help subcommand instance * * @public * @instance * * @return {Command | null} * Help subcommand */ helpCommand(): T | null; /** * Configure the help option. * * > 👉 **Note**: No cleanup is performed when this method is called * > with different flags (i.e. `help` as a string or `help.flags`). * * @see {@linkcode HelpOptionData} * * @public * @instance * * @param {HelpOptionData | null | undefined} help * Option flags, option info, option instance, `false` to disable the help * option, or any other allowed value to use the default configuration * @return {this} * `this` command */ helpOption(help: HelpOptionData | null | undefined): this; /** * Get the help option. * * @see {@linkcode Option} * * @template {Option} T * The help option instance * * @public * @instance * * @return {Option | null} * Help option */ helpOption(): T | null; /** * Add or remove callbacks for a `hook`. * * @see {@linkcode HooksData} * @see {@linkcode KronkHookName} * * @public * @instance * * @template {KronkHookName} H * The hook name * * @param {H} hook * The hook name * @param {HooksData[H] | false} fn * The callback or callbacks to add, * with falsy values used to remove all callbacks * @return {this} * `this` command */ hook(hook: H, fn: HooksData[H] | false): this; /** * Get a list of callbacks for `hook`. * * @see {@linkcode HooksInfo} * @see {@linkcode KronkHookName} * * @public * @instance * * @template {KronkHookName} H * The hook name * * @param {H} hook * The hook name * @return {HooksInfo[H]} * The list of hook callbacks */ hook(hook: H): HooksInfo[H]; /** * Add or remove hooks. * * @see {@linkcode HooksData} * * @public * @instance * * @param {HooksData | false | null | undefined} hooks * The hooks configuration, with falsy values used to remove hooks * @return {this} * `this` command */ hooks(hooks: HooksData | false | null | undefined): this; /** * Get a record of registered hooks. * * @see {@linkcode HooksInfo} * * @public * @instance * * @return {HooksInfo} * The hooks record */ hooks(): HooksInfo; /** * Set the name of the command. * * @see {@linkcode CommandName} * * @public * @instance * * @param {CommandName | undefined} name * Command name * @return {this} * `this` command */ id(name: CommandName | undefined): this; /** * Get the name of the command. * * @see {@linkcode CommandName} * * @public * @instance * * @return {CommandName} * Command name */ id(): CommandName; /** * Check if the given option `reference` is a match for `option`. * * @see {@linkcode Option} * * @protected * @instance * * @param {Option} option * The option instance * @param {string} reference * The option reference * @return {boolean} * `true` if `reference` is a match for `option`, `false` otherwise */ protected matchOption(option: Option, reference: string): boolean; /** * Register an `event` listener. * * @see {@linkcode KronkEvent} * @see {@linkcode KronkEventListener} * @see {@linkcode OnOptions} * * @public * @instance * * @template {KronkEvent} T * The event being listened for * * @param {T['id']} event * The name of the event being listened for * @param {KronkEventListener} listener * The event listener * @param {OnOptions | boolean | undefined} [options] * Event listening options * @return {undefined} */ on(event: T['id'], listener: KronkEventListener, options?: OnOptions | boolean | undefined): undefined; /** * Handle a command `event`. * * @see {@linkcode CommandEvent} * * @protected * @instance * * @template {Command} T * The command * * @param {CommandEvent} event * The emitted command event * @return {undefined} */ protected onCommand(event: CommandEvent): undefined; /** * Handle a help `event`. * * @see {@linkcode CommandEvent} * @see {@linkcode OptionEvent} * * @protected * @instance * * @param {CommandEvent | OptionEvent} event * The emitted event * @return {undefined} */ protected onHelp(event: CommandEvent | OptionEvent): undefined; /** * Handle a parsed option `event`. * * The method will parse the raw option-argument value using the specified * parser, as well as store the raw value source and parsed option value. * * > 👉 **Note**: This event handler is registered each time a prepared option * > is added (i.e. `command.addOption(option)`). * > For convenience, when the command version option is parsed, the command * > version (`event.option.version`) is set as the option value even though * > the option is a boolean option. * * @see {@linkcode Option} * @see {@linkcode OptionEvent} * * @protected * @instance * * @template {Option} T * The parsed option instance * * @param {OptionEvent} event * The emitted parsed option event * @return {undefined} */ protected onOption(event: OptionEvent): undefined; /** * Handle a version `event`. * * @see {@linkcode CommandEvent} * @see {@linkcode OptionEvent} * * @protected * @instance * * @param {CommandEvent | OptionEvent} event * The emitted event * @return {undefined} */ protected onVersion(event: CommandEvent | OptionEvent): undefined; /** * Define an option for the command. * * @see {@linkcode Flags} * @see {@linkcode OptionInfo} * * @public * @instance * * @param {Flags | OptionInfo} info * Option flags or info * @return {this} * `this` command */ option(info: Flags | OptionInfo): this; /** * Define an option for the command. * * @see {@linkcode Flags} * @see {@linkcode OptionData} * * @public * @instance * * @param {Flags} flags * Option flags * @param {OptionData | null | undefined} [info] * Additional option info * @return {this} * `this` command */ option(flags: Flags, info?: OptionData | null | undefined): this; /** * Set the strategy to use when merging global and local options. * * @see {@linkcode OptionPriority} * * @public * @instance * * @param {OptionPriority | null | undefined} [priority='local'] * The strategy to use when merging options * @return {this} * `this` command */ optionPriority(priority: OptionPriority | null | undefined): this; /** * Get the strategy to use when merging global and local options. * * @see {@linkcode OptionPriority} * * @public * @instance * * @return {OptionPriority} * Option merge strategy */ optionPriority(): OptionPriority; /** * Set an option value. * * @see {@linkcode Option.key} * @see {@linkcode OptionValueSource} * * @public * @instance * * @param {Option['key']} key * The option key * @param {unknown} value * The parsed option value * @param {OptionValueSource | null | undefined} [source] * The source of the option value * @return {this} * `this` command */ optionValue(key: Option['key'], value: unknown, source?: OptionValueSource | null | undefined): this; /** * Get an option value. * * @see {@linkcode Option.key} * * @public * @instance * * @template {any} T * The parsed option value * * @param {Option['key']} key * The option key * @return {T} * The parsed option value */ optionValue(key: Option['key']): T; /** * Set an option value source. * * @see {@linkcode Option.key} * @see {@linkcode OptionValueSource} * * @public * @instance * * @param {Option['key']} key * The option key * @param {OptionValueSource | null | undefined} source * The source of the option value * @return {this} * `this` command */ optionValueSource(key: Option['key'], source: OptionValueSource | null | undefined): this; /** * Get an option value source. * * @see {@linkcode Option.key} * @see {@linkcode OptionValueSource} * * @public * @instance * * @param {Option['key']} key * The option key * @return {OptionValueSource | undefined} * The option value source */ optionValueSource(key: Option['key']): OptionValueSource | undefined; /** * Batch define options for the command. * * @see {@linkcode Flags} * @see {@linkcode List} * @see {@linkcode OptionInfo} * * @public * @instance * * @param {List} infos * List of option flags and/or info * @return {this} * `this` command */ options(infos: List): this; /** * Get an options map. * * Each key is a long or short flag and each value is an option. * * @see {@linkcode Option} * * @public * @instance * * @return {Map} * The options map */ options(): Map; /** * Get a record of local option values. * * @see {@linkcode OptionValues} * * @public * @instance * * @template {OptionValues} T * Local option values type * * @return {T} * Local option values */ opts(): T; /** * Get a record of global and local option values. * * > 👉 **Note**: Local options overwrite global options by default. * > Prioritize global options (i.e. `cmd.optionPriority('global')`) to change * > this behavior. * * @see {@linkcode OptionValues} * * @public * @instance * * @template {OptionValues} T * Merged option values type * * @return {T} * Merged option values */ optsWithGlobals(): T; /** * Parse `argv`, setting options and invoking commands when defined. * * The default expectation is that the arguments are from node and have the * application as `argv[0]` and the script being run in `argv[1]`, with user * parameters after that. * * > 👉 **Note**: If any parsers or {@linkcode action} handlers are async, * > the parse needs to be awaited. * * @see {@linkcode Awaitable} * @see {@linkcode List} * @see {@linkcode ParseOptions} * * @public * @instance * * @template {Awaitable} T * The running command * * @param {List | null | undefined} [argv] * The command-line arguments * @param {ParseOptions | null | undefined} [options] * Options for parsing `argv` * @return {T} * The running command */ parse>(argv?: List | null | undefined, options?: ParseOptions | null | undefined): T; /** * Process command arguments and store parsed arguments. * * @see {@linkcode Awaitable} * * @protected * @instance * * @return {Awaitable} * `this` command */ protected parseCommandArguments(): Awaitable; /** * Parse `unknown` arguments. * * @see {@linkcode ParseUnknownResult} * * @protected * @instance * * @param {string[]} unknown * List of unknown arguments * @return {ParseUnknownResult} * Parse result */ protected parseUnknownArguments(unknown: string[]): ParseUnknownResult; /** * Process command-line arguments in the context of `this` command. * * > 👉 **Note**: Modifies `this` command by storing options. Does not reset * > state if called again. * * @see {@linkcode Awaitable} * * @protected * @instance * * @template {Awaitable} T * The command to run * * @param {string[]} operands * List of operands (not options or values) * @param {string[]} unknown * List of unknown arguments * @return {T} * The command to run */ protected prepareCommand>(operands: string[], unknown: string[]): T; /** * Get user arguments. * * @see {@linkcode List} * @see {@linkcode ParseOptions} * * @protected * @instance * * @param {List | null | undefined} [argv] * List of command-line arguments * @param {ParseOptions | null | undefined} [options] * Options for parsing `argv` * @return {string[]} * List of user arguments */ protected prepareUserArgs(argv?: List | null | undefined, options?: ParseOptions | null | undefined): string[]; /** * Print the help text. * * @see {@linkcode Awaitable} * * @protected * @instance * * @template {OptionValues} [Opts=OptionValues] * Parsed command options * @template {any[]} [Args=any[]] * Parsed command arguments * * @param {Opts} opts * The parsed command options * @param {Args} args * The parsed command arguments * @return {Awaitable} * Nothing */ protected printHelp(opts: Opts, ...args: Args): Awaitable; /** * Print the command version. * * @see {@linkcode Awaitable} * * @protected * @instance * * @template {OptionValues} [Opts=OptionValues] * Parsed command options * @template {any[]} [Args=any[]] * Parsed command arguments * * @param {Opts} opts * The parsed command options * @param {Args} args * The parsed command arguments * @return {Awaitable} * Nothing */ protected printVersion(opts: Opts, ...args: Args): Awaitable; /** * Restore the state of the command and its ancestors. * * Resets parsed values, and calls any `restore` functions defined * on argument and option parsers. * Arguments, options, and subcommands will **not** be reset. * * A promise is returned if any `restore` functions are async. * * @see {@linkcode Awaitable} * * @public * @instance * * @template {Awaitable} T * The current command * * @return {T} * `this` command */ restore>(): T; /** * Get a snapshot of `this` command. * * @see {@linkcode CommandSnapshot} * * @public * @instance * * @return {CommandSnapshot} * Command snapshot object */ snapshot(): CommandSnapshot; /** * Set the command summary. * * @public * @instance * * @param {string | null | undefined} summary * The command summary * @return {this} * `this` command */ summary(summary: string | null | undefined): this; /** * Get the command summary. * * @public * @instance * * @return {string | null} * Summary of `this` command */ summary(): string | null; /** * Get the command as a human-readable string. * * @public * @instance * @override * * @return {string} * String representation of `this` command */ toString(): string; /** * Get a list of unique subcommands. * * @public * @instance * * @template {Command} T * The command instance * * @return {Set} * The list of subcommands */ uniqueCommands(): Set; /** * Get a list of unique options. * * @see {@linkcode Option} * * @public * @instance * * @template {Option} T * The option instance * * @return {Set} * The list of options */ uniqueOptions(): Set; /** * Set the strategy for handling unknown command-line arguments. * * @see {@linkcode UnknownStrategy} * * @public * @instance * * @param {UnknownStrategy | null | undefined} [strategy=false] * Unknown command-line argument strategy * @return {this} * `this` command */ unknowns(strategy: UnknownStrategy | null | undefined): this; /** * Set the usage description. * * @see {@linkcode UsageData} * * @public * @instance * * @param {UsageData | null | undefined} usage * Usage data * @return {this} * `this` command */ usage(usage: UsageData | null | undefined): this; /** * Get the usage description. * * @see {@linkcode UsageInfo} * * @public * @instance * * @return {UsageInfo} * Usage info */ usage(): UsageInfo; /** * Get a list of options passed by the user. * * User options are have a value that is not `undefined` * and a source that is not `default`. * * @see {@linkcode Option} * @see {@linkcode OptionValueSource} * * @template {Option} T * The option instance * * @public * @instance * * @param {OptionValueSource | null | undefined} [filter] * An additional option value source filter * @return {T[]} * The list of user options */ userOptions(filter?: OptionValueSource | null | undefined): T[]; /** * Set the command version. * * @see {@linkcode Version} * * @public * @instance * * @param {Version | null | undefined} version * The command version * @return {this} * `this` command */ version(version: Version | null | undefined): this; /** * Print the command version. * * @public * @instance * * @param {true} version * Whether to print the command version * @return {undefined} */ version(version: true): undefined; /** * Get the command version. * * @public * @instance * * @template {Version} T * The command version * * @return {T | null} * The command version */ version(): T | null; /** * Configure the version option. * * > 👉 **Note**: No cleanup is performed when this method is called with * > different flags (i.e. `version` as a string or `version.flags`). * * @see {@linkcode VersionOptionData} * * @public * @instance * * @param {VersionOptionData | null | undefined} version * Option flags, option info, option instance, `false` to disable the version * option, or any other allowed value to use the default configuration * @return {this} * `this` command */ versionOption(version: VersionOptionData | null | undefined): this; /** * Get the version option. * * @see {@linkcode Option} * * @template {Option} T * The version option instance * * @public * @instance * * @return {Option | null} * Version option */ versionOption(): T | null; } export default Command;