zod-args-parser 1.0.16 → 1.2.0
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/README.md +598 -92
- package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.js +1 -1
- package/lib/commonjs/autocomplete-scripts/bash-autocomplete-script.js.map +1 -1
- package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.js +1 -1
- package/lib/commonjs/autocomplete-scripts/powershell-autocomplete-script.js.map +1 -1
- package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.js +1 -1
- package/lib/commonjs/autocomplete-scripts/zsh-autocomplete-script.js.map +1 -1
- package/lib/commonjs/help-message/format-arguments.js +1 -0
- package/lib/commonjs/help-message/format-arguments.js.map +1 -0
- package/lib/commonjs/help-message/format-cli.js +1 -0
- package/lib/commonjs/help-message/format-cli.js.map +1 -0
- package/lib/commonjs/help-message/format-options.js +1 -0
- package/lib/commonjs/help-message/format-options.js.map +1 -0
- package/lib/commonjs/help-message/format-subcommands.js +1 -0
- package/lib/commonjs/help-message/format-subcommands.js.map +1 -0
- package/lib/commonjs/help-message/styles.js +1 -0
- package/lib/commonjs/help-message/styles.js.map +1 -0
- package/lib/commonjs/index.js +1 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/markdown/generate-markdown.js.map +1 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js +1 -1
- package/lib/commonjs/metadata/get-arguments-metadata.js.map +1 -1
- package/lib/commonjs/metadata/get-cli-metadata.js +1 -1
- package/lib/commonjs/metadata/get-cli-metadata.js.map +1 -1
- package/lib/commonjs/metadata/get-options-metadata.js +1 -1
- package/lib/commonjs/metadata/get-options-metadata.js.map +1 -1
- package/lib/commonjs/metadata/get-subcommands-metadata.js.map +1 -1
- package/lib/commonjs/parser/parse/parse.js +1 -0
- package/lib/commonjs/parser/parse/parse.js.map +1 -0
- package/lib/commonjs/parser/parse/parser-helpers.js +1 -0
- package/lib/commonjs/parser/parse/parser-helpers.js.map +1 -0
- package/lib/commonjs/parser/safe-parse.js +1 -1
- package/lib/commonjs/parser/safe-parse.js.map +1 -1
- package/lib/commonjs/parser/unsafe-parse.js +1 -0
- package/lib/commonjs/parser/unsafe-parse.js.map +1 -0
- package/lib/commonjs/parser/validate/validate-type.js +1 -0
- package/lib/commonjs/parser/validate/validate-type.js.map +1 -0
- package/lib/commonjs/parser/validate/validate.js +1 -0
- package/lib/commonjs/parser/validate/validate.js.map +1 -0
- package/lib/commonjs/utils.js +1 -1
- package/lib/commonjs/utils.js.map +1 -1
- package/lib/commonjs/zod-utils.js +1 -0
- package/lib/commonjs/zod-utils.js.map +1 -0
- package/lib/module/autocomplete-scripts/bash-autocomplete-script.js +1 -1
- package/lib/module/autocomplete-scripts/bash-autocomplete-script.js.map +1 -1
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js +1 -1
- package/lib/module/autocomplete-scripts/powershell-autocomplete-script.js.map +1 -1
- package/lib/module/autocomplete-scripts/zsh-autocomplete-script.js +1 -1
- package/lib/module/autocomplete-scripts/zsh-autocomplete-script.js.map +1 -1
- package/lib/module/help-message/format-arguments.js +1 -0
- package/lib/module/help-message/format-arguments.js.map +1 -0
- package/lib/module/help-message/format-cli.js +1 -0
- package/lib/module/help-message/format-cli.js.map +1 -0
- package/lib/module/help-message/format-options.js +1 -0
- package/lib/module/help-message/format-options.js.map +1 -0
- package/lib/module/help-message/{print-subcommands.js → format-subcommands.js} +1 -1
- package/lib/module/help-message/format-subcommands.js.map +1 -0
- package/lib/module/help-message/styles.js +1 -0
- package/lib/module/help-message/styles.js.map +1 -0
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/markdown/generate-markdown.js.map +1 -1
- package/lib/module/metadata/get-arguments-metadata.js +1 -1
- package/lib/module/metadata/get-arguments-metadata.js.map +1 -1
- package/lib/module/metadata/get-cli-metadata.js +1 -1
- package/lib/module/metadata/get-cli-metadata.js.map +1 -1
- package/lib/module/metadata/get-options-metadata.js +1 -1
- package/lib/module/metadata/get-options-metadata.js.map +1 -1
- package/lib/module/metadata/get-subcommands-metadata.js.map +1 -1
- package/lib/module/parser/parse/parse.js +1 -0
- package/lib/module/parser/parse/parse.js.map +1 -0
- package/lib/module/parser/parse/parser-helpers.js +1 -0
- package/lib/module/parser/parse/parser-helpers.js.map +1 -0
- package/lib/module/parser/safe-parse.js +1 -1
- package/lib/module/parser/safe-parse.js.map +1 -1
- package/lib/module/parser/unsafe-parse.js +1 -0
- package/lib/module/parser/unsafe-parse.js.map +1 -0
- package/lib/module/parser/validate/validate-type.js +1 -0
- package/lib/module/parser/validate/validate-type.js.map +1 -0
- package/lib/module/parser/validate/validate.js +1 -0
- package/lib/module/parser/validate/validate.js.map +1 -0
- package/lib/module/utils.js +1 -1
- package/lib/module/utils.js.map +1 -1
- package/lib/module/zod-utils.js +1 -0
- package/lib/module/zod-utils.js.map +1 -0
- package/lib/typescript/help-message/format-arguments.d.ts +4 -0
- package/lib/typescript/help-message/format-arguments.d.ts.map +1 -0
- package/lib/typescript/help-message/format-cli.d.ts +6 -0
- package/lib/typescript/help-message/format-cli.d.ts.map +1 -0
- package/lib/typescript/help-message/format-options.d.ts +4 -0
- package/lib/typescript/help-message/format-options.d.ts.map +1 -0
- package/lib/typescript/help-message/format-subcommands.d.ts +4 -0
- package/lib/typescript/help-message/format-subcommands.d.ts.map +1 -0
- package/lib/typescript/help-message/styles.d.ts +108 -0
- package/lib/typescript/help-message/styles.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +18 -29
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/markdown/generate-markdown.d.ts.map +1 -1
- package/lib/typescript/metadata/get-arguments-metadata.d.ts +2 -1
- package/lib/typescript/metadata/get-arguments-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/get-cli-metadata.d.ts +3 -2
- package/lib/typescript/metadata/get-cli-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/get-options-metadata.d.ts +2 -1
- package/lib/typescript/metadata/get-options-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/get-subcommands-metadata.d.ts +2 -1
- package/lib/typescript/metadata/get-subcommands-metadata.d.ts.map +1 -1
- package/lib/typescript/metadata/metadata-types.d.ts +80 -0
- package/lib/typescript/metadata/metadata-types.d.ts.map +1 -0
- package/lib/typescript/parser/parse/parse-types.d.ts +85 -0
- package/lib/typescript/parser/parse/parse-types.d.ts.map +1 -0
- package/lib/typescript/parser/parse/parse.d.ts +4 -0
- package/lib/typescript/parser/parse/parse.d.ts.map +1 -0
- package/lib/typescript/parser/parse/parser-helpers.d.ts +40 -0
- package/lib/typescript/parser/parse/parser-helpers.d.ts.map +1 -0
- package/lib/typescript/parser/safe-parse.d.ts +1 -0
- package/lib/typescript/parser/safe-parse.d.ts.map +1 -1
- package/lib/typescript/parser/unsafe-parse.d.ts +4 -0
- package/lib/typescript/parser/unsafe-parse.d.ts.map +1 -0
- package/lib/typescript/parser/validate/validate-type.d.ts +22 -0
- package/lib/typescript/parser/validate/validate-type.d.ts.map +1 -0
- package/lib/typescript/parser/validate/validate.d.ts +11 -0
- package/lib/typescript/parser/validate/validate.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +76 -186
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/utils.d.ts +20 -23
- package/lib/typescript/utils.d.ts.map +1 -1
- package/lib/typescript/zod-utils.d.ts +27 -0
- package/lib/typescript/zod-utils.d.ts.map +1 -0
- package/package.json +20 -19
- package/src/autocomplete-scripts/bash-autocomplete-script.ts +1 -1
- package/src/autocomplete-scripts/powershell-autocomplete-script.ts +1 -1
- package/src/autocomplete-scripts/zsh-autocomplete-script.ts +1 -1
- package/src/help-message/format-arguments.ts +38 -0
- package/src/help-message/{print-help-message.ts → format-cli.ts} +43 -48
- package/src/help-message/{print-options.ts → format-options.ts} +16 -14
- package/src/help-message/format-subcommands.ts +37 -0
- package/src/help-message/styles.ts +120 -0
- package/src/index.ts +50 -44
- package/src/markdown/generate-markdown.ts +2 -1
- package/src/metadata/get-arguments-metadata.ts +6 -5
- package/src/metadata/get-cli-metadata.ts +4 -4
- package/src/metadata/get-options-metadata.ts +7 -5
- package/src/metadata/get-subcommands-metadata.ts +2 -1
- package/src/metadata/metadata-types.ts +114 -0
- package/src/parser/parse/parse-types.ts +89 -0
- package/src/parser/parse/parse.ts +227 -0
- package/src/parser/parse/parser-helpers.ts +167 -0
- package/src/parser/safe-parse.ts +60 -8
- package/src/parser/unsafe-parse.ts +98 -0
- package/src/parser/validate/validate-type.ts +21 -0
- package/src/parser/validate/validate.ts +66 -0
- package/src/types.ts +99 -233
- package/src/utils.ts +31 -96
- package/src/zod-utils.ts +199 -0
- package/lib/commonjs/help-message/colors.js +0 -1
- package/lib/commonjs/help-message/colors.js.map +0 -1
- package/lib/commonjs/help-message/print-arguments.js +0 -1
- package/lib/commonjs/help-message/print-arguments.js.map +0 -1
- package/lib/commonjs/help-message/print-help-message.js +0 -1
- package/lib/commonjs/help-message/print-help-message.js.map +0 -1
- package/lib/commonjs/help-message/print-options.js +0 -1
- package/lib/commonjs/help-message/print-options.js.map +0 -1
- package/lib/commonjs/help-message/print-subcommands.js +0 -1
- package/lib/commonjs/help-message/print-subcommands.js.map +0 -1
- package/lib/commonjs/help-message/utils.js +0 -1
- package/lib/commonjs/help-message/utils.js.map +0 -1
- package/lib/commonjs/parser/parse.js +0 -1
- package/lib/commonjs/parser/parse.js.map +0 -1
- package/lib/module/help-message/colors.js +0 -1
- package/lib/module/help-message/colors.js.map +0 -1
- package/lib/module/help-message/print-arguments.js +0 -1
- package/lib/module/help-message/print-arguments.js.map +0 -1
- package/lib/module/help-message/print-help-message.js +0 -1
- package/lib/module/help-message/print-help-message.js.map +0 -1
- package/lib/module/help-message/print-options.js +0 -1
- package/lib/module/help-message/print-options.js.map +0 -1
- package/lib/module/help-message/print-subcommands.js.map +0 -1
- package/lib/module/help-message/utils.js +0 -1
- package/lib/module/help-message/utils.js.map +0 -1
- package/lib/module/parser/parse.js +0 -1
- package/lib/module/parser/parse.js.map +0 -1
- package/lib/typescript/help-message/colors.d.ts +0 -17
- package/lib/typescript/help-message/colors.d.ts.map +0 -1
- package/lib/typescript/help-message/print-arguments.d.ts +0 -4
- package/lib/typescript/help-message/print-arguments.d.ts.map +0 -1
- package/lib/typescript/help-message/print-help-message.d.ts +0 -4
- package/lib/typescript/help-message/print-help-message.d.ts.map +0 -1
- package/lib/typescript/help-message/print-options.d.ts +0 -4
- package/lib/typescript/help-message/print-options.d.ts.map +0 -1
- package/lib/typescript/help-message/print-subcommands.d.ts +0 -4
- package/lib/typescript/help-message/print-subcommands.d.ts.map +0 -1
- package/lib/typescript/help-message/utils.d.ts +0 -13
- package/lib/typescript/help-message/utils.d.ts.map +0 -1
- package/lib/typescript/parser/parse.d.ts +0 -3
- package/lib/typescript/parser/parse.d.ts.map +0 -1
- package/src/help-message/colors.ts +0 -24
- package/src/help-message/print-arguments.ts +0 -35
- package/src/help-message/print-subcommands.ts +0 -28
- package/src/help-message/utils.ts +0 -31
- package/src/parser/parse.ts +0 -270
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { Schema } from "../types.js";
|
|
2
|
+
|
|
3
|
+
export interface CliMetadata {
|
|
4
|
+
/** The name of the cli program. */
|
|
5
|
+
readonly name: string;
|
|
6
|
+
|
|
7
|
+
/** - The description of the cli program. Empty string if not provided */
|
|
8
|
+
readonly description: string;
|
|
9
|
+
|
|
10
|
+
/** - The usage of the cli program. Empty string if not provided */
|
|
11
|
+
readonly usage: string;
|
|
12
|
+
|
|
13
|
+
/** - The example of the cli program. Empty string if not provided */
|
|
14
|
+
readonly example: string;
|
|
15
|
+
|
|
16
|
+
/** - Whether the cli program allows positional arguments. */
|
|
17
|
+
readonly allowPositional: boolean;
|
|
18
|
+
|
|
19
|
+
/** - The options of the cli program. Empty array if not provided */
|
|
20
|
+
readonly options: OptionMetadata[];
|
|
21
|
+
|
|
22
|
+
/** - The arguments of the cli program. Empty array if not provided */
|
|
23
|
+
readonly arguments: ArgumentMetadata[];
|
|
24
|
+
|
|
25
|
+
/** - The subcommands of the cli program. Empty array if not provided */
|
|
26
|
+
readonly subcommands: SubcommandMetadata[];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SubcommandMetadata {
|
|
30
|
+
/** The subcommand name. */
|
|
31
|
+
readonly name: string;
|
|
32
|
+
|
|
33
|
+
/** - The aliases of the subcommand. Empty array if not provided */
|
|
34
|
+
readonly aliases: string[];
|
|
35
|
+
|
|
36
|
+
/** - The description of the subcommand. Empty string if not provided */
|
|
37
|
+
readonly description: string;
|
|
38
|
+
|
|
39
|
+
/** - The placeholder of the subcommand. Empty string if not provided */
|
|
40
|
+
readonly placeholder: string;
|
|
41
|
+
|
|
42
|
+
/** - The usage of the subcommand. Empty string if not provided */
|
|
43
|
+
readonly usage: string;
|
|
44
|
+
|
|
45
|
+
/** - The example of the subcommand. Empty string if not provided */
|
|
46
|
+
readonly example: string;
|
|
47
|
+
|
|
48
|
+
/** - Whether the subcommand allows positional arguments. */
|
|
49
|
+
readonly allowPositional: boolean;
|
|
50
|
+
|
|
51
|
+
/** - The options of the subcommand. Empty array if not provided */
|
|
52
|
+
readonly options: OptionMetadata[];
|
|
53
|
+
|
|
54
|
+
/** - The arguments of the subcommand. Empty array if not provided */
|
|
55
|
+
readonly arguments: ArgumentMetadata[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface OptionMetadata {
|
|
59
|
+
/** The option name in camelCase. E.g. `optionName` */
|
|
60
|
+
readonly name: string;
|
|
61
|
+
|
|
62
|
+
/** The option name in kebab-case. E.g. `--option-name` */
|
|
63
|
+
readonly nameAsArg: string;
|
|
64
|
+
|
|
65
|
+
/** - The aliases of the option in camelCase. Empty array if not provided. E.g. `[aliasName, ...]` */
|
|
66
|
+
readonly aliases: string[];
|
|
67
|
+
|
|
68
|
+
/** - The aliases of the option in kebab-case. Empty array if not provided. E.g. `[--alias-name, ...]` */
|
|
69
|
+
readonly aliasesAsArgs: string[];
|
|
70
|
+
|
|
71
|
+
/** - The description of the option. Empty string if not provided */
|
|
72
|
+
readonly description: string;
|
|
73
|
+
|
|
74
|
+
/** - The placeholder of the option. Empty string if not provided */
|
|
75
|
+
readonly placeholder: string;
|
|
76
|
+
|
|
77
|
+
/** - The example of the option. Empty string if not provided */
|
|
78
|
+
readonly example: string;
|
|
79
|
+
|
|
80
|
+
/** - The default value of the option. */
|
|
81
|
+
readonly defaultValue: unknown;
|
|
82
|
+
|
|
83
|
+
/** - The default value of the option as string. */
|
|
84
|
+
readonly defaultValueAsString: string;
|
|
85
|
+
|
|
86
|
+
/** - Whether the option is optional. */
|
|
87
|
+
readonly optional: boolean;
|
|
88
|
+
|
|
89
|
+
/** - The zod type of the option. */
|
|
90
|
+
readonly type: Schema;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export interface ArgumentMetadata {
|
|
94
|
+
/** The argument name. */
|
|
95
|
+
readonly name: string;
|
|
96
|
+
|
|
97
|
+
/** - The description of the argument. Empty string if not provided */
|
|
98
|
+
readonly description: string;
|
|
99
|
+
|
|
100
|
+
/** - The example of the argument. Empty string if not provided */
|
|
101
|
+
readonly example: string;
|
|
102
|
+
|
|
103
|
+
/** - The default value of the argument. */
|
|
104
|
+
readonly defaultValue: unknown;
|
|
105
|
+
|
|
106
|
+
/** - The default value of the argument as string. */
|
|
107
|
+
readonly defaultValueAsString: string;
|
|
108
|
+
|
|
109
|
+
/** - Whether the argument is optional. */
|
|
110
|
+
readonly optional: boolean;
|
|
111
|
+
|
|
112
|
+
/** - The zod type of the argument. */
|
|
113
|
+
readonly type: Schema;
|
|
114
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import type { Argument, Option, Prettify, Schema, Subcommand } from "../../types.js";
|
|
2
|
+
|
|
3
|
+
type ParsedOption<S extends Schema = Schema, N extends string = string> =
|
|
4
|
+
| {
|
|
5
|
+
/** The name of the option as provided by the user. */
|
|
6
|
+
name: N;
|
|
7
|
+
/** The CLI flag as provided by the user (e.g. `--foo` or `-f`). */
|
|
8
|
+
flag: string;
|
|
9
|
+
/** The schema that validates this option. */
|
|
10
|
+
schema: S;
|
|
11
|
+
/** The raw string value supplied for this option from the CLI. */
|
|
12
|
+
rawValue: string;
|
|
13
|
+
/**
|
|
14
|
+
* The source of the option:
|
|
15
|
+
*
|
|
16
|
+
* - `cli`: provided explicitly in the CLI
|
|
17
|
+
* - `default`: not provided, and the schema has a default.
|
|
18
|
+
*/
|
|
19
|
+
source: "cli";
|
|
20
|
+
}
|
|
21
|
+
| {
|
|
22
|
+
/** The name of the option as provided by the user. */
|
|
23
|
+
name: N;
|
|
24
|
+
/** Undefined when the source is `default`. */
|
|
25
|
+
flag?: never;
|
|
26
|
+
/** The schema that validates this option. */
|
|
27
|
+
schema: S;
|
|
28
|
+
/** Undefined when the source is `default`. */
|
|
29
|
+
rawValue?: string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* The source of the option:
|
|
32
|
+
*
|
|
33
|
+
* - `cli`: provided explicitly in the CLI
|
|
34
|
+
* - `default`: not provided, and the schema has a default.
|
|
35
|
+
*/
|
|
36
|
+
source: "default";
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type ParsedArgument<S extends Schema = Schema> =
|
|
40
|
+
| {
|
|
41
|
+
/** The raw string value provided directly from the CLI. */
|
|
42
|
+
rawValue: string;
|
|
43
|
+
/** The schema that validates this argument. */
|
|
44
|
+
schema: S;
|
|
45
|
+
/**
|
|
46
|
+
* The source of the option:
|
|
47
|
+
*
|
|
48
|
+
* - `cli`: provided explicitly in the CLI
|
|
49
|
+
* - `default`: not provided, and the schema has a default.
|
|
50
|
+
*/
|
|
51
|
+
source: "cli";
|
|
52
|
+
}
|
|
53
|
+
| {
|
|
54
|
+
/** Undefined when the source is `default`. */
|
|
55
|
+
rawValue?: never;
|
|
56
|
+
/** The schema that validates this argument. */
|
|
57
|
+
schema: S;
|
|
58
|
+
/**
|
|
59
|
+
* The source of the option:
|
|
60
|
+
*
|
|
61
|
+
* - `cli`: provided explicitly in the CLI
|
|
62
|
+
* - `default`: not provided, and the schema has a default.
|
|
63
|
+
*/
|
|
64
|
+
source: "default";
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type OptionsArr2Record<T extends Option[] | undefined> = T extends Option[]
|
|
68
|
+
? { [K in T[number]["name"]]: ParsedOption<Extract<T[number], { name: K }>["type"], K> }
|
|
69
|
+
: Record<never, never>;
|
|
70
|
+
|
|
71
|
+
type ArgumentsArr2ArrType<T extends Argument[] | undefined> = T extends Argument[]
|
|
72
|
+
? { [K in keyof T]: ParsedArgument<T[K] extends { type: Schema } ? T[K]["type"] : never> }
|
|
73
|
+
: never;
|
|
74
|
+
|
|
75
|
+
export type ParseResult<S extends Partial<Subcommand>[]> = {
|
|
76
|
+
[K in keyof S]: Prettify<{
|
|
77
|
+
subcommand: S[K]["name"] extends string ? S[K]["name"] : undefined;
|
|
78
|
+
positional: S[K]["allowPositional"] extends true ? string[] : never;
|
|
79
|
+
options: OptionsArr2Record<S[K]["options"]>;
|
|
80
|
+
arguments: ArgumentsArr2ArrType<S[K]["arguments"]>;
|
|
81
|
+
}>;
|
|
82
|
+
}[number];
|
|
83
|
+
|
|
84
|
+
export type ParseCtx = {
|
|
85
|
+
subcommand: string | undefined;
|
|
86
|
+
options: Record<string, ParsedOption>;
|
|
87
|
+
arguments?: ParsedArgument[];
|
|
88
|
+
positional?: string[];
|
|
89
|
+
};
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { generateOrdinalSuffix } from "../../utils.js";
|
|
2
|
+
import { isBooleanSchema, isOptionalSchema, schemaDefaultValue } from "../../zod-utils.js";
|
|
3
|
+
import {
|
|
4
|
+
decoupleFlags,
|
|
5
|
+
findOption,
|
|
6
|
+
findSubcommand,
|
|
7
|
+
isFlagArg,
|
|
8
|
+
isOptionArg,
|
|
9
|
+
transformOptionToArg,
|
|
10
|
+
} from "./parser-helpers.js";
|
|
11
|
+
|
|
12
|
+
import type { Cli, Subcommand } from "../../types.js";
|
|
13
|
+
import type { ParseCtx } from "./parse-types.js";
|
|
14
|
+
|
|
15
|
+
export function parse(argv: string[], ...params: [Cli, ...Subcommand[]]) {
|
|
16
|
+
const subcommandArr = params as Subcommand[];
|
|
17
|
+
const allSubcommands = new Set<string>(subcommandArr.flatMap(c => [c.name, ...(c.aliases || [])]));
|
|
18
|
+
|
|
19
|
+
argv = decoupleFlags(argv); // decouple flags E.g. `-rf` -> `-r, -f`
|
|
20
|
+
|
|
21
|
+
const results: ParseCtx = {
|
|
22
|
+
subcommand: undefined,
|
|
23
|
+
options: {},
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/** - Get current subcommand object */
|
|
27
|
+
const getSubcommandObj = () => findSubcommand(results.subcommand, subcommandArr);
|
|
28
|
+
|
|
29
|
+
for (let i = 0; i < argv.length; i++) {
|
|
30
|
+
const arg = argv[i];
|
|
31
|
+
|
|
32
|
+
// * Subcommand check
|
|
33
|
+
if (i === 0) {
|
|
34
|
+
results.subcommand = allSubcommands.has(arg) ? arg : undefined;
|
|
35
|
+
|
|
36
|
+
// add positional and arguments arrays
|
|
37
|
+
const subcommandObj = getSubcommandObj();
|
|
38
|
+
if (subcommandObj && subcommandObj.allowPositional) {
|
|
39
|
+
results.positional = [];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (subcommandObj && subcommandObj.arguments?.length) {
|
|
43
|
+
results.arguments = [];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// First argument is a subcommand. Skip to the next argument
|
|
47
|
+
if (results.subcommand) continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// * Option check
|
|
51
|
+
|
|
52
|
+
// Check for `--option=value` or `--option value`
|
|
53
|
+
const argAndValue = arg.split("=").filter(Boolean);
|
|
54
|
+
const argWithEquals = arg.includes("=");
|
|
55
|
+
const argument = argAndValue[0];
|
|
56
|
+
const argValue: string | undefined = argAndValue[1];
|
|
57
|
+
|
|
58
|
+
if (isOptionArg(argument)) {
|
|
59
|
+
if (isFlagArg(argument) && argWithEquals) {
|
|
60
|
+
throw new Error(`Flag arguments cannot be assigned using "=": "${arg}"`, { cause: "zod-args-parser" });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const subcommandObj = getSubcommandObj();
|
|
64
|
+
if (!subcommandObj) {
|
|
65
|
+
throw new Error(`Unknown subcommand: "${results.subcommand}"`, { cause: "zod-args-parser" });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!subcommandObj.options) {
|
|
69
|
+
if (!results.subcommand) {
|
|
70
|
+
throw new Error(`Error: options are not allowed here: "${argument}"`, { cause: "zod-args-parser" });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
throw new Error(`Error: subcommand "${results.subcommand}" does not allow options: "${argument}"`, {
|
|
74
|
+
cause: "zod-args-parser",
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const option = findOption(argument, subcommandObj.options);
|
|
79
|
+
if (!option) {
|
|
80
|
+
throw new Error(`Unknown option: "${argument}"`, { cause: "zod-args-parser" });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (option.name in results.options) {
|
|
84
|
+
throw new Error(`Duplicated option: "${argument}"`, { cause: "zod-args-parser" });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const isTypeBoolean = isBooleanSchema(option.type);
|
|
88
|
+
const nextArg = argv[i + 1];
|
|
89
|
+
|
|
90
|
+
let optionValue: string | boolean = argWithEquals ? argValue : nextArg;
|
|
91
|
+
|
|
92
|
+
// infer value for boolean options
|
|
93
|
+
if (isTypeBoolean && !argWithEquals) {
|
|
94
|
+
optionValue = "true";
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (typeof optionValue === "undefined") {
|
|
98
|
+
throw new Error(`Expected a value for "${argument}" but got nothing`, { cause: "zod-args-parser" });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!argWithEquals && isOptionArg(optionValue)) {
|
|
102
|
+
throw new Error(`Expected a value for "${argument}" but got an argument "${nextArg}"`, {
|
|
103
|
+
cause: "zod-args-parser",
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
results.options[option.name] = {
|
|
108
|
+
name: option.name,
|
|
109
|
+
schema: option.type,
|
|
110
|
+
flag: argument,
|
|
111
|
+
rawValue: optionValue.toString(),
|
|
112
|
+
source: "cli",
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Skip to the next argument if it is the current option’s value.
|
|
116
|
+
if (!argWithEquals && !isTypeBoolean) {
|
|
117
|
+
i++;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const subcommandObj = getSubcommandObj();
|
|
124
|
+
|
|
125
|
+
// * Arguments check
|
|
126
|
+
if (subcommandObj?.arguments?.length) {
|
|
127
|
+
if (!results.arguments) {
|
|
128
|
+
results.arguments = [];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const currentArgCount = results.arguments.length;
|
|
132
|
+
|
|
133
|
+
// Any extra arguments are possibly positional
|
|
134
|
+
if (currentArgCount < subcommandObj.arguments.length) {
|
|
135
|
+
const argType = subcommandObj.arguments[currentArgCount].type;
|
|
136
|
+
results.arguments.push({
|
|
137
|
+
schema: argType,
|
|
138
|
+
rawValue: arg,
|
|
139
|
+
source: "cli",
|
|
140
|
+
});
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// * Positional check
|
|
146
|
+
if (subcommandObj?.allowPositional) {
|
|
147
|
+
if (!results.positional) {
|
|
148
|
+
results.positional = [];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
results.positional.push(arg);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// * Unexpected
|
|
156
|
+
if (!results.subcommand) {
|
|
157
|
+
throw new Error(`Unexpected argument "${arg}": positional arguments are not allowed here`, {
|
|
158
|
+
cause: "zod-args-parser",
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
throw new Error(
|
|
163
|
+
`Unexpected argument "${arg}": positional arguments are not allowed for subcommand "${results.subcommand}"`,
|
|
164
|
+
{ cause: "zod-args-parser" },
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// * Check for missing options - set defaults - add `source`
|
|
169
|
+
const subcommandObj = getSubcommandObj();
|
|
170
|
+
if (!subcommandObj) {
|
|
171
|
+
throw new Error(`Unknown subcommand: "${results.subcommand}"`, { cause: "zod-args-parser" });
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Options
|
|
175
|
+
if (subcommandObj.options?.length) {
|
|
176
|
+
for (const option of subcommandObj.options) {
|
|
177
|
+
// option already exists
|
|
178
|
+
if (option.name in results.options) continue;
|
|
179
|
+
|
|
180
|
+
const optional = isOptionalSchema(option.type);
|
|
181
|
+
const defaultValue = schemaDefaultValue(option.type);
|
|
182
|
+
|
|
183
|
+
if (optional) {
|
|
184
|
+
if (typeof defaultValue === "undefined") {
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
results.options[option.name] = { name: option.name, schema: option.type, source: "default" };
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
throw new Error(`Missing required option: ${transformOptionToArg(option.name)}`, { cause: "zod-args-parser" });
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Arguments
|
|
197
|
+
if (subcommandObj.arguments?.length) {
|
|
198
|
+
const currentArgCount = results.arguments?.length ?? 0;
|
|
199
|
+
const subcommandArgCount = subcommandObj.arguments.length;
|
|
200
|
+
|
|
201
|
+
// missing arguments
|
|
202
|
+
if (currentArgCount < subcommandArgCount) {
|
|
203
|
+
for (let i = currentArgCount; i < subcommandArgCount; i++) {
|
|
204
|
+
const argumentType = subcommandObj.arguments[i].type;
|
|
205
|
+
const optional = isOptionalSchema(argumentType);
|
|
206
|
+
const defaultValue = schemaDefaultValue(argumentType);
|
|
207
|
+
|
|
208
|
+
if (optional) {
|
|
209
|
+
if (typeof defaultValue === "undefined") {
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (!results.arguments) results.arguments = [];
|
|
214
|
+
|
|
215
|
+
results.arguments.push({ schema: argumentType, source: "default" });
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
throw new Error(`the ${generateOrdinalSuffix(i)} argument is required: "${subcommandObj.arguments[i].name}"`, {
|
|
220
|
+
cause: "zod-args-parser",
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return results;
|
|
227
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type { Option, Subcommand } from "../../types.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Retrieves a subcommand object from an array of subcommands by matching the provided subcommand name against the
|
|
5
|
+
* subcommand's name or its aliases.
|
|
6
|
+
*
|
|
7
|
+
* @param subCmdName - The name or alias of the subcommand to search for.
|
|
8
|
+
* @param subcommandArr - An array of `Subcommand` objects to search within.
|
|
9
|
+
* @returns The matching `Subcommand` object if found; otherwise, `undefined`.
|
|
10
|
+
*/
|
|
11
|
+
export function findSubcommand(subCmdName: string | undefined, subcommandArr: Subcommand[]): Subcommand | undefined {
|
|
12
|
+
return subcommandArr.find(c => {
|
|
13
|
+
// match for undefined too
|
|
14
|
+
if (c.name === subCmdName) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// match for aliases
|
|
19
|
+
return subCmdName && c.aliases && c.aliases.includes(subCmdName);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Finds and returns an `Option` object from the provided list that matches the given argument string.
|
|
25
|
+
*
|
|
26
|
+
* The function supports matching by option name, aliases, and their negated forms (e.g., `--no-` prefix).
|
|
27
|
+
*
|
|
28
|
+
* @param optionArg - The argument string to match (e.g., `--foo`, `--no-bar`, `-f`).
|
|
29
|
+
* @param options - An array of `Option` objects to search through.
|
|
30
|
+
* @returns The matching `Option` object if found; otherwise, `undefined`.
|
|
31
|
+
*/
|
|
32
|
+
export function findOption(optionArg: string, options: [Option, ...Option[]]): Option | undefined {
|
|
33
|
+
const validVarNames = optionArgToVarNames(optionArg);
|
|
34
|
+
const isNegative = optionArg.startsWith("--no-");
|
|
35
|
+
|
|
36
|
+
const option = options.find(o => {
|
|
37
|
+
if (validVarNames.has(o.name)) {
|
|
38
|
+
return true;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (isNegative && validVarNames.has(negateOption(o.name))) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!o.aliases) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (o.aliases.some(a => validVarNames.has(a))) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (isNegative && o.aliases.map(negateOption).some(a => validVarNames.has(a))) {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return false;
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return option;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** - Decouple flags E.g. `-rf` -> `-r, -f` */
|
|
64
|
+
export function decoupleFlags(args: string[]): string[] {
|
|
65
|
+
const flagsRe = /^-[a-z0-9]{2,}$/i;
|
|
66
|
+
|
|
67
|
+
const result = [];
|
|
68
|
+
for (let i = 0; i < args.length; i++) {
|
|
69
|
+
const arg = args[i];
|
|
70
|
+
const isCoupled = flagsRe.test(arg);
|
|
71
|
+
|
|
72
|
+
if (!isCoupled) {
|
|
73
|
+
result.push(arg);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const decoupledArr = arg
|
|
78
|
+
.substring(1)
|
|
79
|
+
.split("")
|
|
80
|
+
.map(c => "-" + c);
|
|
81
|
+
|
|
82
|
+
result.push(...decoupledArr);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* - Transforms an option argument name to a valid JavaScript variable name
|
|
90
|
+
*
|
|
91
|
+
* @param name - Should start with `'--'` or `'-'`
|
|
92
|
+
*/
|
|
93
|
+
export function optionArgToVarNames(name: string): Set<string> {
|
|
94
|
+
if (!name.startsWith("-")) {
|
|
95
|
+
throw new Error(`[parseArgOptionName] Invalid arg name: ${name}`, { cause: "zod-args-parser" });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
name = name.startsWith("--") ? name.substring(2) : name.substring(1); // remove prefix
|
|
99
|
+
name = name.toLowerCase(); // lowercase
|
|
100
|
+
|
|
101
|
+
const results = new Set<string>();
|
|
102
|
+
|
|
103
|
+
// camelCase
|
|
104
|
+
const camelCase = name.replace(/-(.)/g, m => m[1].toUpperCase());
|
|
105
|
+
results.add(camelCase);
|
|
106
|
+
|
|
107
|
+
// PascalCase (UpperCamelCase)
|
|
108
|
+
results.add(camelCase.replace(/^(.)/, m => m.toUpperCase()));
|
|
109
|
+
|
|
110
|
+
// snake_case
|
|
111
|
+
const snake_case = name.replace(/-(.)/g, g => "_" + g[1]);
|
|
112
|
+
results.add(snake_case);
|
|
113
|
+
|
|
114
|
+
// SCREAMING_SNAKE_CASE
|
|
115
|
+
results.add(snake_case.toUpperCase());
|
|
116
|
+
|
|
117
|
+
return results;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** - Check if an arg string is a short arg. E.g. `-i` -> `true` */
|
|
121
|
+
export function isFlagArg(name: string): boolean {
|
|
122
|
+
return /^-[A-Za-z]$/.test(name);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** - Check if an arg string is a long arg. E.g. `--input-dir` -> `true` */
|
|
126
|
+
function isLongArg(name: string): boolean {
|
|
127
|
+
return /^--[A-Za-z-]+[A-Za-z0-9]$/.test(name);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** - Check if an arg string is an options arg. E.g. `--input-dir` -> `true` , `-i` -> `true` */
|
|
131
|
+
export function isOptionArg(name: string | boolean): boolean {
|
|
132
|
+
if (typeof name !== "string") {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
return isFlagArg(name) || isLongArg(name);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* - Transform option name to no name. E.g. `include` -> `noInclude`
|
|
141
|
+
* - For short name like `-i` it will be ignored
|
|
142
|
+
*/
|
|
143
|
+
export function negateOption(name: string): string {
|
|
144
|
+
if (name.length === 1) {
|
|
145
|
+
return name;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return "no" + name.replace(/^[a-z]/, g => g.toUpperCase());
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** - Reverse of `transformArg`. E.g. `InputDir` -> `--input-dir` , `i` -> `-i` */
|
|
152
|
+
export function transformOptionToArg(name: string): string {
|
|
153
|
+
// single letter option name
|
|
154
|
+
if (name.length === 1) {
|
|
155
|
+
return `-${name.toLowerCase()}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// snake_case, SCREAMING_SNAKE_CASE
|
|
159
|
+
if (name.includes("_") || /[A-Z]+$/.test(name)) {
|
|
160
|
+
name = name.replace(/_/g, "-");
|
|
161
|
+
return `--${name.toLowerCase()}`;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// camelCase, PascalCase
|
|
165
|
+
name = name.replace(/[A-Z]/g, (m, i) => (i ? "-" + m : m)); // add "-" before camel case letters except for the first letter
|
|
166
|
+
return `--${name.toLowerCase()}`;
|
|
167
|
+
}
|
package/src/parser/safe-parse.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import * as help from "../help-message/
|
|
2
|
-
import {
|
|
1
|
+
import * as help from "../help-message/format-cli.js";
|
|
2
|
+
import { findSubcommand } from "./parse/parser-helpers.js";
|
|
3
|
+
import { unsafeParse, unsafeParseAsync } from "./unsafe-parse.js";
|
|
3
4
|
|
|
4
5
|
import type { Cli, NoSubcommand, PrintMethods, SafeParseResult, Subcommand } from "../types.js";
|
|
5
6
|
|
|
@@ -14,15 +15,65 @@ export function safeParse<T extends Subcommand[], U extends Cli>(
|
|
|
14
15
|
type PrintCli = PrintTypes["printCliHelp"];
|
|
15
16
|
type PrintSubcommand = PrintTypes["printSubcommandHelp"];
|
|
16
17
|
|
|
17
|
-
const printCliHelp: PrintCli =
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
const printCliHelp: PrintCli = style => help.printCliHelp(params, style);
|
|
19
|
+
|
|
20
|
+
const printSubcommandHelp: PrintSubcommand = (subCmdName, style) => {
|
|
21
|
+
const subcommand = findSubcommand(subCmdName, subcommandArr);
|
|
22
|
+
if (!subcommand) {
|
|
23
|
+
return console.error(`Cannot print help for subcommand "${subCmdName}" as it does not exist`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
help.printSubcommandHelp(subcommand, style, cliOptions.cliName);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
try {
|
|
30
|
+
const data = unsafeParse(argsv, ...params);
|
|
31
|
+
// @ts-expect-error The operand of a 'delete' operator must be optional.
|
|
32
|
+
delete data.printCliHelp;
|
|
33
|
+
// @ts-expect-errorThe operand of a 'delete' operator must be optional.
|
|
34
|
+
delete data.printSubcommandHelp;
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
success: true,
|
|
38
|
+
data: data as Omit<typeof data, "printCliHelp" | "printSubcommandHelp">,
|
|
39
|
+
printCliHelp,
|
|
40
|
+
printSubcommandHelp,
|
|
41
|
+
} as SafeParseResult<[...T, NoSubcommand & U]>;
|
|
42
|
+
} catch (e) {
|
|
43
|
+
if (!(e instanceof Error) || e.cause !== "zod-args-parser") throw e;
|
|
44
|
+
return {
|
|
45
|
+
success: false,
|
|
46
|
+
error: e as Error,
|
|
47
|
+
printCliHelp,
|
|
48
|
+
printSubcommandHelp,
|
|
49
|
+
} as SafeParseResult<[...T, NoSubcommand & U]>;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export async function safeParseAsync<T extends Subcommand[], U extends Cli>(
|
|
54
|
+
argsv: string[],
|
|
55
|
+
...params: [U, ...T]
|
|
56
|
+
): Promise<SafeParseResult<[...T, NoSubcommand & U]>> {
|
|
57
|
+
const cliOptions = ("cliName" in params[0] ? params[0] : {}) as U;
|
|
58
|
+
const subcommandArr = params as Subcommand[];
|
|
59
|
+
|
|
60
|
+
type PrintTypes = PrintMethods<T[number]["name"]>;
|
|
61
|
+
type PrintCli = PrintTypes["printCliHelp"];
|
|
62
|
+
type PrintSubcommand = PrintTypes["printSubcommandHelp"];
|
|
63
|
+
|
|
64
|
+
const printCliHelp: PrintCli = style => help.printCliHelp(params, style);
|
|
65
|
+
|
|
66
|
+
const printSubcommandHelp: PrintSubcommand = (subCmdName, style) => {
|
|
67
|
+
const subcommand = findSubcommand(subCmdName, subcommandArr);
|
|
68
|
+
if (!subcommand) {
|
|
69
|
+
return console.error(`Cannot print help for subcommand "${subCmdName}" as it does not exist`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
help.printSubcommandHelp(subcommand, style, cliOptions.cliName);
|
|
22
73
|
};
|
|
23
74
|
|
|
24
75
|
try {
|
|
25
|
-
const data =
|
|
76
|
+
const data = await unsafeParseAsync(argsv, ...params);
|
|
26
77
|
// @ts-expect-error The operand of a 'delete' operator must be optional.
|
|
27
78
|
delete data.printCliHelp;
|
|
28
79
|
// @ts-expect-errorThe operand of a 'delete' operator must be optional.
|
|
@@ -35,6 +86,7 @@ export function safeParse<T extends Subcommand[], U extends Cli>(
|
|
|
35
86
|
printSubcommandHelp,
|
|
36
87
|
} as SafeParseResult<[...T, NoSubcommand & U]>;
|
|
37
88
|
} catch (e) {
|
|
89
|
+
if (!(e instanceof Error) || e.cause !== "zod-args-parser") throw e;
|
|
38
90
|
return {
|
|
39
91
|
success: false,
|
|
40
92
|
error: e as Error,
|