| var P=Object.defineProperty;var i=(e,t)=>P(e,"name",{value:t,configurable:!0});const g=i(e=>(typeof e=="object"||typeof e=="function")&&e!==null&&"~standard"in e,"isStandardSchema"),$=i(e=>(t=>{const n=e["~standard"].validate(t);if(n instanceof Promise)throw new TypeError("Async schemas are not supported");if(n.issues)throw new Error(n.issues[0]?.message??"Validation failed");return n.value}),"schemaToParser"),x=/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/g,m=i(e=>e.replaceAll(x,"-").toLowerCase(),"flagNameToKebab"),{hasOwnProperty:F}=Object.prototype,u=i((e,t)=>F.call(e,t),"hasOwn"),d=i(e=>g(e)?[$(e),!1]:typeof e=="function"?[e,!1]:Array.isArray(e)?[d(e[0])[0],!0]:d(e.type),"parseFlagType"),N=i((e,t)=>e===Boolean?t!=="false":t,"normalizeBoolean"),D=i((e,t,n)=>{if(typeof t=="boolean")return t;if(e===Number&&t==="")return Number.NaN;try{return e(t)}catch(r){throw new TypeError(`Flag "--${n}": ${r instanceof Error?r.message:r}`,{cause:r})}},"applyParser"),O=/[\s.:=]/,V=i(e=>{const t=`Flag name "${e}"`;if(e.length===0)throw new Error(`${t} cannot be empty`);const n=e.match(O);if(n)throw new Error(`${t} cannot contain "${n?.[0]}"`)},"validateFlagName"),h=i((e,t,n)=>{if(u(e,t))throw new Error(`Duplicate flags named "${t}"`);e[t]=n},"setFlag"),C=i(e=>{const t={};for(const n in e){if(!u(e,n))continue;V(n);const r=e[n],s=[[],...d(r),r];h(t,n,s);const a=m(n);if(n!==a&&h(t,a,s),"alias"in r&&typeof r.alias=="string"){const{alias:o}=r,c=`Flag alias "${o}" for flag "${n}"`;if(n.length===1)throw new Error(`${c} cannot be defined for a single-character flag`);if(o.length===0)throw new Error(`${c} cannot be empty`);if(o.length>1)throw new Error(`${c} must be a single character`);h(t,o,s)}}return t},"createRegistry"),I=i((e,t)=>{const n={};for(const r in e){if(!u(e,r))continue;const[s,,a,o]=t[r];if(s.length===0&&!g(o)&&"default"in o){let{default:c}=o;typeof c=="function"&&(c=c()),n[r]=c}else n[r]=a?s:s.pop()}return n},"finalizeFlags"),p="--",L=3,T=/^-{1,2}\w/,A=i(e=>{if(!T.test(e))return;const t=!e.startsWith(p);let n=e.slice(t?1:2),r,s=-1;for(const a of["=",":","."]){const o=n.indexOf(a);o!==-1&&(s===-1||o<s)&&(s=o)}return s!==-1&&(r=n.slice(s+1),n=n.slice(0,s)),[n,r,t]},"parseFlagArgv"),j=i((e,{onFlag:t,onArgument:n})=>{let r;const s=i((a,o)=>{if(typeof r!="function")return!0;r(a,o),r=void 0},"triggerValueCallback");for(let a=0;a<e.length;a+=1){const o=e[a];if(o===p){s();const l=e.slice(a+1);n?.(l,[a],!0);break}const c=A(o);if(c){if(s(),!t)continue;const[l,b,E]=c;if(E)for(let f=0;f<l.length;f+=1){s();const y=f===l.length-1;r=t(l[f],y?b:void 0,[a,f+1,y])}else r=t(l,b,[a])}else s(o,[a])&&n?.([o],[a])}s()},"argvIterator"),k=i((e,t)=>{for(let n=t.length-1;n>=0;n-=1){const[r,s,a]=t[n];if(s){const o=e[r];let c=o.slice(0,s);if(a||(c+=o.slice(s+1)),c!=="-"){e[r]=c;continue}}e.splice(r,1)}},"spliceFromArgv"),w=i((e,t)=>Object.assign(e,{[p]:t}),"createPositionalArgumentsFromParts"),z=i(e=>{const t=e.indexOf(p);if(t===-1)return w([...e],[]);const n=e.slice(0,t),r=e.slice(t+1);return w([...n,...r],r)},"createPositionalArguments");export{L as A,j as a,w as b,C as c,D as d,d as e,I as f,z as g,u as h,m as i,g as j,N as n,A as p,k as s}; |
| import { P as PositionalArguments } from './types-eI9jH-uD.js'; | ||
| export { d as StandardSchemaV1, i as isStandardSchema } from './types-eI9jH-uD.js'; | ||
| /** | ||
| * Normalize a schema-declared flag name (e.g. `orgID`, `apiURL`, `fooBar`) | ||
| * to the kebab-case form that matches argv tokens (`--org-id`, `--api-url`, | ||
| * `--foo-bar`). Preserves acronyms as single segments. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * flagNameToKebab('orgID') // => 'org-id' | ||
| * flagNameToKebab('apiURL') // => 'api-url' | ||
| * flagNameToKebab('parseJSONData') // => 'parse-json-data' | ||
| * flagNameToKebab('fooBar') // => 'foo-bar' | ||
| * ``` | ||
| */ | ||
| declare const flagNameToKebab: (name: string) => string; | ||
| /** | ||
| * Build a {@link PositionalArguments} array from raw argv, splitting on the | ||
| * `--` delimiter so tokens after `--` are also exposed on the `'--'` property. | ||
| */ | ||
| declare const createPositionalArguments: (argv: readonly string[]) => PositionalArguments; | ||
| export { createPositionalArguments, flagNameToKebab }; |
| import{g as s,i as o,j as r}from"./internal-DRerJ2G5.mjs";export{s as createPositionalArguments,o as flagNameToKebab,r as isStandardSchema}; |
| /** | ||
| * Standard Schema (https://standardschema.dev) support: the vendored v1 spec | ||
| * types plus the runtime helpers type-flag uses to detect and adapt schemas. | ||
| * The spec is vendored rather than depended on to keep type-flag zero-dependency. | ||
| */ | ||
| /** | ||
| * Minimal vendored subset of the Standard Schema spec, v1. Mirrors | ||
| * `@standard-schema/spec`. Kept byte-compatible with upstream, which relies on a | ||
| * namespace merged with an interface of the same name, so the conflicting | ||
| * stylistic rules are disabled here. | ||
| */ | ||
| declare namespace StandardSchemaV1 { | ||
| interface Props<Input = unknown, Output = Input> { | ||
| readonly version: 1; | ||
| readonly vendor: string; | ||
| readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>; | ||
| readonly types?: Types<Input, Output> | undefined; | ||
| } | ||
| type Result<Output> = SuccessResult<Output> | FailureResult; | ||
| interface SuccessResult<Output> { | ||
| readonly value: Output; | ||
| readonly issues?: undefined; | ||
| } | ||
| interface FailureResult { | ||
| readonly issues: ReadonlyArray<Issue>; | ||
| } | ||
| interface Issue { | ||
| readonly message: string; | ||
| readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined; | ||
| } | ||
| interface PathSegment { | ||
| readonly key: PropertyKey; | ||
| } | ||
| interface Types<Input = unknown, Output = Input> { | ||
| readonly input: Input; | ||
| readonly output: Output; | ||
| } | ||
| type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output']; | ||
| } | ||
| interface StandardSchemaV1<Input = unknown, Output = Input> { | ||
| readonly '~standard': StandardSchemaV1.Props<Input, Output>; | ||
| } | ||
| /** | ||
| * Detect a Standard Schema (Zod, Valibot, ArkType, ...). Used to accept schemas | ||
| * directly as flag types, and exported so tools built on type-flag can apply the | ||
| * same check when introspecting flag definitions. | ||
| */ | ||
| declare const isStandardSchema: (value: unknown) => value is StandardSchemaV1; | ||
| type Simplify<T> = { | ||
| [Key in keyof T]: T[Key]; | ||
| } & {}; | ||
| /** | ||
| * A function that processes a command-line argument and returns a typed value. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const toUpperCase = (value: string) => value.toUpperCase(); | ||
| * ``` | ||
| */ | ||
| type TypeFunction<ReturnType = unknown> = (...args: any[]) => ReturnType; | ||
| /** | ||
| * The value used to type a flag: a parser function or a Standard Schema. | ||
| */ | ||
| type FlagTypeValue = TypeFunction | StandardSchemaV1; | ||
| /** | ||
| * A shorthand for defining a flag's type. | ||
| * | ||
| * - Use a single `TypeFunction` or Standard Schema to accept one value. | ||
| * - Use a readonly tuple (e.g. `[TypeFunction]`) to accept multiple values (as an array). | ||
| * | ||
| * @see FlagSchema | ||
| */ | ||
| type FlagType = (FlagTypeValue | readonly [FlagTypeValue]); | ||
| /** | ||
| * Workaround for TypeScript bug where `Readonly<T>` in parameter position breaks | ||
| * conditional type matching in return type. Adding `& AnyObject` to extends clauses | ||
| * fixes the matcher. | ||
| * | ||
| * @see https://github.com/microsoft/TypeScript/issues/62720 | ||
| */ | ||
| type AnyObject = Record<PropertyKey, unknown>; | ||
| /** | ||
| * Defines the complete schema for a command-line flag. | ||
| */ | ||
| type FlagSchema = { | ||
| /** | ||
| * The function or tuple of functions that parse the `argv` string into a typed value. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * type: String | ||
| * ``` | ||
| * @example | ||
| * ```ts | ||
| * type: [Boolean] | ||
| * ``` | ||
| * @example | ||
| * ```ts | ||
| * type: (value: string) => moment(value).toDate() | ||
| * ``` | ||
| */ | ||
| type: FlagType; | ||
| /** | ||
| * A single-character alias for the flag. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * alias: 's' | ||
| * ``` | ||
| */ | ||
| alias?: string; | ||
| /** | ||
| * The default value for the flag if not provided. | ||
| * Can also be a function that returns the default. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * default: 'hello' | ||
| * ``` | ||
| * @example | ||
| * ```ts | ||
| * default: () => [1, 2, 3] | ||
| * ``` | ||
| */ | ||
| default?: unknown | (() => unknown); | ||
| } & AnyObject; | ||
| /** | ||
| * A flag definition can either be a `FlagType` or a full `FlagSchema` object. | ||
| */ | ||
| type FlagTypeOrSchema<ExtraOptions = Record<string, unknown>> = FlagType | (FlagSchema & ExtraOptions); | ||
| /** | ||
| * A map of flag names to their definitions. | ||
| */ | ||
| type Flags<ExtraOptions = Record<string, unknown>> = { | ||
| [flagName: string]: FlagTypeOrSchema<ExtraOptions>; | ||
| }; | ||
| /** | ||
| * Positional arguments with post-`--` values exposed separately. | ||
| */ | ||
| type PositionalArguments = string[] & { | ||
| /** Arguments that appeared after the `--` separator. */ | ||
| '--': string[]; | ||
| }; | ||
| type InferDefaultType<Flag extends FlagTypeOrSchema, Fallback> = Flag extends { | ||
| default: infer DefaultType | (() => infer DefaultType); | ||
| } & AnyObject ? DefaultType : Fallback; | ||
| /** | ||
| * Resolves the output type of a single flag-type element: a parser function's | ||
| * return type, or a Standard Schema's output type. | ||
| */ | ||
| type InferType<Element> = (Element extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<Element> : Element extends TypeFunction<infer Return> ? Return : never); | ||
| type FlagTypeOf<Flag> = (Flag extends { | ||
| type: infer Type extends FlagType; | ||
| } & AnyObject ? Type : Flag); | ||
| /** | ||
| * Infers the final JavaScript type of a flag from its schema. | ||
| * | ||
| * `FlagTypeOf` unwraps the `{ type }` object form first, so only the two | ||
| * underlying shapes (array vs scalar) need matching. `InferType` then resolves | ||
| * each element whether it is a function or a schema. | ||
| */ | ||
| type InferFlagType<Flag extends FlagTypeOrSchema> = (FlagTypeOf<Flag> extends readonly [infer Element extends FlagTypeValue] ? InferArrayType<Flag, Element> : FlagTypeOf<Flag> extends infer Element extends FlagTypeValue ? InferScalarType<Flag, Element> : unknown); | ||
| type InferArrayType<Flag extends FlagTypeOrSchema, Element> = (InferType<Element> extends infer Output ? (Output[] | InferDefaultType<Flag, never>) : never); | ||
| type InferScalarType<Flag extends FlagTypeOrSchema, Element> = (InferType<Element> extends infer Output ? ([ | ||
| Output | ||
| ] extends [never] ? Output : (Output | InferDefaultType<Flag, undefined>)) : never); | ||
| /** | ||
| * The fully inferred return type from a given flag schema configuration. | ||
| */ | ||
| type TypeFlag<Schemas extends Flags = Flags> = { | ||
| /** Parsed values keyed by flag name. */ | ||
| flags: { | ||
| [flag in keyof Schemas]: InferFlagType<Schemas[flag]>; | ||
| }; | ||
| /** Flags that were passed but not defined in the schema. */ | ||
| unknownFlags: { | ||
| [flagName: string]: (string | boolean)[]; | ||
| }; | ||
| /** | ||
| * Positional arguments (non-flag values). | ||
| * Includes a special `"--"` key for arguments after the double dash. | ||
| */ | ||
| _: PositionalArguments; | ||
| }; | ||
| /** Constant indicating a known flag token type. */ | ||
| declare const KNOWN_FLAG = "known-flag"; | ||
| /** Constant indicating an unknown flag token type. */ | ||
| declare const UNKNOWN_FLAG = "unknown-flag"; | ||
| /** Constant indicating a positional argument token type. */ | ||
| declare const ARGUMENT = "argument"; | ||
| /** | ||
| * A function to dynamically ignore specific elements during parsing. | ||
| * Return `true` to skip the element, or `false`/`undefined` to process it normally. | ||
| * | ||
| * @param type - The type of element being processed: | ||
| * - `'argument'`: A positional argument (non-flag value) | ||
| * - `'known-flag'`: A flag defined in the schema | ||
| * - `'unknown-flag'`: A flag not defined in the schema | ||
| * @param argvElement - The raw argv string. For arguments, this is the value itself. | ||
| * For flags, this is the flag name (e.g., `'--verbose'` or `'-v'`). | ||
| * @param flagValue - The value associated with a flag, if any. | ||
| * - For flags with explicit values: the string value (e.g., `'--port=3000'` → `'3000'`) | ||
| * - For boolean flags or flags without values: `undefined` | ||
| * - For arguments (`type === 'argument'`): always `undefined` | ||
| * @returns `true` to ignore/skip this element, `false` or `undefined` to process it | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ignore all unknown flags | ||
| * ignore: (type) => type === 'unknown-flag' | ||
| * ``` | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ignore arguments starting with a dot | ||
| * ignore: (type, argvElement) => type === 'argument' && argvElement.startsWith('.') | ||
| * ``` | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ignore a specific flag | ||
| * ignore: (type, argvElement) => argvElement === '--internal-only' | ||
| * ``` | ||
| */ | ||
| type IgnoreFunction = (type: typeof ARGUMENT | typeof KNOWN_FLAG | typeof UNKNOWN_FLAG, argvElement: string, flagValue?: string) => boolean | void; | ||
| /** | ||
| * Options to customize the flag parsing behavior. | ||
| */ | ||
| type TypeFlagOptions = { | ||
| /** | ||
| * Optional function to skip certain argv elements from parsing. | ||
| */ | ||
| ignore?: IgnoreFunction; | ||
| /** | ||
| * Enable `--no-<flag>` negation for boolean flags. | ||
| * | ||
| * When enabled, `--no-verbose` is equivalent to `--verbose=false`. | ||
| * Only applies to flags defined as `Boolean` in the schema. | ||
| * Last-wins semantics apply between `--flag` and `--no-flag`. | ||
| */ | ||
| booleanNegation?: boolean; | ||
| }; | ||
| export { StandardSchemaV1 as d, isStandardSchema as i }; | ||
| export type { Flags as F, InferFlagType as I, PositionalArguments as P, Simplify as S, TypeFlagOptions as T, TypeFlag as a, FlagType as b, IgnoreFunction as c }; |
+3
-263
@@ -1,247 +0,5 @@ | ||
| /** | ||
| * Standard Schema (https://standardschema.dev) support: the vendored v1 spec | ||
| * types plus the runtime helpers type-flag uses to detect and adapt schemas. | ||
| * The spec is vendored rather than depended on to keep type-flag zero-dependency. | ||
| */ | ||
| import { F as Flags, T as TypeFlagOptions, S as Simplify, a as TypeFlag, b as FlagType, I as InferFlagType } from './types-eI9jH-uD.js'; | ||
| export { c as IgnoreFunction, P as PositionalArguments } from './types-eI9jH-uD.js'; | ||
| /** | ||
| * Minimal vendored subset of the Standard Schema spec, v1. Mirrors | ||
| * `@standard-schema/spec`. Kept byte-compatible with upstream, which relies on a | ||
| * namespace merged with an interface of the same name, so the conflicting | ||
| * stylistic rules are disabled here. | ||
| */ | ||
| declare namespace StandardSchemaV1 { | ||
| interface Props<Input = unknown, Output = Input> { | ||
| readonly version: 1; | ||
| readonly vendor: string; | ||
| readonly validate: (value: unknown) => Result<Output> | Promise<Result<Output>>; | ||
| readonly types?: Types<Input, Output> | undefined; | ||
| } | ||
| type Result<Output> = SuccessResult<Output> | FailureResult; | ||
| interface SuccessResult<Output> { | ||
| readonly value: Output; | ||
| readonly issues?: undefined; | ||
| } | ||
| interface FailureResult { | ||
| readonly issues: ReadonlyArray<Issue>; | ||
| } | ||
| interface Issue { | ||
| readonly message: string; | ||
| readonly path?: ReadonlyArray<PropertyKey | PathSegment> | undefined; | ||
| } | ||
| interface PathSegment { | ||
| readonly key: PropertyKey; | ||
| } | ||
| interface Types<Input = unknown, Output = Input> { | ||
| readonly input: Input; | ||
| readonly output: Output; | ||
| } | ||
| type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema['~standard']['types']>['output']; | ||
| } | ||
| interface StandardSchemaV1<Input = unknown, Output = Input> { | ||
| readonly '~standard': StandardSchemaV1.Props<Input, Output>; | ||
| } | ||
| /** | ||
| * Detect a Standard Schema (Zod, Valibot, ArkType, ...). Used to accept schemas | ||
| * directly as flag types, and exported so tools built on type-flag can apply the | ||
| * same check when introspecting flag definitions. | ||
| */ | ||
| declare const isStandardSchema: (value: unknown) => value is StandardSchemaV1; | ||
| type Simplify<T> = { | ||
| [Key in keyof T]: T[Key]; | ||
| } & {}; | ||
| /** | ||
| * A function that processes a command-line argument and returns a typed value. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const toUpperCase = (value: string) => value.toUpperCase(); | ||
| * ``` | ||
| */ | ||
| type TypeFunction<ReturnType = unknown> = (...args: any[]) => ReturnType; | ||
| /** | ||
| * The value used to type a flag: a parser function or a Standard Schema. | ||
| */ | ||
| type FlagTypeValue = TypeFunction | StandardSchemaV1; | ||
| /** | ||
| * A shorthand for defining a flag's type. | ||
| * | ||
| * - Use a single `TypeFunction` or Standard Schema to accept one value. | ||
| * - Use a readonly tuple (e.g. `[TypeFunction]`) to accept multiple values (as an array). | ||
| * | ||
| * @see FlagSchema | ||
| */ | ||
| type FlagType = (FlagTypeValue | readonly [FlagTypeValue]); | ||
| /** | ||
| * Workaround for TypeScript bug where `Readonly<T>` in parameter position breaks | ||
| * conditional type matching in return type. Adding `& AnyObject` to extends clauses | ||
| * fixes the matcher. | ||
| * | ||
| * @see https://github.com/microsoft/TypeScript/issues/62720 | ||
| */ | ||
| type AnyObject = Record<PropertyKey, unknown>; | ||
| /** | ||
| * Defines the complete schema for a command-line flag. | ||
| */ | ||
| type FlagSchema = { | ||
| /** | ||
| * The function or tuple of functions that parse the `argv` string into a typed value. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * type: String | ||
| * ``` | ||
| * @example | ||
| * ```ts | ||
| * type: [Boolean] | ||
| * ``` | ||
| * @example | ||
| * ```ts | ||
| * type: (value: string) => moment(value).toDate() | ||
| * ``` | ||
| */ | ||
| type: FlagType; | ||
| /** | ||
| * A single-character alias for the flag. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * alias: 's' | ||
| * ``` | ||
| */ | ||
| alias?: string; | ||
| /** | ||
| * The default value for the flag if not provided. | ||
| * Can also be a function that returns the default. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * default: 'hello' | ||
| * ``` | ||
| * @example | ||
| * ```ts | ||
| * default: () => [1, 2, 3] | ||
| * ``` | ||
| */ | ||
| default?: unknown | (() => unknown); | ||
| } & AnyObject; | ||
| /** | ||
| * A flag definition can either be a `FlagType` or a full `FlagSchema` object. | ||
| */ | ||
| type FlagTypeOrSchema<ExtraOptions = Record<string, unknown>> = FlagType | (FlagSchema & ExtraOptions); | ||
| /** | ||
| * A map of flag names to their definitions. | ||
| */ | ||
| type Flags<ExtraOptions = Record<string, unknown>> = { | ||
| [flagName: string]: FlagTypeOrSchema<ExtraOptions>; | ||
| }; | ||
| /** | ||
| * Positional arguments with post-`--` values exposed separately. | ||
| */ | ||
| type PositionalArguments = string[] & { | ||
| /** Arguments that appeared after the `--` separator. */ | ||
| '--': string[]; | ||
| }; | ||
| type InferDefaultType<Flag extends FlagTypeOrSchema, Fallback> = Flag extends { | ||
| default: infer DefaultType | (() => infer DefaultType); | ||
| } & AnyObject ? DefaultType : Fallback; | ||
| /** | ||
| * Resolves the output type of a single flag-type element: a parser function's | ||
| * return type, or a Standard Schema's output type. | ||
| */ | ||
| type InferType<Element> = (Element extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<Element> : Element extends TypeFunction<infer Return> ? Return : never); | ||
| type FlagTypeOf<Flag> = (Flag extends { | ||
| type: infer Type extends FlagType; | ||
| } & AnyObject ? Type : Flag); | ||
| /** | ||
| * Infers the final JavaScript type of a flag from its schema. | ||
| * | ||
| * `FlagTypeOf` unwraps the `{ type }` object form first, so only the two | ||
| * underlying shapes (array vs scalar) need matching. `InferType` then resolves | ||
| * each element whether it is a function or a schema. | ||
| */ | ||
| type InferFlagType<Flag extends FlagTypeOrSchema> = (FlagTypeOf<Flag> extends readonly [infer Element extends FlagTypeValue] ? InferArrayType<Flag, Element> : FlagTypeOf<Flag> extends infer Element extends FlagTypeValue ? InferScalarType<Flag, Element> : unknown); | ||
| type InferArrayType<Flag extends FlagTypeOrSchema, Element> = (InferType<Element> extends infer Output ? (Output[] | InferDefaultType<Flag, never>) : never); | ||
| type InferScalarType<Flag extends FlagTypeOrSchema, Element> = (InferType<Element> extends infer Output ? ([ | ||
| Output | ||
| ] extends [never] ? Output : (Output | InferDefaultType<Flag, undefined>)) : never); | ||
| /** | ||
| * The fully inferred return type from a given flag schema configuration. | ||
| */ | ||
| type TypeFlag<Schemas extends Flags = Flags> = { | ||
| /** Parsed values keyed by flag name. */ | ||
| flags: { | ||
| [flag in keyof Schemas]: InferFlagType<Schemas[flag]>; | ||
| }; | ||
| /** Flags that were passed but not defined in the schema. */ | ||
| unknownFlags: { | ||
| [flagName: string]: (string | boolean)[]; | ||
| }; | ||
| /** | ||
| * Positional arguments (non-flag values). | ||
| * Includes a special `"--"` key for arguments after the double dash. | ||
| */ | ||
| _: PositionalArguments; | ||
| }; | ||
| /** Constant indicating a known flag token type. */ | ||
| declare const KNOWN_FLAG = "known-flag"; | ||
| /** Constant indicating an unknown flag token type. */ | ||
| declare const UNKNOWN_FLAG = "unknown-flag"; | ||
| /** Constant indicating a positional argument token type. */ | ||
| declare const ARGUMENT = "argument"; | ||
| /** | ||
| * A function to dynamically ignore specific elements during parsing. | ||
| * Return `true` to skip the element, or `false`/`undefined` to process it normally. | ||
| * | ||
| * @param type - The type of element being processed: | ||
| * - `'argument'`: A positional argument (non-flag value) | ||
| * - `'known-flag'`: A flag defined in the schema | ||
| * - `'unknown-flag'`: A flag not defined in the schema | ||
| * @param argvElement - The raw argv string. For arguments, this is the value itself. | ||
| * For flags, this is the flag name (e.g., `'--verbose'` or `'-v'`). | ||
| * @param flagValue - The value associated with a flag, if any. | ||
| * - For flags with explicit values: the string value (e.g., `'--port=3000'` → `'3000'`) | ||
| * - For boolean flags or flags without values: `undefined` | ||
| * - For arguments (`type === 'argument'`): always `undefined` | ||
| * @returns `true` to ignore/skip this element, `false` or `undefined` to process it | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ignore all unknown flags | ||
| * ignore: (type) => type === 'unknown-flag' | ||
| * ``` | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ignore arguments starting with a dot | ||
| * ignore: (type, argvElement) => type === 'argument' && argvElement.startsWith('.') | ||
| * ``` | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // Ignore a specific flag | ||
| * ignore: (type, argvElement) => argvElement === '--internal-only' | ||
| * ``` | ||
| */ | ||
| type IgnoreFunction = (type: typeof ARGUMENT | typeof KNOWN_FLAG | typeof UNKNOWN_FLAG, argvElement: string, flagValue?: string) => boolean | void; | ||
| /** | ||
| * Options to customize the flag parsing behavior. | ||
| */ | ||
| type TypeFlagOptions = { | ||
| /** | ||
| * Optional function to skip certain argv elements from parsing. | ||
| */ | ||
| ignore?: IgnoreFunction; | ||
| /** | ||
| * Enable `--no-<flag>` negation for boolean flags. | ||
| * | ||
| * When enabled, `--no-verbose` is equivalent to `--verbose=false`. | ||
| * Only applies to flags defined as `Boolean` in the schema. | ||
| * Last-wins semantics apply between `--flag` and `--no-flag`. | ||
| */ | ||
| booleanNegation?: boolean; | ||
| }; | ||
| /** | ||
| type-flag: typed argv parser | ||
@@ -270,20 +28,2 @@ | ||
| /** | ||
| * Normalize a schema-declared flag name (e.g. `orgID`, `apiURL`, `fooBar`) | ||
| * to the kebab-case form that matches argv tokens (`--org-id`, `--api-url`, | ||
| * `--foo-bar`). Preserves acronyms as single segments. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * flagNameToKebab('orgID') // => 'org-id' | ||
| * flagNameToKebab('apiURL') // => 'api-url' | ||
| * flagNameToKebab('parseJSONData') // => 'parse-json-data' | ||
| * flagNameToKebab('fooBar') // => 'foo-bar' | ||
| * ``` | ||
| */ | ||
| declare const flagNameToKebab: (name: string) => string; | ||
| declare const createPositionalArguments: (argv: readonly string[]) => PositionalArguments; | ||
| export { StandardSchemaV1, createPositionalArguments, flagNameToKebab, getFlag, isStandardSchema, typeFlag }; | ||
| export type { Flags, IgnoreFunction, PositionalArguments, TypeFlag, TypeFlagOptions }; | ||
| export { Flags, TypeFlag, TypeFlagOptions, getFlag, typeFlag }; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| var j=Object.defineProperty;var a=(t,e)=>j(t,"name",{value:e,configurable:!0});const z="known-flag",G="unknown-flag",K="argument",F=a(t=>(typeof t=="object"||typeof t=="function")&&t!==null&&"~standard"in t,"isStandardSchema"),R=a(t=>(e=>{const s=t["~standard"].validate(e);if(s instanceof Promise)throw new TypeError("Async schemas are not supported");if(s.issues)throw new Error(s.issues[0]?.message??"Validation failed");return s.value}),"schemaToParser"),W=/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/g,k=a(t=>t.replaceAll(W,"-").toLowerCase(),"flagNameToKebab"),{hasOwnProperty:U}=Object.prototype,d=a((t,e)=>U.call(t,e),"hasOwn"),A=a(t=>F(t)?[R(t),!1]:typeof t=="function"?[t,!1]:Array.isArray(t)?[A(t[0])[0],!0]:A(t.type),"parseFlagType"),D=a((t,e)=>t===Boolean?e!=="false":e,"normalizeBoolean"),L=a((t,e,s)=>{if(typeof e=="boolean")return e;if(t===Number&&e==="")return Number.NaN;try{return t(e)}catch(n){throw new TypeError(`Flag "--${s}": ${n instanceof Error?n.message:n}`,{cause:n})}},"applyParser"),Z=/[\s.:=]/,H=a(t=>{const e=`Flag name "${t}"`;if(t.length===0)throw new Error(`${e} cannot be empty`);const s=t.match(Z);if(s)throw new Error(`${e} cannot contain "${s?.[0]}"`)},"validateFlagName"),m=a((t,e,s)=>{if(d(t,e))throw new Error(`Duplicate flags named "${e}"`);t[e]=s},"setFlag"),I=a(t=>{const e={};for(const s in t){if(!d(t,s))continue;H(s);const n=t[s],r=[[],...A(n),n];m(e,s,r);const i=k(s);if(s!==i&&m(e,i,r),"alias"in n&&typeof n.alias=="string"){const{alias:o}=n,l=`Flag alias "${o}" for flag "${s}"`;if(s.length===1)throw new Error(`${l} cannot be defined for a single-character flag`);if(o.length===0)throw new Error(`${l} cannot be empty`);if(o.length>1)throw new Error(`${l} must be a single character`);m(e,o,r)}}return e},"createRegistry"),M=a((t,e)=>{const s={};for(const n in t){if(!d(t,n))continue;const[r,,i,o]=e[n];if(r.length===0&&!F(o)&&"default"in o){let{default:l}=o;typeof l=="function"&&(l=l()),s[n]=l}else s[n]=i?r:r.pop()}return s},"finalizeFlags"),b="--",X=3,q=/^-{1,2}\w/,T=a(t=>{if(!q.test(t))return;const e=!t.startsWith(b);let s=t.slice(e?1:2),n,r=-1;for(const i of["=",":","."]){const o=s.indexOf(i);o!==-1&&(r===-1||o<r)&&(r=o)}return r!==-1&&(n=s.slice(r+1),s=s.slice(0,r)),[s,n,e]},"parseFlagArgv"),C=a((t,{onFlag:e,onArgument:s})=>{let n;const r=a((i,o)=>{if(typeof n!="function")return!0;n(i,o),n=void 0},"triggerValueCallback");for(let i=0;i<t.length;i+=1){const o=t[i];if(o===b){r();const u=t.slice(i+1);s?.(u,[i],!0);break}const l=T(o);if(l){if(r(),!e)continue;const[u,c,g]=l;if(g)for(let f=0;f<u.length;f+=1){r();const p=f===u.length-1;n=e(u[f],p?c:void 0,[i,f+1,p])}else n=e(u,c,[i])}else r(o,[i])&&s?.([o],[i])}r()},"argvIterator"),_=a((t,e)=>{for(let s=e.length-1;s>=0;s-=1){const[n,r,i]=e[s];if(r){const o=t[n];let l=o.slice(0,r);if(i||(l+=o.slice(r+1)),l!=="-"){t[n]=l;continue}}t.splice(n,1)}},"spliceFromArgv"),E=a((t,e)=>Object.assign(t,{[b]:e}),"createPositionalArgumentsFromParts"),x=a(t=>{const e=t.indexOf(b);if(e===-1)return E([...t],[]);const s=t.slice(0,e),n=t.slice(e+1);return E([...s,...n],n)},"createPositionalArguments"),J=a((t,e=process.argv.slice(2),{ignore:s,booleanNegation:n}={})=>{const r=[],i=I(t),o={},l=[];let u=[];return C(e,{onFlag(c,g,f){const p=f.length===X,h=(p||c.length>1)&&d(i,c);let y;if(!h&&n&&!p&&c.length>3&&c.startsWith("no-")){const w=c.slice(3);d(i,w)&&i[w][1]===Boolean&&(y=w)}if(!s?.(h||y?z:G,c,g)){if(h){const[w,P]=i[c],$=D(P,g),V=a((B,O)=>{r.push(f),O&&r.push(O),w.push(L(P,B||"",c))},"getFollowingValue");return $===void 0?V:V($)}if(y){i[y][0].push(!1),r.push(f);return}d(o,c)||(o[c]=[]),o[c].push(g===void 0?!0:g),r.push(f)}},onArgument:a((c,g,f)=>{s?.(K,e[g[0]])||(l.push(...c),f?(u=c,e.splice(g[0])):r.push(g))},"onArgument")}),_(e,r),{flags:M(t,i),unknownFlags:o,_:E(l,u)}},"typeFlag"),Q=a((t,e,s=process.argv.slice(2))=>{const n=new Set(t.split(",").map(u=>T(u)?.[0])),[r,i]=A(e),o=[],l=[];return C(s,{onFlag:a((u,c,g)=>{if(!n.has(u)||!i&&o.length>0)return;const f=D(r,c),p=a((N,h)=>{l.push(g),h&&l.push(h),o.push(L(r,N||"",u))},"getFollowingValue");return f===void 0?p:p(f)},"onFlag")}),_(s,l),i?o:o[0]},"getFlag");export{x as createPositionalArguments,k as flagNameToKebab,Q as getFlag,F as isStandardSchema,J as typeFlag}; | ||
| var K=Object.defineProperty;var g=(i,a)=>K(i,"name",{value:a,configurable:!0});import{c as O,a as L,s as b,b as P,f as R,A as T,h as N,n as k,d as B,p as V,e as W}from"./internal-DRerJ2G5.mjs";const z="known-flag",D="unknown-flag",S="argument",U=g((i,a=process.argv.slice(2),{ignore:p,booleanNegation:F}={})=>{const o=[],r=O(i),n={},u=[];let l=[];return L(a,{onFlag(s,t,e){const h=e.length===T,c=(h||s.length>1)&&N(r,s);let A;if(!c&&F&&!h&&s.length>3&&s.startsWith("no-")){const f=s.slice(3);N(r,f)&&r[f][1]===Boolean&&(A=f)}if(!p?.(c||A?z:D,s,t)){if(c){const[f,v]=r[s],y=k(v,t),_=g((E,G)=>{o.push(e),G&&o.push(G),f.push(B(v,E||"",s))},"getFollowingValue");return y===void 0?_:_(y)}if(A){r[A][0].push(!1),o.push(e);return}N(n,s)||(n[s]=[]),n[s].push(t===void 0?!0:t),o.push(e)}},onArgument:g((s,t,e)=>{p?.(S,a[t[0]])||(u.push(...s),e?(l=s,a.splice(t[0])):o.push(t))},"onArgument")}),b(a,o),{flags:R(i,r),unknownFlags:n,_:P(u,l)}},"typeFlag"),d=g((i,a,p=process.argv.slice(2))=>{const F=new Set(i.split(",").map(l=>V(l)?.[0])),[o,r]=W(a),n=[],u=[];return L(p,{onFlag:g((l,s,t)=>{if(!F.has(l)||!r&&n.length>0)return;const e=k(o,s),h=g((w,c)=>{u.push(t),c&&u.push(c),n.push(B(o,w||"",l))},"getFollowingValue");return e===void 0?h:h(e)},"onFlag")}),b(p,u),r?n:n[0]},"getFlag");export{d as getFlag,U as typeFlag}; |
+12
-3
| { | ||
| "name": "type-flag", | ||
| "version": "5.0.0-beta.9", | ||
| "version": "5.0.0-beta.10", | ||
| "description": "Typed command-line arguments parser", | ||
@@ -28,4 +28,10 @@ "keywords": [ | ||
| "exports": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.mjs" | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.mjs" | ||
| }, | ||
| "./internal": { | ||
| "types": "./dist/internal.d.ts", | ||
| "default": "./dist/internal.mjs" | ||
| } | ||
| }, | ||
@@ -35,2 +41,5 @@ "imports": { | ||
| "default": "./dist/index.mjs" | ||
| }, | ||
| "#type-flag/internal": { | ||
| "default": "./dist/internal.mjs" | ||
| } | ||
@@ -37,0 +46,0 @@ }, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
44375
2.91%10
66.67%306
3.73%4
33.33%1
Infinity%