zod-args-parser 1.1.0 → 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 +597 -91
- 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 +66 -181
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/utils.d.ts +20 -19
- 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 +7 -6
- 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 +3 -2
- package/src/metadata/get-cli-metadata.ts +4 -4
- package/src/metadata/get-options-metadata.ts +4 -3
- 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 +90 -231
- package/src/utils.ts +33 -65
- package/src/{zodUtils.ts → zod-utils.ts} +65 -17
- 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/commonjs/zodUtils.js +0 -1
- package/lib/commonjs/zodUtils.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/module/zodUtils.js +0 -1
- package/lib/module/zodUtils.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/lib/typescript/zodUtils.d.ts +0 -14
- package/lib/typescript/zodUtils.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 -272
|
@@ -1,92 +1,14 @@
|
|
|
1
1
|
import type * as Z3 from "zod/v3";
|
|
2
2
|
import type * as Z4 from "zod/v4/core";
|
|
3
|
+
import type { ParseResult } from "./parser/parse/parse-types.js";
|
|
4
|
+
import type { ValidateResult } from "./parser/validate/validate-type.js";
|
|
3
5
|
export type SchemaV3 = Z3.ZodTypeAny;
|
|
4
6
|
export type SchemaV4 = Z4.$ZodType;
|
|
5
7
|
export type Schema = SchemaV3 | SchemaV4;
|
|
6
|
-
type ZodInfer<T extends Schema> = T extends SchemaV4 ? Z4.infer<T> : T extends SchemaV3 ? Z3.infer<T> : never;
|
|
7
|
-
export interface CliMetadata {
|
|
8
|
-
/** The name of the cli program. */
|
|
9
|
-
readonly name: string;
|
|
10
|
-
/** - The description of the cli program. Empty string if not provided */
|
|
11
|
-
readonly description: string;
|
|
12
|
-
/** - The placeholder of the option. Empty string if not provided */
|
|
13
|
-
readonly placeholder: string;
|
|
14
|
-
/** - The usage of the cli program. Empty string if not provided */
|
|
15
|
-
readonly usage: string;
|
|
16
|
-
/** - The example of the cli program. Empty string if not provided */
|
|
17
|
-
readonly example: string;
|
|
18
|
-
/** - Whether the cli program allows positional arguments. */
|
|
19
|
-
readonly allowPositional: boolean;
|
|
20
|
-
/** - The options of the cli program. Empty array if not provided */
|
|
21
|
-
readonly options: OptionMetadata[];
|
|
22
|
-
/** - The arguments of the cli program. Empty array if not provided */
|
|
23
|
-
readonly arguments: ArgumentMetadata[];
|
|
24
|
-
/** - The subcommands of the cli program. Empty array if not provided */
|
|
25
|
-
readonly subcommands: SubcommandMetadata[];
|
|
26
|
-
}
|
|
27
|
-
export interface SubcommandMetadata {
|
|
28
|
-
/** The subcommand name. */
|
|
29
|
-
readonly name: string;
|
|
30
|
-
/** - The aliases of the subcommand. Empty array if not provided */
|
|
31
|
-
readonly aliases: string[];
|
|
32
|
-
/** - The description of the subcommand. Empty string if not provided */
|
|
33
|
-
readonly description: string;
|
|
34
|
-
/** - The placeholder of the subcommand. Empty string if not provided */
|
|
35
|
-
readonly placeholder: string;
|
|
36
|
-
/** - The usage of the subcommand. Empty string if not provided */
|
|
37
|
-
readonly usage: string;
|
|
38
|
-
/** - The example of the subcommand. Empty string if not provided */
|
|
39
|
-
readonly example: string;
|
|
40
|
-
/** - Whether the subcommand allows positional arguments. */
|
|
41
|
-
readonly allowPositional: boolean;
|
|
42
|
-
/** - The options of the subcommand. Empty array if not provided */
|
|
43
|
-
readonly options: OptionMetadata[];
|
|
44
|
-
/** - The arguments of the subcommand. Empty array if not provided */
|
|
45
|
-
readonly arguments: ArgumentMetadata[];
|
|
46
|
-
}
|
|
47
|
-
export interface OptionMetadata {
|
|
48
|
-
/** The option name in camelCase. E.g. `optionName` */
|
|
49
|
-
readonly name: string;
|
|
50
|
-
/** The option name in kebab-case. E.g. `--option-name` */
|
|
51
|
-
readonly nameAsArg: string;
|
|
52
|
-
/** - The aliases of the option in camelCase. Empty array if not provided. E.g. `[aliasName, ...]` */
|
|
53
|
-
readonly aliases: string[];
|
|
54
|
-
/** - The aliases of the option in kebab-case. Empty array if not provided. E.g. `[--alias-name, ...]` */
|
|
55
|
-
readonly aliasesAsArgs: string[];
|
|
56
|
-
/** - The description of the option. Empty string if not provided */
|
|
57
|
-
readonly description: string;
|
|
58
|
-
/** - The placeholder of the option. Empty string if not provided */
|
|
59
|
-
readonly placeholder: string;
|
|
60
|
-
/** - The example of the option. Empty string if not provided */
|
|
61
|
-
readonly example: string;
|
|
62
|
-
/** - The default value of the option. */
|
|
63
|
-
readonly defaultValue: unknown;
|
|
64
|
-
/** - The default value of the option as string. */
|
|
65
|
-
readonly defaultValueAsString: string;
|
|
66
|
-
/** - Whether the option is optional. */
|
|
67
|
-
readonly optional: boolean;
|
|
68
|
-
/** - The zod type of the option. */
|
|
69
|
-
readonly type: Schema;
|
|
70
|
-
}
|
|
71
|
-
export interface ArgumentMetadata {
|
|
72
|
-
/** The argument name. */
|
|
73
|
-
readonly name: string;
|
|
74
|
-
/** - The description of the argument. Empty string if not provided */
|
|
75
|
-
readonly description: string;
|
|
76
|
-
/** - The example of the argument. Empty string if not provided */
|
|
77
|
-
readonly example: string;
|
|
78
|
-
/** - The default value of the argument. */
|
|
79
|
-
readonly defaultValue: unknown;
|
|
80
|
-
/** - The default value of the argument as string. */
|
|
81
|
-
readonly defaultValueAsString: string;
|
|
82
|
-
/** - Whether the argument is optional. */
|
|
83
|
-
readonly optional: boolean;
|
|
84
|
-
/** - The zod type of the argument. */
|
|
85
|
-
readonly type: Schema;
|
|
86
|
-
}
|
|
8
|
+
export type ZodInfer<T extends Schema> = T extends SchemaV4 ? Z4.infer<T> : T extends SchemaV3 ? Z3.infer<T> : never;
|
|
87
9
|
export type Subcommand = {
|
|
88
10
|
/**
|
|
89
|
-
* - The subcommand name
|
|
11
|
+
* - The subcommand name
|
|
90
12
|
* - Make sure to not duplicate commands and aliases.
|
|
91
13
|
*
|
|
92
14
|
* @example
|
|
@@ -141,27 +63,36 @@ export type Subcommand = {
|
|
|
141
63
|
* const helpCommand = createSubcommand({ name: "help", options: [...] });
|
|
142
64
|
* helpCommand.setAction(res => console.log(res));
|
|
143
65
|
*/
|
|
144
|
-
action?: (results?: any) =>
|
|
66
|
+
action?: (results?: any) => any;
|
|
67
|
+
/**
|
|
68
|
+
* - The preValidation hook is executed before the action.
|
|
69
|
+
* - To get typescript types use `setPreValidationHook` instead of this.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* const helpCommand = createSubcommand({ name: "help", options: [...] });
|
|
73
|
+
* helpCommand.setPreValidationHook(ctx => console.log(ctx));
|
|
74
|
+
*/
|
|
75
|
+
preValidation?: (ctx?: any) => any;
|
|
145
76
|
};
|
|
146
|
-
export type Cli = Prettify<Omit<Subcommand, "name"> & {
|
|
77
|
+
export type Cli = Prettify<Omit<Subcommand, "name" | "aliases" | "placeholder"> & {
|
|
147
78
|
/** - The name of the CLI program. */
|
|
148
79
|
cliName: string;
|
|
149
80
|
}>;
|
|
150
81
|
export type Option = {
|
|
151
82
|
/**
|
|
152
|
-
*
|
|
153
|
-
*
|
|
83
|
+
* The name of the option, use a valid **JavaScript** variable name.\
|
|
84
|
+
* **Supports:** `camelCase`, `PascalCase`, `snake_case`, and `SCREAMING_SNAKE_CASE`.\
|
|
85
|
+
* **Examples:**
|
|
86
|
+
*
|
|
87
|
+
* - `I` or `i` ➡️ `-i`
|
|
88
|
+
* - `InputDir`, `inputDir`, or `INPUT_DIR` ➡️ `--input-dir`
|
|
89
|
+
* - `Help`, `help`, or `HELP` ➡️ `--help`
|
|
154
90
|
*/
|
|
155
91
|
name: string;
|
|
156
92
|
/**
|
|
157
93
|
* - The will be used to validate the user input.
|
|
158
94
|
*
|
|
159
|
-
* @
|
|
160
|
-
* type: z.boolean().default(false);
|
|
161
|
-
* type: z.coerce.number(); // will be coerced to number by Zod
|
|
162
|
-
* type: z.preprocess(parseStringToArrFn, z.array(z.coerce.number())); // array of numbers
|
|
163
|
-
*
|
|
164
|
-
* @see https://zod.dev/?id=types
|
|
95
|
+
* @see https://zod.dev/api
|
|
165
96
|
*/
|
|
166
97
|
type: Schema;
|
|
167
98
|
/**
|
|
@@ -189,12 +120,7 @@ export type Argument = {
|
|
|
189
120
|
/**
|
|
190
121
|
* - The will be used to validate the user input.
|
|
191
122
|
*
|
|
192
|
-
* @
|
|
193
|
-
* type: z.boolean();
|
|
194
|
-
* type: z.coerce.number(); // will be coerced to number by Zod
|
|
195
|
-
* type: z.preprocess(ParseStringToArrFn, z.array(z.coerce.number())); // array of numbers
|
|
196
|
-
*
|
|
197
|
-
* @see https://zod.dev/?id=types
|
|
123
|
+
* @see https://zod.dev/api
|
|
198
124
|
*/
|
|
199
125
|
type: Schema;
|
|
200
126
|
/**
|
|
@@ -209,50 +135,8 @@ export type Argument = {
|
|
|
209
135
|
example?: string;
|
|
210
136
|
};
|
|
211
137
|
export type ColorFnType = (...text: unknown[]) => string;
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
* - **Optional** `boolean`
|
|
215
|
-
* - Whether to print colors or not.
|
|
216
|
-
* - Default: `true`
|
|
217
|
-
*/
|
|
218
|
-
colors?: boolean;
|
|
219
|
-
/**
|
|
220
|
-
* - **Optional** `object`
|
|
221
|
-
* - The colors to use for the help message.
|
|
222
|
-
*/
|
|
223
|
-
customColors?: {
|
|
224
|
-
title?: ColorFnType;
|
|
225
|
-
description?: ColorFnType;
|
|
226
|
-
default?: ColorFnType;
|
|
227
|
-
optional?: ColorFnType;
|
|
228
|
-
exampleTitle?: ColorFnType;
|
|
229
|
-
example?: ColorFnType;
|
|
230
|
-
command?: ColorFnType;
|
|
231
|
-
option?: ColorFnType;
|
|
232
|
-
argument?: ColorFnType;
|
|
233
|
-
placeholder?: ColorFnType;
|
|
234
|
-
punctuation?: ColorFnType;
|
|
235
|
-
};
|
|
236
|
-
};
|
|
237
|
-
export type _Info = {
|
|
238
|
-
/**
|
|
239
|
-
* - The raw argument as it was passed in
|
|
240
|
-
* - For options that have a default value and are not passed in, the raw argument will be `undefined`
|
|
241
|
-
*/
|
|
242
|
-
rawArg?: string;
|
|
243
|
-
/**
|
|
244
|
-
* - The raw value of the argument as it was passed in
|
|
245
|
-
* - It will be empty string for `boolean` options. E.g. `--help` or `-h`
|
|
246
|
-
* - For options that have a default value and are not passed in, the raw value will be `undefined`
|
|
247
|
-
*/
|
|
248
|
-
rawValue?: string;
|
|
249
|
-
/**
|
|
250
|
-
* - The source value of the argument:
|
|
251
|
-
* - `cli`: The argument was passed in by the user
|
|
252
|
-
* - `default`: The argument was not passed in and has a default value
|
|
253
|
-
*/
|
|
254
|
-
source: "cli" | "default";
|
|
255
|
-
};
|
|
138
|
+
/** - The colors to use for the help message. */
|
|
139
|
+
export type HelpMsgStyle = Record<"title" | "description" | "default" | "optional" | "exampleTitle" | "example" | "command" | "option" | "argument" | "placeholder" | "punctuation", ColorFnType>;
|
|
256
140
|
/**
|
|
257
141
|
* - Infer the options type from a subcommand.
|
|
258
142
|
*
|
|
@@ -284,55 +168,29 @@ export type Prettify<T> = {
|
|
|
284
168
|
/** Allow string type for literal union and get auto completion */
|
|
285
169
|
export type LiteralUnion<T extends string> = T | (string & {});
|
|
286
170
|
/** Extract the undefined properties from an object */
|
|
287
|
-
type UndefinedProperties<T> = {
|
|
171
|
+
export type UndefinedProperties<T> = {
|
|
288
172
|
[P in keyof T]-?: undefined extends T[P] ? P : never;
|
|
289
173
|
}[keyof T];
|
|
290
174
|
/** Make undefined properties optional? */
|
|
291
|
-
type ToOptional<T> = Prettify<Partial<Pick<T, UndefinedProperties<T>>> & Pick<T, Exclude<keyof T, UndefinedProperties<T>>>>;
|
|
292
|
-
export type OptionsArr2RecordType<T extends Option[] | undefined> = T extends Option[] ? ToOptional<{
|
|
293
|
-
[K in T[number]["name"]]: ZodInfer<Extract<T[number], {
|
|
294
|
-
name: K;
|
|
295
|
-
}>["type"]>;
|
|
296
|
-
}> : object;
|
|
297
|
-
export type ArgumentsArr2ArrType<T extends Argument[] | undefined> = T extends Argument[] ? {
|
|
298
|
-
arguments: {
|
|
299
|
-
[K in keyof T]: T[K] extends {
|
|
300
|
-
type: Schema;
|
|
301
|
-
} ? ZodInfer<T[K]["type"]> : never;
|
|
302
|
-
};
|
|
303
|
-
} : object;
|
|
304
|
-
export type Positional<S extends Partial<Subcommand>> = S["allowPositional"] extends true ? {
|
|
305
|
-
positional: string[];
|
|
306
|
-
} : object;
|
|
307
|
-
export type Info<T extends Option[] | undefined> = T extends Option[] ? {
|
|
308
|
-
_info: ToOptional<{
|
|
309
|
-
[K in T[number]["name"]]: Extract<T[number], {
|
|
310
|
-
name: K;
|
|
311
|
-
}> extends infer U extends Option ? undefined extends ZodInfer<U["type"]> ? undefined | Prettify<_Info & U> : Prettify<_Info & U> : never;
|
|
312
|
-
}>;
|
|
313
|
-
} : object;
|
|
175
|
+
export type ToOptional<T> = Prettify<Partial<Pick<T, UndefinedProperties<T>>> & Pick<T, Exclude<keyof T, UndefinedProperties<T>>>>;
|
|
314
176
|
export type NoSubcommand = {
|
|
315
177
|
name: undefined;
|
|
316
178
|
};
|
|
317
|
-
export type ParseResult<S extends Partial<Subcommand>[]> = {
|
|
318
|
-
[K in keyof S]: Prettify<{
|
|
319
|
-
subcommand: S[K]["name"];
|
|
320
|
-
} & Positional<S[K]> & Info<S[K]["options"]> & OptionsArr2RecordType<S[K]["options"]> & ArgumentsArr2ArrType<S[K]["arguments"]>>;
|
|
321
|
-
}[number];
|
|
322
179
|
export type PrintMethods<N extends Subcommand["name"]> = {
|
|
323
|
-
printCliHelp: (
|
|
324
|
-
printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>,
|
|
180
|
+
printCliHelp: (style?: Partial<HelpMsgStyle>) => void;
|
|
181
|
+
printSubcommandHelp: (subcommand: LiteralUnion<NonNullable<N>>, style?: Partial<HelpMsgStyle>) => void;
|
|
325
182
|
};
|
|
326
|
-
export type
|
|
183
|
+
export type UnsafeParseResult<S extends Partial<Subcommand>[]> = CheckDuplicatedSubcommands<S> extends infer E extends string ? E : Prettify<ValidateResult<S> & PrintMethods<NonNullable<S[number]["name"]>>>;
|
|
327
184
|
export type SafeParseResult<S extends Partial<Subcommand>[]> = CheckDuplicatedSubcommands<S> extends infer E extends string ? E : Prettify<({
|
|
328
185
|
success: false;
|
|
329
186
|
error: Error;
|
|
330
187
|
} | {
|
|
331
188
|
success: true;
|
|
332
|
-
data:
|
|
189
|
+
data: ValidateResult<S>;
|
|
333
190
|
}) & PrintMethods<NonNullable<S[number]["name"]>>>;
|
|
334
|
-
export type
|
|
335
|
-
setAction: (actions: (res:
|
|
191
|
+
export type ActionsFn<T extends Subcommand | Cli> = {
|
|
192
|
+
setAction: (actions: (res: UnsafeParseResult<[T]>) => void) => void;
|
|
193
|
+
setPreValidationHook: (hookFn: (ctx: ParseResult<[T]>) => void) => void;
|
|
336
194
|
};
|
|
337
195
|
/** - Combine `name` and `aliases` to a `string[]` */
|
|
338
196
|
type MapNameAndAliases2StrArr<T extends {
|
|
@@ -353,16 +211,43 @@ type IsDuplicatesInArr<Input extends any[]> = Input extends [infer Item, ...infe
|
|
|
353
211
|
/**
|
|
354
212
|
* - Check if there are duplicated options including aliases in `subcommand`
|
|
355
213
|
* - Return an error message if duplicated is found
|
|
356
|
-
* - Return `
|
|
214
|
+
* - Return `undefined` if not found
|
|
357
215
|
*/
|
|
358
216
|
export type CheckDuplicatedOptions<T extends {
|
|
359
217
|
options?: Option[];
|
|
360
|
-
}> = T["options"] extends infer O extends Option[] ? IsDuplicatesInArr<MapNameAndAliases2StrArr<O>> extends infer
|
|
218
|
+
}> = T["options"] extends infer O extends Option[] ? IsDuplicatesInArr<MapNameAndAliases2StrArr<O>> extends infer Name extends string ? `>>> Error: Duplicated Options. Check the options with the name \`${Name}\` <<<` : undefined : undefined;
|
|
361
219
|
/**
|
|
362
220
|
* - Check for duplicated subcommands including aliases
|
|
363
221
|
* - Return an error message if duplicated is found
|
|
364
|
-
* - Return the `
|
|
222
|
+
* - Return the `undefined` if no error
|
|
223
|
+
*/
|
|
224
|
+
type CheckDuplicatedSubcommands<T extends Partial<Subcommand>[]> = IsDuplicatesInArr<MapNameAndAliases2StrArr<T>> extends infer Name extends string ? `>>> Error: Duplicated Subcommand. Check the subcommands with the name \`${Name}\` <<<` : undefined;
|
|
225
|
+
/**
|
|
226
|
+
* - Check for duplicated arguments
|
|
227
|
+
* - Return an error message if duplicated is found
|
|
228
|
+
* - Return the `undefined` if no error
|
|
229
|
+
*/
|
|
230
|
+
export type CheckDuplicatedArguments<T extends {
|
|
231
|
+
arguments?: Argument[];
|
|
232
|
+
}> = T["arguments"] extends infer A extends Argument[] ? IsDuplicatesInArr<MapNameAndAliases2StrArr<A>> extends infer Name extends string ? `>>> Error: Duplicated Arguments. Check the arguments with the name \`${Name}\` <<<` : undefined : undefined;
|
|
233
|
+
type OptionalUnion = Z3.ZodOptional<Z3.ZodAny> | Z4.$ZodOptional | Z3.ZodDefault<Z3.ZodAny> | Z4.$ZodDefault;
|
|
234
|
+
/**
|
|
235
|
+
* - Insures that only the last argument is optional
|
|
236
|
+
* - Insures no optional arguments are allowed when `allowPositional` is enabled
|
|
365
237
|
*/
|
|
366
|
-
export type
|
|
238
|
+
export type CheckArgumentsOptional<T extends {
|
|
239
|
+
allowPositional?: boolean;
|
|
240
|
+
arguments?: readonly Argument[];
|
|
241
|
+
}> = T["arguments"] extends readonly [...infer Rest, infer Last] ? Last extends {
|
|
242
|
+
type: OptionalUnion;
|
|
243
|
+
} ? T["allowPositional"] extends true ? `>>> Error: Cannot have optional arguments when \`allowPositional\` is enabled. The argument \`${Last extends {
|
|
244
|
+
name: string;
|
|
245
|
+
} ? Last["name"] : ""}\` should not be optional <<<` : T : Extract<Rest[number], {
|
|
246
|
+
type: OptionalUnion;
|
|
247
|
+
}> extends never ? T : T["allowPositional"] extends true ? `>>> Error: Cannot have optional arguments when \`allowPositional\` is enabled. The argument \`${Rest[number] extends {
|
|
248
|
+
name: string;
|
|
249
|
+
} ? Rest[number]["name"] : ""}\` should not be optional <<<` : `>>> Error: Only the last argument may be optional. The argument \`${Rest[number] extends {
|
|
250
|
+
name: string;
|
|
251
|
+
} ? Rest[number]["name"] : ""}\` should not be optional <<<` : T;
|
|
367
252
|
export {};
|
|
368
253
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,EAAE,MAAM,QAAQ,CAAC;AAClC,OAAO,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oCAAoC,CAAC;AAEzE,MAAM,MAAM,QAAQ,GAAG,EAAE,CAAC,UAAU,CAAC;AACrC,MAAM,MAAM,QAAQ,GAAG,EAAE,CAAC,QAAQ,CAAC;AACnC,MAAM,MAAM,MAAM,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAEzC,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,SAAS,QAAQ,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,SAAS,QAAQ,GAAG,EAAE,CAAC,KAAK,CAAC,C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CAAC,MAAM,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,EAAE,+BAA+B,GACjM,qEAAqE,IAAI,CAAC,MAAM,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,EAAE,+BAA+B,GAC3K,CAAC,CAAC"}
|
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Converts a string to its corresponding boolean value if the string is "true" or "false" (case-insensitive).
|
|
3
|
+
*
|
|
4
|
+
* @param str - The input string to convert.
|
|
5
|
+
* @returns `true` if the input is "true", `false` if the input is "false", or the original string otherwise.
|
|
4
6
|
*/
|
|
5
|
-
export declare function
|
|
6
|
-
/** - Reverse of `transformArg`. E.g. `InputDir` -> `--input-dir` , `i` -> `-i` */
|
|
7
|
-
export declare function transformOptionToArg(name: string): string;
|
|
8
|
-
/** - Check if an arg string is a short arg. E.g. `-i` -> `true` */
|
|
9
|
-
export declare function isFlagArg(name: string): boolean;
|
|
10
|
-
/** - Check if an arg string is a long arg. E.g. `--input-dir` -> `true` */
|
|
11
|
-
export declare function isLongArg(name: string): boolean;
|
|
12
|
-
/** - Check if an arg string is an options arg. E.g. `--input-dir` -> `true` , `-i` -> `true` */
|
|
13
|
-
export declare function isOptionArg(name: string | boolean): boolean;
|
|
7
|
+
export declare function stringToBoolean(str: string): boolean | string;
|
|
14
8
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
9
|
+
* Returns the ordinal representation of a given zero-based index.
|
|
10
|
+
*
|
|
11
|
+
* For example, passing `0` returns `"1st"`, `1` returns `"2nd"`, and so on.
|
|
12
|
+
*
|
|
13
|
+
* Handles special cases for numbers ending in 11, 12, or 13 (e.g., `10` returns `"11th"`).
|
|
14
|
+
*
|
|
15
|
+
* @param index - The zero-based index to convert to an ordinal string.
|
|
16
|
+
* @returns The ordinal string representation (e.g., `"1st"`, `"2nd"`, `"3rd"`, `"4th"`, ...).
|
|
17
17
|
*/
|
|
18
|
-
export declare function
|
|
19
|
-
/**
|
|
20
|
-
export declare function
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
export declare function generateOrdinalSuffix(index: number): string;
|
|
19
|
+
/** New line */
|
|
20
|
+
export declare function ln(count: number): string;
|
|
21
|
+
/** Space */
|
|
22
|
+
export declare function indent(count: number): string;
|
|
23
|
+
/** Concat strings */
|
|
24
|
+
export declare function concat(...messages: string[]): string;
|
|
24
25
|
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,CAU7D;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAiB3D;AAED,eAAe;AACf,wBAAgB,EAAE,CAAC,KAAK,EAAE,MAAM,UAE/B;AAED,YAAY;AACZ,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,UAEnC;AAED,qBAAqB;AACrB,wBAAgB,MAAM,CAAC,GAAG,QAAQ,EAAE,MAAM,EAAE,UAG3C"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type * as Z4 from "zod/v4/core";
|
|
2
|
+
import type { Schema } from "./types.js";
|
|
3
|
+
/** - Safe parse a value against a schema */
|
|
4
|
+
export declare function safeParseSchema(schema: Schema, value: unknown): Z4.util.SafeParseError<unknown> | import("zod/v3").SafeParseSuccess<any> | import("zod/v3").SafeParseError<any>;
|
|
5
|
+
/** - Check if a schema is a boolean */
|
|
6
|
+
export declare function isBooleanSchema(schema: Schema): boolean;
|
|
7
|
+
/** - Get the default value of a schema */
|
|
8
|
+
export declare function schemaDefaultValue(schema: Schema): unknown | undefined;
|
|
9
|
+
/** - Get the description of a schema */
|
|
10
|
+
export declare function schemaDescription(schema: Schema): string | undefined;
|
|
11
|
+
/** - Check if a schema is optional */
|
|
12
|
+
export declare function isOptionalSchema(schema: Schema): schema is Z4.$ZodOptional;
|
|
13
|
+
/**
|
|
14
|
+
* A preprocessing function for Zod that converts a string to an array of strings.
|
|
15
|
+
*
|
|
16
|
+
* @param val - The value given by zod.
|
|
17
|
+
* @param sep - The separator to use when splitting the string. Defaults to ",".
|
|
18
|
+
*/
|
|
19
|
+
export declare function stringToArray(val: unknown, sep?: string): unknown;
|
|
20
|
+
/**
|
|
21
|
+
* A preprocessing function for Zod that converts a string to a `Set` of strings.
|
|
22
|
+
*
|
|
23
|
+
* @param val - The value given by zod.
|
|
24
|
+
* @param sep - The separator to use when splitting the string. Defaults to ",".
|
|
25
|
+
*/
|
|
26
|
+
export declare function stringToSet(val: unknown, sep?: string): unknown;
|
|
27
|
+
//# sourceMappingURL=zod-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-utils.d.ts","sourceRoot":"","sources":["../../src/zod-utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,KAAK,EAAE,MAAM,EAAsB,MAAM,YAAY,CAAC;AAM7D,4CAA4C;AAC5C,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,mHAM7D;AAED,uCAAuC;AACvC,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAMvD;AAyCD,0CAA0C;AAC1C,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS,CAMtE;AAyCD,wCAAwC;AACxC,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOpE;AAED,sCAAsC;AACtC,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC,YAAY,CAM1E;AAmCD;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,GAAE,MAAY,WAS5D;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,EAAE,GAAG,GAAE,MAAY,WAQ1D"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod-args-parser",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A strictly typed command-line arguments parser powered by Zod.",
|
|
5
5
|
"author": "Ahmed ALABSI <alabsi91@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -45,9 +45,9 @@
|
|
|
45
45
|
"@eslint/compat": "^1.3.2",
|
|
46
46
|
"@eslint/js": "^9.34.0",
|
|
47
47
|
"@types/babel__core": "^7.20.5",
|
|
48
|
-
"@types/node": "^
|
|
48
|
+
"@types/node": "^24.3.0",
|
|
49
49
|
"eslint": "^9.34.0",
|
|
50
|
-
"eslint-config-prettier": "^10.1.
|
|
50
|
+
"eslint-config-prettier": "^10.1.8",
|
|
51
51
|
"eslint-plugin-prettier": "^5.5.4",
|
|
52
52
|
"glob": "^11.0.3",
|
|
53
53
|
"globals": "^16.3.0",
|
|
@@ -55,10 +55,11 @@
|
|
|
55
55
|
"prettier-plugin-jsdoc": "^1.3.3",
|
|
56
56
|
"tsx": "^4.20.5",
|
|
57
57
|
"typescript": "^5.9.2",
|
|
58
|
-
"typescript-eslint": "^8.41.0"
|
|
58
|
+
"typescript-eslint": "^8.41.0",
|
|
59
|
+
"zod": "^4.1.5"
|
|
59
60
|
},
|
|
60
61
|
"peerDependencies": {
|
|
61
|
-
"
|
|
62
|
-
"
|
|
62
|
+
"chalk": "*",
|
|
63
|
+
"zod": "^3.25.0 || ^4.0.0"
|
|
63
64
|
}
|
|
64
65
|
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { concat, indent, ln } from "../utils.js";
|
|
2
|
+
|
|
3
|
+
import type { ArgumentMetadata } from "../metadata/metadata-types.js";
|
|
4
|
+
import type { HelpMsgStyle } from "../types.js";
|
|
5
|
+
|
|
6
|
+
export function formatHelpMsgArguments(argsMetadata: ArgumentMetadata[], c: HelpMsgStyle, longest: number): string {
|
|
7
|
+
if (!argsMetadata.length) return "";
|
|
8
|
+
|
|
9
|
+
let msg = c.title(" ARGUMENTS") + ln(1);
|
|
10
|
+
|
|
11
|
+
for (const metadata of argsMetadata) {
|
|
12
|
+
const defaultStr =
|
|
13
|
+
typeof metadata.defaultValue !== "undefined" ? `(default: ${metadata.defaultValueAsString})` : "";
|
|
14
|
+
|
|
15
|
+
const spacing = longest + 2 - metadata.name.length;
|
|
16
|
+
const normalizeDesc = metadata.description.replace(/\n/g, "\n" + indent(longest + 6) + c.punctuation("└"));
|
|
17
|
+
|
|
18
|
+
msg += concat(
|
|
19
|
+
indent(2) + c.argument(metadata.name),
|
|
20
|
+
indent(spacing),
|
|
21
|
+
c.description(normalizeDesc),
|
|
22
|
+
(defaultStr ? c.default(defaultStr) : metadata.optional ? c.optional("(optional)") : "") + ln(1),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
if (metadata.example) {
|
|
26
|
+
const normalizeExample = metadata.example.replace(/\n/g, "\n" + indent(longest + 16));
|
|
27
|
+
msg += concat(
|
|
28
|
+
indent(longest + 5),
|
|
29
|
+
c.punctuation("└") + c.exampleTitle("Example:"),
|
|
30
|
+
c.example(normalizeExample) + ln(1),
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
msg += ln(1);
|
|
36
|
+
|
|
37
|
+
return msg;
|
|
38
|
+
}
|