zod-args-parser 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -13
- package/lib/commonjs/help.js +1 -1
- package/lib/commonjs/help.js.map +1 -1
- package/lib/commonjs/index.js +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/parser.js +1 -1
- package/lib/commonjs/parser.js.map +1 -1
- package/lib/module/help.js +1 -1
- package/lib/module/help.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/parser.js +1 -1
- package/lib/module/parser.js.map +1 -1
- package/lib/typescript/help.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +5 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/parser.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +41 -16
- package/lib/typescript/types.d.ts.map +1 -1
- package/package.json +17 -2
- package/src/help.ts +11 -10
- package/src/index.ts +16 -4
- package/src/parser.ts +24 -7
- package/src/types.ts +69 -23
- package/lib/commonjs/types.js +0 -1
- package/lib/commonjs/types.js.map +0 -1
- package/lib/module/types.js +0 -1
- package/lib/module/types.js.map +0 -1
package/src/parser.ts
CHANGED
|
@@ -50,12 +50,27 @@ export function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ..
|
|
|
50
50
|
help.printCliHelp(params, opt);
|
|
51
51
|
},
|
|
52
52
|
printSubcommandHelp(subcommandStr, opt) {
|
|
53
|
-
const subcommand = subcommandArr.find(c =>
|
|
53
|
+
const subcommand = subcommandArr.find(c => {
|
|
54
|
+
if (c.name === subcommandStr) return true;
|
|
55
|
+
if (!subcommandStr) return false;
|
|
56
|
+
if (!c.aliases?.length) return false;
|
|
57
|
+
return c.aliases.includes(subcommandStr);
|
|
58
|
+
});
|
|
54
59
|
if (!subcommand) return console.error(`Cannot print help for subcommand "${subcommandStr}" as it does not exist`);
|
|
55
60
|
help.printSubcommandHelp(subcommand, opt, cliOptions.cliName);
|
|
56
61
|
},
|
|
57
62
|
};
|
|
58
63
|
|
|
64
|
+
/** - Get current subcommand props */
|
|
65
|
+
const GetSubcommandProps = (cmd = results.subcommand) => {
|
|
66
|
+
return subcommandArr.find(c => {
|
|
67
|
+
if (c.name === cmd) return true;
|
|
68
|
+
if (!cmd) return false;
|
|
69
|
+
if (!c.aliases?.length) return false;
|
|
70
|
+
return c.aliases.includes(cmd);
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
|
|
59
74
|
const addRawArg = (optionName: string, rawArg: string) => {
|
|
60
75
|
if (!results._info) results._info = {};
|
|
61
76
|
if (!results._info[optionName]) results._info[optionName] = Object.create({});
|
|
@@ -88,7 +103,7 @@ export function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ..
|
|
|
88
103
|
results.subcommand = allSubcommands.has(arg) ? arg : undefined;
|
|
89
104
|
|
|
90
105
|
// add positional and arguments array
|
|
91
|
-
const subcommandProps =
|
|
106
|
+
const subcommandProps = GetSubcommandProps();
|
|
92
107
|
if (subcommandProps?.allowPositional) results.positional = [];
|
|
93
108
|
if (subcommandProps?.arguments?.length) results.arguments = [];
|
|
94
109
|
|
|
@@ -106,7 +121,7 @@ export function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ..
|
|
|
106
121
|
throw new Error(`Flag arguments cannot be assigned using "=": "${arg}"`);
|
|
107
122
|
}
|
|
108
123
|
|
|
109
|
-
const subcommandProps =
|
|
124
|
+
const subcommandProps = GetSubcommandProps();
|
|
110
125
|
if (!subcommandProps) throw new Error(`Unknown subcommand: "${results.subcommand}"`);
|
|
111
126
|
|
|
112
127
|
if (!subcommandProps.options) {
|
|
@@ -117,8 +132,6 @@ export function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ..
|
|
|
117
132
|
}
|
|
118
133
|
|
|
119
134
|
const optionName = transformArg(argument);
|
|
120
|
-
if (optionName in results) throw new Error(`Duplicate option: "${argument}"`);
|
|
121
|
-
|
|
122
135
|
const isNegative = argument.startsWith("--no-");
|
|
123
136
|
|
|
124
137
|
const option = subcommandProps.options.find(o => {
|
|
@@ -136,6 +149,10 @@ export function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ..
|
|
|
136
149
|
throw new Error(`Unknown option: "${argument}"`);
|
|
137
150
|
}
|
|
138
151
|
|
|
152
|
+
if (option.name in results) {
|
|
153
|
+
throw new Error(`Duplicated option: "${argument}"`);
|
|
154
|
+
}
|
|
155
|
+
|
|
139
156
|
const isTypeBoolean = isBooleanSchema(option.type);
|
|
140
157
|
const nextArg = argsv[i + 1];
|
|
141
158
|
|
|
@@ -173,7 +190,7 @@ export function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ..
|
|
|
173
190
|
continue;
|
|
174
191
|
}
|
|
175
192
|
|
|
176
|
-
const subcommandProps =
|
|
193
|
+
const subcommandProps = GetSubcommandProps();
|
|
177
194
|
|
|
178
195
|
// * arguments
|
|
179
196
|
if (subcommandProps?.arguments?.length) {
|
|
@@ -212,7 +229,7 @@ export function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ..
|
|
|
212
229
|
}
|
|
213
230
|
|
|
214
231
|
// check for missing options - set defaults - add _source
|
|
215
|
-
const subcommandProps =
|
|
232
|
+
const subcommandProps = GetSubcommandProps();
|
|
216
233
|
if (subcommandProps?.options?.length) {
|
|
217
234
|
for (const option of subcommandProps.options) {
|
|
218
235
|
if (option.name in results) {
|
package/src/types.ts
CHANGED
|
@@ -11,22 +11,15 @@ export type Subcommand = {
|
|
|
11
11
|
*/
|
|
12
12
|
name: string;
|
|
13
13
|
|
|
14
|
-
/**
|
|
15
|
-
* - The action is executed with the result of the parsed arguments.
|
|
16
|
-
* - To get typescript types use `setAction` instead of this.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* const helpCommand = createSubcommand({ name: "help", options: [...] });
|
|
20
|
-
* helpCommand.setAction(res => console.log(res));
|
|
21
|
-
*/
|
|
22
|
-
action?: (results?: any) => void;
|
|
23
|
-
|
|
24
14
|
/**
|
|
25
15
|
* - The description of the subcommand.
|
|
26
16
|
* - Used for generating the help message.
|
|
27
17
|
*/
|
|
28
18
|
description?: string;
|
|
29
19
|
|
|
20
|
+
/** - The usage message in the help message. */
|
|
21
|
+
usage?: string;
|
|
22
|
+
|
|
30
23
|
/** - Used for generating the help message. */
|
|
31
24
|
placeholder?: string;
|
|
32
25
|
|
|
@@ -64,18 +57,22 @@ export type Subcommand = {
|
|
|
64
57
|
* which arguments are optional.
|
|
65
58
|
*/
|
|
66
59
|
arguments?: [Argument, ...Argument[]];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* - The action is executed with the result of the parsed arguments.
|
|
63
|
+
* - To get typescript types use `setAction` instead of this.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* const helpCommand = createSubcommand({ name: "help", options: [...] });
|
|
67
|
+
* helpCommand.setAction(res => console.log(res));
|
|
68
|
+
*/
|
|
69
|
+
action?: (results?: any) => void;
|
|
67
70
|
};
|
|
68
71
|
|
|
69
72
|
export type Cli = Prettify<
|
|
70
73
|
Omit<Subcommand, "name"> & {
|
|
71
74
|
/** - The name of the CLI program. */
|
|
72
75
|
cliName: string;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* - The usage of the CLI program.
|
|
76
|
-
* - Used for generating the help message.
|
|
77
|
-
*/
|
|
78
|
-
usage?: string;
|
|
79
76
|
}
|
|
80
77
|
>;
|
|
81
78
|
|
|
@@ -243,7 +240,9 @@ export type ArgumentsArr2ArrType<T extends Argument[] | undefined> = T extends A
|
|
|
243
240
|
? { arguments: { [K in keyof T]: T[K] extends { type: z.ZodTypeAny } ? z.infer<T[K]["type"]> : never } }
|
|
244
241
|
: object;
|
|
245
242
|
|
|
246
|
-
export type Positional<S extends Partial<Subcommand>> = S["allowPositional"] extends true
|
|
243
|
+
export type Positional<S extends Partial<Subcommand>> = S["allowPositional"] extends true
|
|
244
|
+
? { positional: string[] }
|
|
245
|
+
: object;
|
|
247
246
|
|
|
248
247
|
export type Info<T extends Option[] | undefined> = T extends Option[]
|
|
249
248
|
? {
|
|
@@ -273,14 +272,61 @@ export type PrintMethods<N extends Subcommand["name"] | undefined> = {
|
|
|
273
272
|
printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>, options?: PrintHelpOpt) => void;
|
|
274
273
|
};
|
|
275
274
|
|
|
276
|
-
export type UnSafeParseResult<S extends Partial<Subcommand>[]> =
|
|
277
|
-
|
|
278
|
-
|
|
275
|
+
export type UnSafeParseResult<S extends Partial<Subcommand>[]> =
|
|
276
|
+
CheckDuplicatedSubcommands<S> extends infer E extends string
|
|
277
|
+
? E
|
|
278
|
+
: Prettify<ParseResult<S> & PrintMethods<S[number]["name"]>>;
|
|
279
279
|
|
|
280
|
-
export type SafeParseResult<S extends Partial<Subcommand>[]> =
|
|
281
|
-
|
|
282
|
-
|
|
280
|
+
export type SafeParseResult<S extends Partial<Subcommand>[]> =
|
|
281
|
+
CheckDuplicatedSubcommands<S> extends infer E extends string
|
|
282
|
+
? E
|
|
283
|
+
: Prettify<
|
|
284
|
+
({ success: false; error: Error } | { success: true; data: ParseResult<S> }) & PrintMethods<S[number]["name"]>
|
|
285
|
+
>;
|
|
283
286
|
|
|
284
287
|
export type ActionFn<T extends Subcommand | Cli> = {
|
|
285
288
|
setAction: (actions: (res: UnSafeParseResult<[T]>) => void) => void;
|
|
286
289
|
};
|
|
290
|
+
|
|
291
|
+
/** - Combine `name` and `aliases` to a `string[]` */
|
|
292
|
+
type MapNameAndAliases2StrArr<T extends { name?: string; aliases?: string[] }[]> = T extends [
|
|
293
|
+
infer First extends Subcommand,
|
|
294
|
+
...infer Rest,
|
|
295
|
+
]
|
|
296
|
+
? Rest extends { name?: string; aliases?: string[] }[]
|
|
297
|
+
? [First["name"], ...(First["aliases"] extends string[] ? First["aliases"] : []), ...MapNameAndAliases2StrArr<Rest>]
|
|
298
|
+
: [First["name"], ...(First["aliases"] extends string[] ? First["aliases"] : [])]
|
|
299
|
+
: [];
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* - Find duplicated items in an array and return it
|
|
303
|
+
* - Return `false` if not found
|
|
304
|
+
*/
|
|
305
|
+
type IsDuplicatesInArr<Input extends any[]> = Input extends [infer Item, ...infer Rest]
|
|
306
|
+
? Rest extends any[]
|
|
307
|
+
? Item extends Rest[number]
|
|
308
|
+
? Item
|
|
309
|
+
: IsDuplicatesInArr<Rest>
|
|
310
|
+
: false
|
|
311
|
+
: false;
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* - Check if there are duplicated options including aliases in `subcommand`
|
|
315
|
+
* - Return an error message if duplicated is found
|
|
316
|
+
* - Return `subcommand` if not found
|
|
317
|
+
*/
|
|
318
|
+
export type CheckDuplicatedOptions<T extends Subcommand | Cli> = T["options"] extends infer O extends Option[]
|
|
319
|
+
? IsDuplicatesInArr<MapNameAndAliases2StrArr<O>> extends infer D extends string
|
|
320
|
+
? `>>> Error: Duplicated Options \`${D}\` <<<`
|
|
321
|
+
: T
|
|
322
|
+
: T;
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* - Check for duplicated subcommands including aliases
|
|
326
|
+
* - Return an error message if duplicated is found
|
|
327
|
+
* - Return the `subcommand[]` if no error
|
|
328
|
+
*/
|
|
329
|
+
export type CheckDuplicatedSubcommands<T extends Partial<Subcommand>[]> =
|
|
330
|
+
IsDuplicatesInArr<MapNameAndAliases2StrArr<T>> extends infer D extends string
|
|
331
|
+
? `>>> Error: Duplicated Subcommand \`${D}\` <<<`
|
|
332
|
+
: T;
|
package/lib/commonjs/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../src","sources":["types.ts"],"sourcesContent":["import type { z } from \"zod\";\n\nexport type Subcommand = {\n /**\n * - The subcommand name, use `kebab-case`.\n * - Make sure to not duplicate commands and aliases.\n *\n * @example\n * name: \"test\";\n * name: \"run-app\";\n */\n name: string;\n\n /**\n * - The action is executed with the result of the parsed arguments.\n * - To get typescript types use `setAction` instead of this.\n *\n * @example\n * const helpCommand = createSubcommand({ name: \"help\", options: [...] });\n * helpCommand.setAction(res => console.log(res));\n */\n action?: (results?: any) => void;\n\n /**\n * - The description of the subcommand.\n * - Used for generating the help message.\n */\n description?: string;\n\n /** - Used for generating the help message. */\n placeholder?: string;\n\n /**\n * - Provide an example to show to the user.\n * - Used for generating the help message.\n */\n example?: string;\n\n /**\n * - The aliases of the subcommand.\n * - Make sure to not duplicate aliases and commands.\n */\n aliases?: string[];\n\n /**\n * - Allows positional arguments for this subcommand.\n * - Unlike `arguments`, which are strictly typed, positional arguments are untyped and represented as a string array of\n * variable length.\n * - When enabled and `arguments` are provided, `arguments` will be parsed first. Any remaining arguments will be\n * considered positional arguments and added to the `positional` property in the result.\n */\n allowPositional?: boolean;\n\n /**\n * - The options of the command.\n * - Those options are specific to this subcommand.\n */\n options?: [Option, ...Option[]];\n\n /**\n * - Specifies a list of strictly typed arguments.\n * - The order is important; for example, the first argument will be validated against the first specified type.\n * - It is recommended to not use optional arguments as the parser will fill the arguments by order and can't determine\n * which arguments are optional.\n */\n arguments?: [Argument, ...Argument[]];\n};\n\nexport type Cli = Prettify<\n Omit<Subcommand, \"name\"> & {\n /** - The name of the CLI program. */\n cliName: string;\n\n /**\n * - The usage of the CLI program.\n * - Used for generating the help message.\n */\n usage?: string;\n }\n>;\n\nexport type Option = {\n /**\n * - The name of the option, use `CamelCase`.\n * - For example: the syntax for the option `rootPath` is `--root-path`.\n */\n name: string;\n\n /**\n * - The will be used to validate the user input.\n *\n * @example\n * type: z.boolean().default(false);\n * type: z.coerce.number(); // will be coerced to number by Zod\n * type: z.preprocess(parseStringToArrFn, z.array(z.coerce.number())); // array of numbers\n *\n * @see https://zod.dev/?id=types\n */\n type: z.ZodTypeAny;\n\n /**\n * - The description of the option.\n * - Used for generating the help message.\n */\n description?: string;\n\n /** - Used for generating the help message. */\n placeholder?: string;\n\n /**\n * - The example of using the option.\n * - Used for generating the help message.\n */\n example?: string;\n\n /**\n * - The aliases of the option, use `CamelCase`.\n * - Here you can specify short names or flags.\n * - Make sure to not duplicate aliases.\n */\n aliases?: [string, ...string[]];\n};\n\nexport type Argument = {\n /** - The name of the argument. */\n name: string;\n\n /**\n * - The will be used to validate the user input.\n *\n * @example\n * type: z.boolean();\n * type: z.coerce.number(); // will be coerced to number by Zod\n * type: z.preprocess(ParseStringToArrFn, z.array(z.coerce.number())); // array of numbers\n *\n * @see https://zod.dev/?id=types\n */\n type: z.ZodTypeAny;\n\n /**\n * - The description of the argument.\n * - Used for generating the help message.\n */\n description?: string;\n\n /**\n * - The example of using the argument.\n * - Used for generating the help message.\n */\n example?: string;\n};\n\nexport type ColorFnType = (...text: unknown[]) => string;\n\nexport type PrintHelpOpt = {\n /**\n * - **Optional** `boolean`\n * - Whether to print colors or not.\n * - Default: `true`\n */\n colors?: boolean;\n\n /**\n * - **Optional** `object`\n * - The colors to use for the help message.\n */\n customColors?: {\n title?: ColorFnType;\n description?: ColorFnType;\n default?: ColorFnType;\n optional?: ColorFnType;\n exampleTitle?: ColorFnType;\n example?: ColorFnType;\n command?: ColorFnType;\n option?: ColorFnType;\n argument?: ColorFnType;\n placeholder?: ColorFnType;\n punctuation?: ColorFnType;\n };\n};\n\nexport type _Info = {\n /**\n * - The raw argument as it was passed in\n * - For options that have a default value and are not passed in, the raw argument will be `undefined`\n */\n rawArg?: string;\n /**\n * - The raw value of the argument as it was passed in\n * - It will be empty string for `boolean` options. E.g. `--help` or `-h`\n * - For options that have a default value and are not passed in, the raw value will be `undefined`\n */\n rawValue?: string;\n /**\n * - The source value of the argument:\n * - `cli`: The argument was passed in by the user\n * - `default`: The argument was not passed in and has a default value\n */\n source: \"cli\" | \"default\";\n};\n\n/**\n * - Infer the options type from a subcommand.\n *\n * @example\n * const subcommand = createSubcommand({ name: \"build\", options: [...] });\n * type OptionsType = InferOptionsType<typeof subcommand>;\n */\nexport type InferOptionsType<T extends Partial<Subcommand>> = T[\"options\"] extends infer U extends Option[]\n ? ToOptional<{ [K in U[number][\"name\"]]: z.infer<Extract<U[number], { name: K }>[\"type\"]> }>\n : undefined;\n\n/**\n * - Infer the arguments type from a subcommand.\n *\n * @example\n * const subcommand = createSubcommand({ name: \"build\", arguments: [...] });\n * type ArgumentsType = InferArgumentsType<typeof subcommand>;\n */\nexport type InferArgumentsType<T extends Partial<Subcommand>> = T[\"arguments\"] extends infer U extends Argument[]\n ? { [K in keyof U]: U[K] extends { type: z.ZodTypeAny } ? z.infer<U[K][\"type\"]> : never }\n : undefined;\n\n/** `{ some props } & { other props }` => `{ some props, other props }` */\nexport type Prettify<T> = { [K in keyof T]: T[K] } & {};\n\n/** Allow string type for literal union and get auto completion */\nexport type LiteralUnion<T extends string> = T | (string & {});\n\n/** Extract the undefined properties from an object */\ntype UndefinedProperties<T> = { [P in keyof T]-?: undefined extends T[P] ? P : never }[keyof T];\n\n/** Make undefined properties optional? */\ntype ToOptional<T> = Prettify<\n Partial<Pick<T, UndefinedProperties<T>>> & Pick<T, Exclude<keyof T, UndefinedProperties<T>>>\n>;\n\nexport type OptionsArr2RecordType<T extends Option[] | undefined> = T extends Option[]\n ? ToOptional<{ [K in T[number][\"name\"]]: z.infer<Extract<T[number], { name: K }>[\"type\"]> }>\n : object;\n\nexport type ArgumentsArr2ArrType<T extends Argument[] | undefined> = T extends Argument[]\n ? { arguments: { [K in keyof T]: T[K] extends { type: z.ZodTypeAny } ? z.infer<T[K][\"type\"]> : never } }\n : object;\n\nexport type Positional<S extends Partial<Subcommand>> = S[\"allowPositional\"] extends true ? { positional: string[] } : object;\n\nexport type Info<T extends Option[] | undefined> = T extends Option[]\n ? {\n _info: ToOptional<{\n [K in T[number][\"name\"]]: Extract<T[number], { name: K }> extends infer U extends Option\n ? undefined extends z.infer<U[\"type\"]>\n ? undefined | Prettify<_Info & U> // if optional add undefined\n : Prettify<_Info & U>\n : never;\n }>;\n }\n : object;\n\nexport type NoSubcommand = { name: undefined };\n\nexport type ParseResult<S extends Partial<Subcommand>[]> = {\n [K in keyof S]: Prettify<\n { subcommand: S[K][\"name\"] } & Positional<S[K]> &\n Info<S[K][\"options\"]> &\n OptionsArr2RecordType<S[K][\"options\"]> &\n ArgumentsArr2ArrType<S[K][\"arguments\"]>\n >;\n}[number];\n\nexport type PrintMethods<N extends Subcommand[\"name\"] | undefined> = {\n printCliHelp: (options?: PrintHelpOpt) => void;\n printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>, options?: PrintHelpOpt) => void;\n};\n\nexport type UnSafeParseResult<S extends Partial<Subcommand>[]> = Prettify<\n ParseResult<S> & PrintMethods<S[number][\"name\"]>\n>;\n\nexport type SafeParseResult<S extends Partial<Subcommand>[]> = Prettify<\n ({ success: false; error: Error } | { success: true; data: ParseResult<S> }) & PrintMethods<S[number][\"name\"]>\n>;\n\nexport type ActionFn<T extends Subcommand | Cli> = {\n setAction: (actions: (res: UnSafeParseResult<[T]>) => void) => void;\n};\n"],"mappings":"","ignoreList":[]}
|
package/lib/module/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export{};
|
package/lib/module/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../src","sources":["types.ts"],"sourcesContent":["import type { z } from \"zod\";\n\nexport type Subcommand = {\n /**\n * - The subcommand name, use `kebab-case`.\n * - Make sure to not duplicate commands and aliases.\n *\n * @example\n * name: \"test\";\n * name: \"run-app\";\n */\n name: string;\n\n /**\n * - The action is executed with the result of the parsed arguments.\n * - To get typescript types use `setAction` instead of this.\n *\n * @example\n * const helpCommand = createSubcommand({ name: \"help\", options: [...] });\n * helpCommand.setAction(res => console.log(res));\n */\n action?: (results?: any) => void;\n\n /**\n * - The description of the subcommand.\n * - Used for generating the help message.\n */\n description?: string;\n\n /** - Used for generating the help message. */\n placeholder?: string;\n\n /**\n * - Provide an example to show to the user.\n * - Used for generating the help message.\n */\n example?: string;\n\n /**\n * - The aliases of the subcommand.\n * - Make sure to not duplicate aliases and commands.\n */\n aliases?: string[];\n\n /**\n * - Allows positional arguments for this subcommand.\n * - Unlike `arguments`, which are strictly typed, positional arguments are untyped and represented as a string array of\n * variable length.\n * - When enabled and `arguments` are provided, `arguments` will be parsed first. Any remaining arguments will be\n * considered positional arguments and added to the `positional` property in the result.\n */\n allowPositional?: boolean;\n\n /**\n * - The options of the command.\n * - Those options are specific to this subcommand.\n */\n options?: [Option, ...Option[]];\n\n /**\n * - Specifies a list of strictly typed arguments.\n * - The order is important; for example, the first argument will be validated against the first specified type.\n * - It is recommended to not use optional arguments as the parser will fill the arguments by order and can't determine\n * which arguments are optional.\n */\n arguments?: [Argument, ...Argument[]];\n};\n\nexport type Cli = Prettify<\n Omit<Subcommand, \"name\"> & {\n /** - The name of the CLI program. */\n cliName: string;\n\n /**\n * - The usage of the CLI program.\n * - Used for generating the help message.\n */\n usage?: string;\n }\n>;\n\nexport type Option = {\n /**\n * - The name of the option, use `CamelCase`.\n * - For example: the syntax for the option `rootPath` is `--root-path`.\n */\n name: string;\n\n /**\n * - The will be used to validate the user input.\n *\n * @example\n * type: z.boolean().default(false);\n * type: z.coerce.number(); // will be coerced to number by Zod\n * type: z.preprocess(parseStringToArrFn, z.array(z.coerce.number())); // array of numbers\n *\n * @see https://zod.dev/?id=types\n */\n type: z.ZodTypeAny;\n\n /**\n * - The description of the option.\n * - Used for generating the help message.\n */\n description?: string;\n\n /** - Used for generating the help message. */\n placeholder?: string;\n\n /**\n * - The example of using the option.\n * - Used for generating the help message.\n */\n example?: string;\n\n /**\n * - The aliases of the option, use `CamelCase`.\n * - Here you can specify short names or flags.\n * - Make sure to not duplicate aliases.\n */\n aliases?: [string, ...string[]];\n};\n\nexport type Argument = {\n /** - The name of the argument. */\n name: string;\n\n /**\n * - The will be used to validate the user input.\n *\n * @example\n * type: z.boolean();\n * type: z.coerce.number(); // will be coerced to number by Zod\n * type: z.preprocess(ParseStringToArrFn, z.array(z.coerce.number())); // array of numbers\n *\n * @see https://zod.dev/?id=types\n */\n type: z.ZodTypeAny;\n\n /**\n * - The description of the argument.\n * - Used for generating the help message.\n */\n description?: string;\n\n /**\n * - The example of using the argument.\n * - Used for generating the help message.\n */\n example?: string;\n};\n\nexport type ColorFnType = (...text: unknown[]) => string;\n\nexport type PrintHelpOpt = {\n /**\n * - **Optional** `boolean`\n * - Whether to print colors or not.\n * - Default: `true`\n */\n colors?: boolean;\n\n /**\n * - **Optional** `object`\n * - The colors to use for the help message.\n */\n customColors?: {\n title?: ColorFnType;\n description?: ColorFnType;\n default?: ColorFnType;\n optional?: ColorFnType;\n exampleTitle?: ColorFnType;\n example?: ColorFnType;\n command?: ColorFnType;\n option?: ColorFnType;\n argument?: ColorFnType;\n placeholder?: ColorFnType;\n punctuation?: ColorFnType;\n };\n};\n\nexport type _Info = {\n /**\n * - The raw argument as it was passed in\n * - For options that have a default value and are not passed in, the raw argument will be `undefined`\n */\n rawArg?: string;\n /**\n * - The raw value of the argument as it was passed in\n * - It will be empty string for `boolean` options. E.g. `--help` or `-h`\n * - For options that have a default value and are not passed in, the raw value will be `undefined`\n */\n rawValue?: string;\n /**\n * - The source value of the argument:\n * - `cli`: The argument was passed in by the user\n * - `default`: The argument was not passed in and has a default value\n */\n source: \"cli\" | \"default\";\n};\n\n/**\n * - Infer the options type from a subcommand.\n *\n * @example\n * const subcommand = createSubcommand({ name: \"build\", options: [...] });\n * type OptionsType = InferOptionsType<typeof subcommand>;\n */\nexport type InferOptionsType<T extends Partial<Subcommand>> = T[\"options\"] extends infer U extends Option[]\n ? ToOptional<{ [K in U[number][\"name\"]]: z.infer<Extract<U[number], { name: K }>[\"type\"]> }>\n : undefined;\n\n/**\n * - Infer the arguments type from a subcommand.\n *\n * @example\n * const subcommand = createSubcommand({ name: \"build\", arguments: [...] });\n * type ArgumentsType = InferArgumentsType<typeof subcommand>;\n */\nexport type InferArgumentsType<T extends Partial<Subcommand>> = T[\"arguments\"] extends infer U extends Argument[]\n ? { [K in keyof U]: U[K] extends { type: z.ZodTypeAny } ? z.infer<U[K][\"type\"]> : never }\n : undefined;\n\n/** `{ some props } & { other props }` => `{ some props, other props }` */\nexport type Prettify<T> = { [K in keyof T]: T[K] } & {};\n\n/** Allow string type for literal union and get auto completion */\nexport type LiteralUnion<T extends string> = T | (string & {});\n\n/** Extract the undefined properties from an object */\ntype UndefinedProperties<T> = { [P in keyof T]-?: undefined extends T[P] ? P : never }[keyof T];\n\n/** Make undefined properties optional? */\ntype ToOptional<T> = Prettify<\n Partial<Pick<T, UndefinedProperties<T>>> & Pick<T, Exclude<keyof T, UndefinedProperties<T>>>\n>;\n\nexport type OptionsArr2RecordType<T extends Option[] | undefined> = T extends Option[]\n ? ToOptional<{ [K in T[number][\"name\"]]: z.infer<Extract<T[number], { name: K }>[\"type\"]> }>\n : object;\n\nexport type ArgumentsArr2ArrType<T extends Argument[] | undefined> = T extends Argument[]\n ? { arguments: { [K in keyof T]: T[K] extends { type: z.ZodTypeAny } ? z.infer<T[K][\"type\"]> : never } }\n : object;\n\nexport type Positional<S extends Partial<Subcommand>> = S[\"allowPositional\"] extends true ? { positional: string[] } : object;\n\nexport type Info<T extends Option[] | undefined> = T extends Option[]\n ? {\n _info: ToOptional<{\n [K in T[number][\"name\"]]: Extract<T[number], { name: K }> extends infer U extends Option\n ? undefined extends z.infer<U[\"type\"]>\n ? undefined | Prettify<_Info & U> // if optional add undefined\n : Prettify<_Info & U>\n : never;\n }>;\n }\n : object;\n\nexport type NoSubcommand = { name: undefined };\n\nexport type ParseResult<S extends Partial<Subcommand>[]> = {\n [K in keyof S]: Prettify<\n { subcommand: S[K][\"name\"] } & Positional<S[K]> &\n Info<S[K][\"options\"]> &\n OptionsArr2RecordType<S[K][\"options\"]> &\n ArgumentsArr2ArrType<S[K][\"arguments\"]>\n >;\n}[number];\n\nexport type PrintMethods<N extends Subcommand[\"name\"] | undefined> = {\n printCliHelp: (options?: PrintHelpOpt) => void;\n printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>, options?: PrintHelpOpt) => void;\n};\n\nexport type UnSafeParseResult<S extends Partial<Subcommand>[]> = Prettify<\n ParseResult<S> & PrintMethods<S[number][\"name\"]>\n>;\n\nexport type SafeParseResult<S extends Partial<Subcommand>[]> = Prettify<\n ({ success: false; error: Error } | { success: true; data: ParseResult<S> }) & PrintMethods<S[number][\"name\"]>\n>;\n\nexport type ActionFn<T extends Subcommand | Cli> = {\n setAction: (actions: (res: UnSafeParseResult<[T]>) => void) => void;\n};\n"],"mappings":"","ignoreList":[]}
|