/** * Builder functions for defining CLI options and positionals. * * Provides ergonomic helpers with full TypeScript type inference for * constructing option schemas (`opt.string()`, `opt.boolean()`, `opt.enum()`, * etc.) and positional schemas (`opt.stringPos()`, `opt.numberPos()`, * `opt.variadic()`). * * @packageDocumentation */ import type { ArrayOption, BooleanOption, CountOption, EnumArrayOption, EnumOption, EnumPositional, InferOptions, InferPositionals, NumberOption, NumberPositional, OptionsSchema, Parser, PositionalDef, StringOption, StringPositional, VariadicPositional } from "./types.cjs"; /** * A Parser that can also be called as a function to merge with another parser. * This allows `opt.options()` to work both as: * * - First arg in pipe: used directly as a Parser * - Later arg in pipe: called as function to merge with incoming Parser * * @group Type Utilities * @knipignore */ export type CallableOptionsParser = ((parser: Parser) => Parser) & Parser; /** * Extract the inferred values type from a CallableOptionsParser or Parser. * * Useful for getting the type of global options to use elsewhere in your code. * * @example * * ```typescript * const globalOptions = opt.options({ * verbose: opt.boolean(), * 'output-dir': opt.string(), * }); * * // Extract the type for use elsewhere * type GlobalOpts = InferParserValues; * // { verbose: boolean | undefined; 'output-dir': string | undefined } * ``` * * @group Type Utilities */ export type InferParserValues = T extends CallableOptionsParser ? V : T extends Parser ? V : never; /** * Namespaced option builders. * * @example * * ```typescript * import { opt } from 'bargs'; * * const parser = opt.options({ * verbose: opt.boolean({ aliases: ['v'] }), * name: opt.string({ default: 'world' }), * level: opt.enum(['low', 'medium', 'high']), * }); * ``` * * @group Core API */ export declare const opt: { /** * Define an array option (--flag value --flag value2). * * @example * * ```typescript * // Primitive array * opt.array('string'); // --file a.txt --file b.txt → ['a.txt', 'b.txt'] * opt.array('number'); // --port 80 --port 443 → [80, 443] * * // Enum array (with choices) * opt.array(['low', 'medium', 'high']); // --priority low --priority high * ``` */ array: { (items: "number" | "string", props?: Omit): ArrayOption; (choices: T, props?: Omit, "choices" | "type">): EnumArrayOption; }; /** * Define a boolean option. * * @function */ boolean:

= Omit>(props?: P) => BooleanOption & P; /** * Define a count option (--verbose --verbose = 2). * * @function */ count: (props?: Omit) => CountOption; /** * Define an enum option with string choices. * * @function */ enum: , "choices" | "type"> = Omit, "type" | "choices">>(choices: T, props?: P) => EnumOption & P; /** * Define an enum positional argument with string choices. * * @function */ enumPos: , "choices" | "type"> = Omit, "type" | "choices">>(choices: T, props?: P) => EnumPositional & P; /** * Define a number option. * * @function */ number:

= Omit>(props?: P) => NumberOption & P; /** * Define a number positional argument. * * @function */ numberPos:

= Omit>(props?: P) => NumberPositional & P; /** * Create a Parser from an options schema. * * @example * * ```typescript * const parser = opt.options({ * verbose: opt.boolean({ aliases: ['v'] }), * name: opt.string({ default: 'world' }), * }); * // Type: Parser<{ verbose: boolean | undefined, name: string }, []> * ``` */ options: (schema: T) => CallableOptionsParser>; /** * Define a string option. * * @function */ string:

= Omit>(props?: P) => P & StringOption; /** * Define a string positional argument. * * @function */ stringPos:

= Omit>(props?: P) => P & StringPositional; /** * Define a variadic positional (rest args). * * @function */ variadic: (items: "number" | "string", props?: Omit) => VariadicPositional; }; /** * A Parser that can also be called as a function to merge with another parser. * This allows `pos.positionals()` to work both as: * * - First arg in pipe: used directly as a Parser * - Later arg in pipe: called as function to merge with incoming Parser * * For positionals, we DON'T intersect values - we just pass through V2. * * @group Type Utilities * @knipignore */ export type CallablePositionalsParser

= ((parser: Parser) => Parser) & Parser; /** * Extract the inferred positionals tuple type from a CallablePositionalsParser * or Parser. * * @example * * ```typescript * const positionals = pos.positionals( * pos.string({ name: 'source', required: true }), * pos.string({ name: 'dest', required: true }), * ); * * // Extract the type for use elsewhere * type Positionals = InferParserPositionals; * // readonly [string, string] * ``` * * @group Type Utilities */ export type InferParserPositionals = T extends CallablePositionalsParser ? P : T extends Parser ? P : never; /** * Namespaced positional builders. * * @example * * ```typescript * import { pos } from 'bargs'; * * const parser = pos.positionals( * pos.string({ name: 'input', required: true }), * pos.string({ name: 'output' }), * ); * ``` * * @group Core API */ export declare const pos: { /** * Define an enum positional argument with string choices. * * @function */ enum: , "choices" | "type"> = Omit, "type" | "choices">>(choices: T, props?: P) => EnumPositional & P; /** * Define a number positional argument. * * @function */ number:

= Omit>(props?: P) => NumberPositional & P; /** * Create a Parser from positional definitions. * * @example * * ```typescript * const parser = pos.positionals( * pos.string({ name: 'input', required: true }), * pos.string({ name: 'output' }), * ); * // Type: Parser<{}, readonly [string, string | undefined]> * ``` */ positionals: { (a: A): CallablePositionalsParser[0]]>; (a: A, b: B): CallablePositionalsParser>; (a: A, b: B, c: C): CallablePositionalsParser>; (a: A, b: B, c: C, d: D): CallablePositionalsParser>; (...positionals: PositionalDef[]): CallablePositionalsParser; }; /** * Define a string positional argument. * * @function */ string:

= Omit>(props?: P) => P & StringPositional; /** * Define a variadic positional (rest args). * * @function */ variadic: (items: "number" | "string", props?: Omit) => VariadicPositional; }; //# sourceMappingURL=opt.d.ts.map