UNPKG

@jondotsoy/flags

Version:

A Javascript arguments processor

66 lines (65 loc) 3.13 kB
export { FlagsError } from "./src/common/errors/flags.error.js"; export interface Spec { names?: string[]; category?: string; description?: string; } export interface Context<T> { nextIndex: number; args: string[]; index: number; arg: string; argValue: null | string; flags: Partial<T>; } export type Test<T> = ((arg: string, ctx: Context<T>) => boolean) & Spec; export interface Handler<T> { (ctx: Context<T>): void; } export type Rule<T> = [Test<T>, Handler<T>]; export declare const rule: <T>(test: Test<T>, handler: Handler<T>, ...specs: Spec[]) => Rule<T>; export declare const flag: <T>(...flags: string[]) => Test<T>; export declare const command: <T>(command: string) => Test<T>; /** @deprecated prefer {@link argument} */ export declare const commandOption: <T>(optionName: keyof T) => (arg: string, ctx: Context<T>) => boolean; /** * Creates a handler function for processing command-line flags and updating a property on the flags object. * * @template T - The type of the flags object. * @param propName - The property name on the flags object to update. * @param reducer - A function that receives the current context, the accumulated value, and the flag value (as a string or null), * and returns the next value to set for the property. * @param requireValue - Optional. If `true` (default), the handler expects a value for the flag; if `false`, the flag is treated as a boolean. * @returns A handler function that processes the flag and updates the specified property on the flags object. * * @remarks * The handler uses the provided reducer to determine the next value for the property. * If `requireValue` is enabled and no value is provided, the handler advances the argument index. */ export declare const flagHandler: <T>(propName: keyof T, reducer: (ctx: Context<T>, accumulate: unknown, value: string | null) => unknown, requireValue?: boolean) => Handler<T>; export declare const isBooleanAt: <T>(propName: keyof T) => Handler<T>; export declare const isStringAt: <T>(propName: keyof T) => Handler<T>; export declare const isNumberAt: <T>(propName: keyof T) => Handler<T>; export declare const isArrayStringAt: <T>(propName: keyof T) => Handler<T>; export declare const isArrayNumberAt: <T>(propName: keyof T) => Handler<T>; export declare const any: <T>() => Test<T>; export declare const restArgumentsAt: <T>(propName: keyof T) => Handler<T>; export declare const describe: <D extends Test<any>>(test: D, ...specs: Spec[]) => D; export declare function getSpecs(rules: Rule<any>[]): Generator<{ description?: string; category?: string; names?: string[]; }>; export declare const makeHelpMessage: (command: string, rules: Rule<any>[], samples?: string[]) => string; export declare const flags: <T>(args: string[], init: Partial<T>, parses: Rule<T>[]) => Partial<T>; /** * Catch the next argument. * * @example * const options = flags(["foo"],{}, [rule(argument(), isStringAt("arg"))]) * options.arg // => "foo" */ export declare const argument: { (): (arg: string, ctx: Context<any>) => boolean; visited: Set<symbol>; };