///
declare module "@run-z/optionz" {
/**
* The location of the option(s) within command line.
*/
export interface ZOptionLocation {
/**
* Command line arguments containing target option(s).
*/
readonly args: readonly string[];
/**
* An index of the first command line argument containing the option.
*/
readonly index: number;
/**
* An index of command line arguments up to which the command line contains option(s).
*
* @default Next to `index`. I.e. `index + 1`.
*/
readonly endIndex?: number | undefined;
/**
* An offset of relevant characters within the first command line argument.
*
* @default First character. I.e. `0`.
*/
readonly offset?: number | undefined;
/**
* The end offset of relevant characters within the last command line argument.
*
* @default The length of the last command line argument. I.e. `args[endIndex - 1].length`.
*/
readonly endOffset?: number | undefined;
}
export namespace ZOptionLocation {
/**
* Initial properties of option location.
*/
interface Init {
/**
* An index of the first command line argument containing the option.
*
* @default {@link ZOption.argIndex}
*/
readonly index?: number | undefined;
/**
* An index of command line arguments up to which the command line contains option(s).
*
* @default Next to `index`. I.e. `index + 1`.
*/
readonly endIndex?: number | undefined;
/**
* An offset of relevant characters within the first command line argument.
*
* @default First character. I.e. `0`.
*/
readonly offset?: number | undefined;
/**
* The end offset of relevant characters within the last command line argument.
*
* @default The length of the last command line argument. I.e. `args[endIndex - 1].length`.
*/
readonly endOffset?: number | undefined;
}
}
export const ZOptionLocation: {
/**
* Reconstructs option location.
*
* @param location - Option location.
*
* @returns Reconstructed option location with all properties set.
*/
by(location: ZOptionLocation): Required;
};
}
declare module "@run-z/optionz" {
/**
* Meta information for the option.
*
* Can either contain {@link ZOptionMeta.Help help information}, be a reference to another option this one is an
* {@link ZOptionMeta.Alias alias for}, or be {@link ZOptionMeta.Hidden hidden}.
*/
export type ZOptionMeta = ZOptionMeta.Hidden | ZOptionMeta.Alias | ZOptionMeta.Help;
export namespace ZOptionMeta {
/**
* Option help information.
*/
interface Help {
readonly hidden?: false | undefined;
/**
* Option usage description(s).
*/
readonly usage?: string | readonly string[] | undefined;
readonly aliasOf?: undefined;
/**
* Brief help text.
*/
readonly help?: string | undefined;
/**
* Full option description.
*/
readonly description?: string | undefined;
/**
* The group the option belongs to.
*
* Used when sorting options.
*/
readonly group?: string | undefined;
}
/**
* A reference to alias option.
*/
interface Alias {
readonly hidden?: false | undefined;
/**
* Option usage descriptions(s).
*/
readonly usage?: string | readonly string[] | undefined;
/**
* An option name this one is alias of.
*/
readonly aliasOf: string;
}
/**
* Hidden option.
*/
interface Hidden {
readonly hidden: true;
}
/**
* Option help information combined from multiple sources.
*/
interface Combined {
/**
* Option usage descriptions, including aliases usage.
*
* May be empty for unsupported and hidden options.
*/
readonly usage: readonly string[];
/**
* Brief help text.
*/
readonly help?: string | undefined;
/**
* Full option description.
*/
readonly description?: string | undefined;
/**
* The group the option belongs to.
*
* Used when sorting options.
*/
readonly group?: string | undefined;
}
/**
* A list of options meta information.
*
* A read-only array of key/meta tuples.
*/
type List = readonly (readonly [
string,
Combined
])[];
}
}
declare module "@run-z/optionz" {
/**
* Option reader.
*
* This can be either a {@link ZOptionReader.Fn reader function}, or its {@link ZOptionReader.Spec full specifier}.
*
* @typeParam TOption A type of command line option representation expected by reader.
* @typeParam TThis A type of `this` parameter.
*/
export type ZOptionReader = ZOptionReader.Fn | ZOptionReader.Spec;
export namespace ZOptionReader {
/**
* Option reader function (or method) signature.
*
* A reader function accepts a {@link ZOption command line option} corresponding to the key the reader is
* {@link SupportedZOptions.Map registered for} and tries to recognize it.
*
* @typeParam TOption A type of command line option representation expected by reader.
* @typeParam TThis A type of `this` parameter.
* @param option - Command line option to recognize.
*
* @returns Either nothing or promise-like instance resolved when the reader finishes option processing,
* either recognized or not.
*/
type Fn = (this: TThis, option: TOption) => void | PromiseLike;
/**
* Option reader specifier.
*
* Contains option reader function along with meta information.
*
* @typeParam TOption A type of command line option representation expected by reader.
*/
interface Spec {
/**
* Reads and tries to recognize the option.
*/
readonly read: Fn;
/**
* Option meta information.
*/
readonly meta?: ZOptionMeta | undefined;
}
}
}
declare module "@run-z/optionz" {
/**
* Base representation of command line option passed to its {@link ZOptionReader reader} in order to be recognized.
*
* A class implementing this interface along with any interface specific to the parser is created by
* {@link ZOptionsParser.Config.optionClass} method.
*
* The {@link ZOptionReader reader} uses option methods to recognize and consume command line arguments. Once recognized
* these arguments are consumed, and option reading continues from the next non-consumed argument.
*
* If multiple readers correspond to the same option key, they all consulted in order. Once one of them recognized the
* option, the remaining and {@link defer deferred} ones receive an option instance representing already consumed
* option. The latter can be read by readers, but can no longer be updated.
*/
export interface ZOption {
/**
* Option name.
*
* This is a command line {@link args argument} at {@link argIndex currently processed index}.
*/
readonly name: string;
/**
* Option key used to find corresponding option readers.
*
* This may be the same as [name], or may be e.g. a wildcard key when no readers for the option name recognized it.
* In any case, the option key is {@link ZOptionInput.key syntax-specific}.
*/
readonly key: string;
/**
* Command line arguments the option is read from.
*
* These arguments may be updated by {@link ZOptionSyntax option syntax} between the read attempts.
*/
readonly args: readonly string[];
/**
* An index of the first command line argument the reader should recognized.
*/
readonly argIndex: number;
/**
* Reads up to the maximum number of values of the option.
*
* Values are command line options following the {@link argIndex currently processed one}. Their number depend on the
* {@link ZOptionSyntax option syntax}. E.g. a `--name=value` syntax supports one value, while
* `--name value1 value2 ...` syntax supports any number of them.
*
* Calling this method marks the option and value arguments as {@link recognize recognized}. This can be undone
* by calling any recognition method again.
*
* When called on already recognized option this method just returns the values previously returned to the reader
* that recognized them.
*
* @param max - The maximum number of values to read.
*
* @returns Option values array.
*/
values(max?: number): readonly string[];
/**
* Reads up to the maximum number of the remaining command line arguments and treat them as option values.
*
* Reads [values] and all command line arguments following them up to the end of command line or their maximum number.
*
* Calling this method marks all arguments read as {@link recognize recognized}. This can be undone by calling
* any recognition method again.
*
* When called on already recognized option this method just returns the values previously returned to the reader
* that recognized them.
*
* @param max - The maximum number of arguments to read.
*
* @returns Command line arguments array.
*/
rest(max?: number): readonly string[];
/**
* Marks current option as recognized.
*
* This method is called by [rest] and [values].
*
* This method can be called multiple times to register multiple actions to call. These actions won't be called once
* the option is marked as {@link unrecognize unrecognized} again.
*
* @param action - The action to perform when the option recognized and all readers processed.
*/
recognize(action?: (this: void) => void): void;
/**
* Defers the option processing until recognized by another reader available for the same option key.
*
* Calling this method marks arguments as unrecognized. This can be changed by calling any recognition method again.
* The already registered callback will be unregistered on such call.
*
* @param whenRecognized - Optional callback function that will be called when option recognized by another reader.
*/
defer(whenRecognized?: ZOptionReader.Fn): void;
/**
* Marks current option as unrecognized.
*
* This method is called by [defer].
*
* When calling without parameter the previous reason is not updated.
*
* Does nothing if current option is recognized by another reader.
*
* @param reason - An error to throw when all readers processed and option is still unrecognized. When omitted,
* the {@link ZOptionError} will be thrown.
*/
unrecognize(reason?: unknown): void;
/**
* Allows to await for option recognition.
*
* Calling this method does not alter option recognition status in any way, unlike {@link defer recognition
* deferring}. An unlike deferring, the callback registered by this method will be called when the option recognized
* by any reader, not just the readers registered for the same option key.
*
* @param receiver - Callback function that will be called when the option recognized and all readers processed.
*/
whenRecognized(receiver: (this: void, option: this) => void): void;
/**
* Builds option location within parsed command line.
*
* @param init - Initial properties of option location.
*
* @returns Option location within parsed command line arguments.
*/
optionLocation(init?: ZOptionLocation.Init): Required;
/**
* Lists all options supported by the parser.
*
* @returns A read-only array of all supported option keys except hidden ones.
*/
supportedOptions(): readonly string[];
/**
* Returns meta information for option.
*
* @param key - Option key.
*
* @returns Combined meta information for the option with the given key.
*/
optionMeta(key: string): ZOptionMeta.Combined;
}
export namespace ZOption {
/**
* A class constructor implementing a command line option representation.
*
* This is a class to extend by {@link ZOptionsParser.Config.optionClass options parser}.
*
* @typeParam TArgs A type of constructor arguments.
*/
interface BaseClass {
prototype: ZOption;
new (...args: TArgs): ZOption;
}
/**
* A class constructor representing a parser-specific implementation of the command line option.
*
* @typeParam TOption Parser-specific command line option interface implemented by this class.
* @typeParam TCtx A type of option processing context required by parser.
* @typeParam TArgs A type of arguments to pass to the {@link BaseClass base constructor}.
*/
interface ImplClass {
prototype: TOption;
/**
* Constructs command line option representation.
*
* @param context - Option processing context.
* @param args - Arguments to pass to the {@link BaseClass base constructor}.
*/
new (context: TCtx, ...args: TArgs): TOption;
}
}
}
declare module "@run-z/optionz" {
/**
* An error thrown by {@link ZOptionsParser command line options parser} when it encounters unrecognized or malformed
* option.
*/
export class ZOptionError extends Error {
#private;
/**
* Constructs option error.
*
* @param optionLocation - Erroneous option location.
* @param message - Error message.
*/
constructor(optionLocation: ZOptionLocation, message?: string);
/**
* Erroneous option location.
*/
get optionLocation(): Required;
}
}
declare module "@run-z/optionz" {
/**
* Command line option input.
*
* This is constructed from raw command line arguments by {@link ZOptionSyntax options syntax}.
*/
export interface ZOptionInput {
/**
* A key of {@link ZOptionReader option readers} to recognize te option.
*
* @default [name]
*/
readonly key?: string | undefined;
/**
* Option name.
*
* I.e. a command line argument to process.
*/
readonly name: string;
/**
* Array of option values.
*
* I.e. command line arguments immediately following the option [name].
*
* @default Empty array.
*/
readonly values?: readonly string[] | undefined;
/**
* Array of command line arguments following the option [name] and its [values].
*/
readonly tail?: readonly string[] | undefined;
/**
* Whether to retry the option processing.
*
* When an input with this flag set is encountered, current option processing terminates and restarts for another one.
*/
readonly retry?: boolean | undefined;
}
export const ZOptionInput: {
/**
* Checks whether the given command line argument is suitable to be used as option value.
*
* This is used by default to find option values.
*
* @param arg - Command line argument to check.
*
* @returns `true` unless argument starts with `-` and not equal to `-` itself.
*/
isOptionValue(this: void, arg: string): boolean;
/**
* Extracts command line option values.
*
* This is used by default to extract option values.
*
* @param args - Command line arguments.
* @param fromIndex - First argument index to start extracting values at.
*
* @returns An array of command line {@link isOptionValue option values}.
*/
valuesOf(this: void, args: readonly string[], fromIndex?: number): readonly string[];
/**
* Checks whether two option inputs are equal to each other.
* @param first
* @param second
*/
equal(this: void, first: ZOptionInput, second: ZOptionInput): boolean;
};
}
declare module "@run-z/optionz" {
/**
* Supported command line syntax.
*
* The syntax is a function that converts raw command line arguments to {@link ZOptionInput option input} suitable
* to be recognized by {@link ZOptionReader option readers}.
*
* A syntax should be {@link ZOptionsParser.Config.syntax registered} for options parser to respect it.
*
* @param args - An array of command line arguments to process.
*
* @returns A read-only array of option inputs. May be empty if the command line argument has another syntax.
*/
export type ZOptionSyntax = (this: void, args: readonly [
string,
...string[]
]) => readonly ZOptionInput[];
export const ZOptionSyntax: {
/**
* Default command line option syntax.
*
* Includes support for:
*
* - {@link longOptions long options syntax},
* - {@link shortOptions short options syntax}, and
* - {@link any any options syntax} as a fallback.
*/
readonly default: ZOptionSyntax;
/**
* Long command line option syntax.
*
* Supports the following option formats:
*
* - `--name=VALUE`. Corresponding option key is `--name`.
* - `--name [VALUE [VALUE ...]]`. Corresponding option key is `--name=VALUE`, `--name=*`, or `--name`.
*
* Uses `--*=*` as a fallback option key for options in `--name=VALUE` format.
* Uses `--*` as a generic fallback option key.
*
* Enabled {@link default by default}.
*/
readonly longOptions: ZOptionSyntax;
/**
* Short command line option syntax.
*
* Supports the following option formats:
*
* - `-name=VALUE`. Corresponding key is `-name=VALUE`, `-name=*`, or `-name`.
* - `-name [VALUE [VALUE ...]]`. Corresponding option key is `-name`.
* - `-n[m[o...]][VALUE]`. Corresponding option key is `-n*`.
*
* Uses `-*=*` as a fallback option key for options in `-name=VALUE` format.
* Uses `-?` as a fallback option key for one-letter options.
* Uses `-*` as a generic fallback option key.
*
* Enabled {@link default by default}.
*/
readonly shortOptions: ZOptionSyntax;
/**
* Any command line option syntax.
*
* Supports `name [value1 [value2 ...]]` format. Uses `*` as a fallback option key.
*
* This is used as a fallback {@link default by default}.
*/
readonly any: ZOptionSyntax;
/**
* Builds an option syntax supporting other syntaxes.
*
* @param syntax - Either a single syntax, or an array of syntaxes to support.
*
* @returns Either a single `syntax` passed in, or a syntax supporting all of them.
*/
by(this: void, syntax: ZOptionSyntax | readonly ZOptionSyntax[]): ZOptionSyntax;
};
}
declare module "@run-z/optionz" {
/**
* A set of options supported by {@link ZOptionsParser parser}.
*
* This is either a {@link SupportedZOptions.Map map of option readers}, its {@link SupportedZOptions.Provider provider
* function}, or an array of the above. Multiple readers may be specified per option key with the latter.
*
* @typeParam TOption A type of option representation.
* @typeParam TCtx A type of option processing context required by parser.
*/
export type SupportedZOptions = SupportedZOptions.Map | SupportedZOptions.Provider | readonly (SupportedZOptions.Map | SupportedZOptions.Provider)[];
export namespace SupportedZOptions {
/**
* A map of {@link ZOptionReader readers} corresponding to option keys or their wildcards.
*
* @typeParam TCtx A type of option processing context required by parser.
* @typeParam TOption A type of option representation.
*/
interface Map {
/**
* Option reader method corresponding to option key.
*
* The latter can be either an option name, or its wildcard supported by the {@link ZOptionSyntax syntax} used.
*/
readonly [key: string]: ZOptionReader | undefined;
/**
* Fallback option reader consulted when none of the readers recognized the option in.
* {@link ZOptionSyntax:var#longOptions `--name=VALUE` syntax}.
*/
readonly '--*=*'?: ZOptionReader | undefined;
/**
* Fallback option reader consulted when none of the readers recognized the option in
* {@link ZOptionSyntax:var#longOptions long syntax}.
*/
readonly '--*'?: ZOptionReader | undefined;
/**
* Fallback option reader consulted when none of the readers recognized the option in
* {@link ZOptionSyntax:var#shortOptions one-letter syntax}.
*/
readonly '-?'?: ZOptionReader | undefined;
/**
* Fallback option reader consulted when none of the readers recognized the option in
* {@link ZOptionSyntax:var#shortOptions `-name=VALUE` syntax}.
*/
readonly '-*=*'?: ZOptionReader | undefined;
/**
* Fallback option reader consulted when none of the readers recognized the option in
* {@link ZOptionSyntax:var#shortOptions short syntax}.
*/
readonly '-*'?: ZOptionReader | undefined;
/**
* Fallback option reader consulted when none of the readers the option in {@link ZOptionSyntax:var#any any syntax}.
*/
readonly '*'?: ZOptionReader | undefined;
}
/**
* Options syntax provider signature.
*
* The provider is called at most once per {@link ZOptionsParser parser call}.
*
* @typeParam TCtx A type of option processing context required by parser.
* @typeParam TOption A type of option representation.
* @param context - Option processing context.
*
* @returns A {@link Map map of option readers}.
*/
type Provider = (this: void, context: TCtx) => Map;
}
}
declare module "@run-z/optionz" {
/**
* Command line options parser signature.
*
* @typeParam TOption A type of option representation.
* @typeParam TCtx A type of option processing context required by parser.
* @param context - Options processing context. This context is supposed to receive the processing results.
* @param args - Array of command line arguments
* @param opts - Parser options.
*
* @returns A promise resolved to processing context when parsing completes.
*/
export type ZOptionsParser = (this: void, context: TCtx, args: readonly string[], opts?: ZOptionsParser.Opts) => Promise;
export namespace ZOptionsParser {
/**
* Command line options parser configuration.
*
* @typeParam TOption A type of option representation.
* @typeParam TCtx Option processing context required by parser.
*/
interface Config {
/**
* Supported command line options.
*/
readonly options: SupportedZOptions;
/**
* Supported command line syntax.
*
* @default {@link ZOptionSyntax.default}
*/
readonly syntax?: ZOptionSyntax | readonly ZOptionSyntax[] | undefined;
/**
* Builds command line option representation class.
*
* @param base - Base option representation class.
*
* @returns Command line option representation class constructor.
*/
optionClass(base: ZOption.BaseClass): ZOption.ImplClass;
}
/**
* Additional options for command line options parser.
*
* @typeParam TOption A type of option representation.
* @typeParam TCtx A type of option processing context required by parser.
*/
interface Opts {
/**
* An index of command line argument to start processing from.
*/
readonly fromIndex?: number | undefined;
/**
* Additional options to support.
*/
readonly options?: SupportedZOptions | undefined;
}
}
/**
* Builds custom command line options parser.
*
* @typeParam TOption A type of option representation.
* @typeParam TCtx A type of option processing context required by parser.
* @param config - Parser configuration.
*
* @returns New options parser.
*/
export function customZOptionsParser(config: ZOptionsParser.Config): ZOptionsParser;
}
declare module "@run-z/optionz" {
/**
* Simple command line options parser signature.
*
* @param args - Array of command line arguments
* @param fromIndex - An index of command line argument to start processing from.
* @param opts - Parser options.
*
* @returns A promise resolved to parse result.
*/
export type SimpleZOptionsParser = (this: void, args: readonly string[], opts?: ZOptionsParser.Opts) => Promise;
export namespace SimpleZOptionsParser {
/**
* Simple command line options parser configuration.
*/
interface Config {
/**
* Supported command line options.
*
* Recognizes all options by default.
*/
readonly options?: SupportedZOptions | undefined;
/**
* Supported command line syntax.
*
* @default {@link ZOptionSyntax.default}
*/
readonly syntax?: ZOptionSyntax | readonly ZOptionSyntax[] | undefined;
}
/**
* A result returned by simple command line option parser.
*/
interface Result {
/**
* Maps recognized option name to array of its values.
*/
readonly [name: string]: readonly string[];
}
}
/**
* Builds a simple command line options parser.
*
* @param config - Parser configuration.
*
* @returns New options parser.
*/
export function simpleZOptionsParser(config?: SimpleZOptionsParser.Config): SimpleZOptionsParser;
}
declare module "@run-z/optionz" {
/**
* Parses command line options.
*
* @param args - Array of command line arguments
* @param opts - Parser options.
*
* @returns A promise resolved to a map of recognized option names to arrays of their values.
*/
export function parseZOptions(args: readonly string[], opts?: ZOptionsParser.Opts): Promise;
}
//# sourceMappingURL=optionz.d.ts.map