@clerc/parser 1.0.0-beta.16 → 1.0.0-beta.18
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/dist/index.d.ts +40 -22
- package/dist/index.js +72 -39
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -6,23 +6,6 @@ declare class MissingRequiredFlagError extends Error {
|
|
|
6
6
|
constructor(name: string);
|
|
7
7
|
}
|
|
8
8
|
//#endregion
|
|
9
|
-
//#region src/flag-types.d.ts
|
|
10
|
-
/**
|
|
11
|
-
* Creates a Enum type function that validates the input against allowed values.
|
|
12
|
-
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
13
|
-
*
|
|
14
|
-
* @param values - Array of allowed string values
|
|
15
|
-
* @returns A FlagTypeFunction that validates and returns the input value
|
|
16
|
-
* @throws {Error} If the value is not in the allowed values list
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const format = Enum(['json', 'yaml', 'xml']);
|
|
21
|
-
* // Help output will show: json | yaml | xml
|
|
22
|
-
* ```
|
|
23
|
-
*/
|
|
24
|
-
declare function Enum<T extends string>(...values: T[]): FlagTypeFunction<T>;
|
|
25
|
-
//#endregion
|
|
26
9
|
//#region ../utils/src/types/type-fest.d.ts
|
|
27
10
|
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
28
11
|
type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
@@ -30,6 +13,11 @@ type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
|
30
13
|
//#region ../utils/src/types/index.d.ts
|
|
31
14
|
type MaybeArray<T> = T | T[];
|
|
32
15
|
//#endregion
|
|
16
|
+
//#region src/iterator.d.ts
|
|
17
|
+
declare const KNOWN_FLAG = "known-flag";
|
|
18
|
+
declare const UNKNOWN_FLAG = "unknown-flag";
|
|
19
|
+
declare const PARAMETER = "parameter";
|
|
20
|
+
//#endregion
|
|
33
21
|
//#region src/types.d.ts
|
|
34
22
|
interface FlagDefaultValueFunction<T> {
|
|
35
23
|
(): T;
|
|
@@ -159,10 +147,40 @@ type _InferFlags<T extends FlagsDefinition> = { [K in keyof T]: IsTypeAny<T[K]>
|
|
|
159
147
|
*/
|
|
160
148
|
type InferFlags<T extends FlagsDefinition> = Prettify<_InferFlags<T>>;
|
|
161
149
|
//#endregion
|
|
162
|
-
//#region src/
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
150
|
+
//#region src/flag-types.d.ts
|
|
151
|
+
/**
|
|
152
|
+
* Creates a Enum type function that validates the input against allowed values.
|
|
153
|
+
* The display name will be formatted as "value1 | value2 | ..." for help output.
|
|
154
|
+
*
|
|
155
|
+
* @param values - Array of allowed string values
|
|
156
|
+
* @returns A FlagTypeFunction that validates and returns the input value
|
|
157
|
+
* @throws {Error} If the value is not in the allowed values list
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```typescript
|
|
161
|
+
* const format = Enum(['json', 'yaml', 'xml']);
|
|
162
|
+
* // Help output will show: json | yaml | xml
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
declare function Enum<T extends string>(...values: T[]): FlagTypeFunction<T>;
|
|
166
|
+
/**
|
|
167
|
+
* Creates a range type function that validates the input is a number within the specified range.
|
|
168
|
+
*
|
|
169
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
170
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
171
|
+
* @returns A FlagTypeFunction that validates the input value
|
|
172
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
173
|
+
*/
|
|
174
|
+
declare function Range(min: number, max: number): FlagTypeFunction<number>;
|
|
175
|
+
/**
|
|
176
|
+
* Creates a regex type function that validates the input against the provided pattern.
|
|
177
|
+
*
|
|
178
|
+
* @param pattern - The regular expression pattern to validate against
|
|
179
|
+
* @param description - Optional description for display purposes
|
|
180
|
+
* @returns A FlagTypeFunction that validates the input value
|
|
181
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
182
|
+
*/
|
|
183
|
+
declare function Regex(pattern: RegExp, description?: string): FlagTypeFunction<string>;
|
|
166
184
|
//#endregion
|
|
167
185
|
//#region src/parse.d.ts
|
|
168
186
|
declare const DOUBLE_DASH = "--";
|
|
@@ -172,4 +190,4 @@ declare function createParser<T extends FlagsDefinition>(options?: ParserOptions
|
|
|
172
190
|
};
|
|
173
191
|
declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
|
|
174
192
|
//#endregion
|
|
175
|
-
export { BaseFlagOptions, DOUBLE_DASH, Enum, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagType, FlagTypeFunction, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, RawInputType, UNKNOWN_FLAG, createParser, parse };
|
|
193
|
+
export { BaseFlagOptions, DOUBLE_DASH, Enum, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagType, FlagTypeFunction, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, Range, RawInputType, Regex, UNKNOWN_FLAG, createParser, parse };
|
package/dist/index.js
CHANGED
|
@@ -36,6 +36,39 @@ function Enum(...values) {
|
|
|
36
36
|
fn.display = values.join(" | ");
|
|
37
37
|
return fn;
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a range type function that validates the input is a number within the specified range.
|
|
41
|
+
*
|
|
42
|
+
* @param min - The minimum acceptable value (inclusive)
|
|
43
|
+
* @param max - The maximum acceptable value (inclusive)
|
|
44
|
+
* @returns A FlagTypeFunction that validates the input value
|
|
45
|
+
* @throws {Error} If the value is not a number or is outside the specified range
|
|
46
|
+
*/
|
|
47
|
+
function Range(min, max) {
|
|
48
|
+
const fn = ((value) => {
|
|
49
|
+
const num = Number(value);
|
|
50
|
+
if (Number.isNaN(num) || num < min || num > max) throw new Error(`Invalid value: ${value}. Must be a number between ${min} and ${max}`);
|
|
51
|
+
return num;
|
|
52
|
+
});
|
|
53
|
+
fn.display = `${min}-${max}`;
|
|
54
|
+
return fn;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a regex type function that validates the input against the provided pattern.
|
|
58
|
+
*
|
|
59
|
+
* @param pattern - The regular expression pattern to validate against
|
|
60
|
+
* @param description - Optional description for display purposes
|
|
61
|
+
* @returns A FlagTypeFunction that validates the input value
|
|
62
|
+
* @throws {Error} If the value does not match the regex pattern
|
|
63
|
+
*/
|
|
64
|
+
function Regex(pattern, description) {
|
|
65
|
+
const fn = ((value) => {
|
|
66
|
+
if (!pattern.test(value)) throw new Error(`Invalid value: ${value}. Must match pattern: ${pattern}`);
|
|
67
|
+
return value;
|
|
68
|
+
});
|
|
69
|
+
fn.display = description ?? pattern.toString();
|
|
70
|
+
return fn;
|
|
71
|
+
}
|
|
39
72
|
|
|
40
73
|
//#endregion
|
|
41
74
|
//#region src/iterator.ts
|
|
@@ -88,6 +121,7 @@ function iterateArgs(args, result, shouldProcessAsFlag, isKnownFlag, ignore, cal
|
|
|
88
121
|
|
|
89
122
|
//#endregion
|
|
90
123
|
//#region ../utils/src/index.ts
|
|
124
|
+
const looseIsArray = (arr) => Array.isArray(arr);
|
|
91
125
|
const toArray = (a) => Array.isArray(a) ? a : [a];
|
|
92
126
|
/**
|
|
93
127
|
* Converts a dash- or space-separated string to camelCase.
|
|
@@ -110,9 +144,45 @@ function camelCase(str) {
|
|
|
110
144
|
return result;
|
|
111
145
|
}
|
|
112
146
|
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/config.ts
|
|
149
|
+
const defaultParserOptions = { delimiters: ["=", ":"] };
|
|
150
|
+
const resolveParserOptions = (options = {}) => ({
|
|
151
|
+
...defaultParserOptions,
|
|
152
|
+
...options
|
|
153
|
+
});
|
|
154
|
+
const normalizeConfig = (config) => typeof config === "function" || looseIsArray(config) ? { type: config } : config;
|
|
155
|
+
const BUILDTIN_DELIMITERS_RE = /[\s.]/;
|
|
156
|
+
function buildConfigsAndAliases(delimiters, flags) {
|
|
157
|
+
const configs = /* @__PURE__ */ new Map();
|
|
158
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
159
|
+
const isNameInvalid = (name) => delimiters.some((char) => name.includes(char)) || BUILDTIN_DELIMITERS_RE.test(name);
|
|
160
|
+
function validateFlagOptions(name, options) {
|
|
161
|
+
const prefix = `Flag "${name}"`;
|
|
162
|
+
if (Array.isArray(options.type) && options.type.length > 1) throw new InvalidSchemaError(`${prefix} has an invalid type array. Only single-element arrays are allowed to denote multiple occurrences.`);
|
|
163
|
+
const names = [name];
|
|
164
|
+
if (options.alias) names.push(...toArray(options.alias));
|
|
165
|
+
if (names.some(isNameInvalid)) throw new InvalidSchemaError(`${prefix} contains reserved characters, which are used as delimiters.`);
|
|
166
|
+
}
|
|
167
|
+
for (const [name, config] of Object.entries(flags)) {
|
|
168
|
+
const normalized = normalizeConfig(config);
|
|
169
|
+
validateFlagOptions(name, normalized);
|
|
170
|
+
configs.set(name, normalized);
|
|
171
|
+
aliases.set(name, name);
|
|
172
|
+
aliases.set(camelCase(name), name);
|
|
173
|
+
if (normalized.alias) {
|
|
174
|
+
const list = Array.isArray(normalized.alias) ? normalized.alias : [normalized.alias];
|
|
175
|
+
for (const a of list) aliases.set(a, name);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
configs,
|
|
180
|
+
aliases
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
113
184
|
//#endregion
|
|
114
185
|
//#region src/utils.ts
|
|
115
|
-
const looseIsArray = (arr) => Array.isArray(arr);
|
|
116
186
|
const isArrayOfType = (arr, type) => Array.isArray(arr) && arr[0] === type;
|
|
117
187
|
/**
|
|
118
188
|
* Check if it's a letter (a-z: 97-122, A-Z: 65-90)
|
|
@@ -163,43 +233,6 @@ function splitNameAndValue(arg, delimiters) {
|
|
|
163
233
|
};
|
|
164
234
|
}
|
|
165
235
|
|
|
166
|
-
//#endregion
|
|
167
|
-
//#region src/config.ts
|
|
168
|
-
const defaultParserOptions = { delimiters: ["=", ":"] };
|
|
169
|
-
const resolveParserOptions = (options = {}) => ({
|
|
170
|
-
...defaultParserOptions,
|
|
171
|
-
...options
|
|
172
|
-
});
|
|
173
|
-
const normalizeConfig = (config) => typeof config === "function" || looseIsArray(config) ? { type: config } : config;
|
|
174
|
-
const BUILDTIN_DELIMITERS_RE = /[\s.]/;
|
|
175
|
-
function buildConfigsAndAliases(delimiters, flags) {
|
|
176
|
-
const configs = /* @__PURE__ */ new Map();
|
|
177
|
-
const aliases = /* @__PURE__ */ new Map();
|
|
178
|
-
const isNameInvalid = (name) => delimiters.some((char) => name.includes(char)) || BUILDTIN_DELIMITERS_RE.test(name);
|
|
179
|
-
function validateFlagOptions(name, options) {
|
|
180
|
-
const prefix = `Flag "${name}"`;
|
|
181
|
-
if (Array.isArray(options.type) && options.type.length > 1) throw new InvalidSchemaError(`${prefix} has an invalid type array. Only single-element arrays are allowed to denote multiple occurrences.`);
|
|
182
|
-
const names = [name];
|
|
183
|
-
if (options.alias) names.push(...toArray(options.alias));
|
|
184
|
-
if (names.some(isNameInvalid)) throw new InvalidSchemaError(`${prefix} contains reserved characters, which are used as delimiters.`);
|
|
185
|
-
}
|
|
186
|
-
for (const [name, config] of Object.entries(flags)) {
|
|
187
|
-
const normalized = normalizeConfig(config);
|
|
188
|
-
validateFlagOptions(name, normalized);
|
|
189
|
-
configs.set(name, normalized);
|
|
190
|
-
aliases.set(name, name);
|
|
191
|
-
aliases.set(camelCase(name), name);
|
|
192
|
-
if (normalized.alias) {
|
|
193
|
-
const list = Array.isArray(normalized.alias) ? normalized.alias : [normalized.alias];
|
|
194
|
-
for (const a of list) aliases.set(a, name);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return {
|
|
198
|
-
configs,
|
|
199
|
-
aliases
|
|
200
|
-
};
|
|
201
|
-
}
|
|
202
|
-
|
|
203
236
|
//#endregion
|
|
204
237
|
//#region src/parse.ts
|
|
205
238
|
const DOUBLE_DASH = "--";
|
|
@@ -348,4 +381,4 @@ function createParser(options = {}) {
|
|
|
348
381
|
const parse = (args, options = {}) => createParser(options).parse(args);
|
|
349
382
|
|
|
350
383
|
//#endregion
|
|
351
|
-
export { DOUBLE_DASH, Enum, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, PARAMETER, UNKNOWN_FLAG, createParser, parse };
|
|
384
|
+
export { DOUBLE_DASH, Enum, InvalidSchemaError, KNOWN_FLAG, MissingRequiredFlagError, PARAMETER, Range, Regex, UNKNOWN_FLAG, createParser, parse };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/parser",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.18",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc parser",
|
|
@@ -56,6 +56,6 @@
|
|
|
56
56
|
"nopt": "^9.0.0",
|
|
57
57
|
"type-flag": "^4.0.3",
|
|
58
58
|
"yargs-parser": "^22.0.0",
|
|
59
|
-
"@clerc/utils": "1.0.0-beta.
|
|
59
|
+
"@clerc/utils": "1.0.0-beta.18"
|
|
60
60
|
}
|
|
61
61
|
}
|