@optique/core 1.0.0-dev.1547 → 1.0.0-dev.1555
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/constructs.cjs +60 -5
- package/dist/constructs.js +60 -5
- package/dist/dependency.cjs +34 -0
- package/dist/dependency.js +34 -0
- package/dist/facade.cjs +1 -0
- package/dist/facade.d.cts +7 -5
- package/dist/facade.d.ts +7 -5
- package/dist/facade.js +2 -1
- package/dist/modifiers.cjs +73 -9
- package/dist/modifiers.js +73 -9
- package/dist/parser.d.cts +20 -0
- package/dist/parser.d.ts +20 -0
- package/dist/primitives.cjs +33 -0
- package/dist/primitives.js +33 -0
- package/dist/program.cjs +4 -0
- package/dist/program.d.cts +2 -0
- package/dist/program.d.ts +2 -0
- package/dist/program.js +5 -0
- package/dist/valueparser.cjs +246 -30
- package/dist/valueparser.d.cts +27 -0
- package/dist/valueparser.d.ts +27 -0
- package/dist/valueparser.js +246 -30
- package/package.json +1 -1
package/dist/constructs.cjs
CHANGED
|
@@ -491,7 +491,7 @@ function or(...args) {
|
|
|
491
491
|
success: false
|
|
492
492
|
};
|
|
493
493
|
};
|
|
494
|
-
|
|
494
|
+
const singleResult = {
|
|
495
495
|
$mode: combinedMode,
|
|
496
496
|
$valueType: [],
|
|
497
497
|
$stateType: [],
|
|
@@ -535,6 +535,7 @@ function or(...args) {
|
|
|
535
535
|
};
|
|
536
536
|
}
|
|
537
537
|
};
|
|
538
|
+
return singleResult;
|
|
538
539
|
}
|
|
539
540
|
/**
|
|
540
541
|
* @since 0.5.0
|
|
@@ -630,7 +631,7 @@ function longestMatch(...args) {
|
|
|
630
631
|
success: false
|
|
631
632
|
};
|
|
632
633
|
};
|
|
633
|
-
|
|
634
|
+
const multiResult = {
|
|
634
635
|
$mode: combinedMode,
|
|
635
636
|
$valueType: [],
|
|
636
637
|
$stateType: [],
|
|
@@ -681,6 +682,7 @@ function longestMatch(...args) {
|
|
|
681
682
|
};
|
|
682
683
|
}
|
|
683
684
|
};
|
|
685
|
+
return multiResult;
|
|
684
686
|
}
|
|
685
687
|
/**
|
|
686
688
|
* Internal sync helper for object suggest functionality.
|
|
@@ -1219,7 +1221,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1219
1221
|
success: false
|
|
1220
1222
|
};
|
|
1221
1223
|
};
|
|
1222
|
-
|
|
1224
|
+
const objectParser = {
|
|
1223
1225
|
$mode: combinedMode,
|
|
1224
1226
|
$valueType: [],
|
|
1225
1227
|
$stateType: [],
|
|
@@ -1423,6 +1425,28 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1423
1425
|
})) };
|
|
1424
1426
|
}
|
|
1425
1427
|
};
|
|
1428
|
+
const fieldNormalizers = [];
|
|
1429
|
+
for (const [key, fieldParser] of parserPairs) if (typeof fieldParser.normalizeValue === "function") fieldNormalizers.push([key, fieldParser.normalizeValue.bind(fieldParser)]);
|
|
1430
|
+
if (fieldNormalizers.length > 0) Object.defineProperty(objectParser, "normalizeValue", {
|
|
1431
|
+
value(obj) {
|
|
1432
|
+
if (typeof obj !== "object" || obj == null) return obj;
|
|
1433
|
+
let changed = false;
|
|
1434
|
+
let result;
|
|
1435
|
+
for (const [key, normalize] of fieldNormalizers) if (Object.hasOwn(obj, key)) try {
|
|
1436
|
+
const original = obj[key];
|
|
1437
|
+
const normalized = normalize(original);
|
|
1438
|
+
if (normalized !== original) {
|
|
1439
|
+
if (!result) result = { ...obj };
|
|
1440
|
+
result[key] = normalized;
|
|
1441
|
+
changed = true;
|
|
1442
|
+
}
|
|
1443
|
+
} catch {}
|
|
1444
|
+
return changed ? result : obj;
|
|
1445
|
+
},
|
|
1446
|
+
configurable: true,
|
|
1447
|
+
enumerable: false
|
|
1448
|
+
});
|
|
1449
|
+
return objectParser;
|
|
1426
1450
|
}
|
|
1427
1451
|
function suggestTupleSync(context, prefix, parsers) {
|
|
1428
1452
|
const suggestions = [];
|
|
@@ -1594,7 +1618,7 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1594
1618
|
consumed: allConsumed
|
|
1595
1619
|
};
|
|
1596
1620
|
};
|
|
1597
|
-
|
|
1621
|
+
const tupleParser = {
|
|
1598
1622
|
$mode: combinedMode,
|
|
1599
1623
|
$valueType: [],
|
|
1600
1624
|
$stateType: [],
|
|
@@ -1768,6 +1792,31 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1768
1792
|
return label ? `tuple(${JSON.stringify(label)}, ${parsersStr})` : `tuple(${parsersStr})`;
|
|
1769
1793
|
}
|
|
1770
1794
|
};
|
|
1795
|
+
const tupleNormalizers = [];
|
|
1796
|
+
for (let i = 0; i < parsers.length; i++) {
|
|
1797
|
+
const p = parsers[i];
|
|
1798
|
+
if (typeof p.normalizeValue === "function") tupleNormalizers.push([i, p.normalizeValue.bind(p)]);
|
|
1799
|
+
}
|
|
1800
|
+
if (tupleNormalizers.length > 0) Object.defineProperty(tupleParser, "normalizeValue", {
|
|
1801
|
+
value(arr) {
|
|
1802
|
+
if (!Array.isArray(arr)) return arr;
|
|
1803
|
+
let changed = false;
|
|
1804
|
+
let result;
|
|
1805
|
+
for (const [idx, normalize] of tupleNormalizers) if (idx < arr.length && Object.hasOwn(arr, idx)) try {
|
|
1806
|
+
const original = arr[idx];
|
|
1807
|
+
const normalized = normalize(original);
|
|
1808
|
+
if (normalized !== original) {
|
|
1809
|
+
if (!result) result = [...arr];
|
|
1810
|
+
result[idx] = normalized;
|
|
1811
|
+
changed = true;
|
|
1812
|
+
}
|
|
1813
|
+
} catch {}
|
|
1814
|
+
return changed ? result : arr;
|
|
1815
|
+
},
|
|
1816
|
+
configurable: true,
|
|
1817
|
+
enumerable: false
|
|
1818
|
+
});
|
|
1819
|
+
return tupleParser;
|
|
1771
1820
|
}
|
|
1772
1821
|
function merge(...args) {
|
|
1773
1822
|
const label = typeof args[0] === "string" ? args[0] : void 0;
|
|
@@ -1915,7 +1964,7 @@ function merge(...args) {
|
|
|
1915
1964
|
};
|
|
1916
1965
|
};
|
|
1917
1966
|
const mergedFieldParsers = collectChildFieldParsers(parsers);
|
|
1918
|
-
|
|
1967
|
+
const mergeParser = {
|
|
1919
1968
|
$mode: combinedMode,
|
|
1920
1969
|
$valueType: [],
|
|
1921
1970
|
$stateType: [],
|
|
@@ -2134,6 +2183,7 @@ function merge(...args) {
|
|
|
2134
2183
|
};
|
|
2135
2184
|
}
|
|
2136
2185
|
};
|
|
2186
|
+
return mergeParser;
|
|
2137
2187
|
}
|
|
2138
2188
|
/**
|
|
2139
2189
|
* Builds a dependency registry from the pre-parsed context state and returns
|
|
@@ -2597,6 +2647,11 @@ function group(label, parser, options = {}) {
|
|
|
2597
2647
|
configurable: true,
|
|
2598
2648
|
enumerable: false
|
|
2599
2649
|
});
|
|
2650
|
+
if (typeof parser.normalizeValue === "function") Object.defineProperty(groupParser, "normalizeValue", {
|
|
2651
|
+
value: parser.normalizeValue.bind(parser),
|
|
2652
|
+
configurable: true,
|
|
2653
|
+
enumerable: false
|
|
2654
|
+
});
|
|
2600
2655
|
return groupParser;
|
|
2601
2656
|
}
|
|
2602
2657
|
/**
|
package/dist/constructs.js
CHANGED
|
@@ -491,7 +491,7 @@ function or(...args) {
|
|
|
491
491
|
success: false
|
|
492
492
|
};
|
|
493
493
|
};
|
|
494
|
-
|
|
494
|
+
const singleResult = {
|
|
495
495
|
$mode: combinedMode,
|
|
496
496
|
$valueType: [],
|
|
497
497
|
$stateType: [],
|
|
@@ -535,6 +535,7 @@ function or(...args) {
|
|
|
535
535
|
};
|
|
536
536
|
}
|
|
537
537
|
};
|
|
538
|
+
return singleResult;
|
|
538
539
|
}
|
|
539
540
|
/**
|
|
540
541
|
* @since 0.5.0
|
|
@@ -630,7 +631,7 @@ function longestMatch(...args) {
|
|
|
630
631
|
success: false
|
|
631
632
|
};
|
|
632
633
|
};
|
|
633
|
-
|
|
634
|
+
const multiResult = {
|
|
634
635
|
$mode: combinedMode,
|
|
635
636
|
$valueType: [],
|
|
636
637
|
$stateType: [],
|
|
@@ -681,6 +682,7 @@ function longestMatch(...args) {
|
|
|
681
682
|
};
|
|
682
683
|
}
|
|
683
684
|
};
|
|
685
|
+
return multiResult;
|
|
684
686
|
}
|
|
685
687
|
/**
|
|
686
688
|
* Internal sync helper for object suggest functionality.
|
|
@@ -1219,7 +1221,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1219
1221
|
success: false
|
|
1220
1222
|
};
|
|
1221
1223
|
};
|
|
1222
|
-
|
|
1224
|
+
const objectParser = {
|
|
1223
1225
|
$mode: combinedMode,
|
|
1224
1226
|
$valueType: [],
|
|
1225
1227
|
$stateType: [],
|
|
@@ -1423,6 +1425,28 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1423
1425
|
})) };
|
|
1424
1426
|
}
|
|
1425
1427
|
};
|
|
1428
|
+
const fieldNormalizers = [];
|
|
1429
|
+
for (const [key, fieldParser] of parserPairs) if (typeof fieldParser.normalizeValue === "function") fieldNormalizers.push([key, fieldParser.normalizeValue.bind(fieldParser)]);
|
|
1430
|
+
if (fieldNormalizers.length > 0) Object.defineProperty(objectParser, "normalizeValue", {
|
|
1431
|
+
value(obj) {
|
|
1432
|
+
if (typeof obj !== "object" || obj == null) return obj;
|
|
1433
|
+
let changed = false;
|
|
1434
|
+
let result;
|
|
1435
|
+
for (const [key, normalize] of fieldNormalizers) if (Object.hasOwn(obj, key)) try {
|
|
1436
|
+
const original = obj[key];
|
|
1437
|
+
const normalized = normalize(original);
|
|
1438
|
+
if (normalized !== original) {
|
|
1439
|
+
if (!result) result = { ...obj };
|
|
1440
|
+
result[key] = normalized;
|
|
1441
|
+
changed = true;
|
|
1442
|
+
}
|
|
1443
|
+
} catch {}
|
|
1444
|
+
return changed ? result : obj;
|
|
1445
|
+
},
|
|
1446
|
+
configurable: true,
|
|
1447
|
+
enumerable: false
|
|
1448
|
+
});
|
|
1449
|
+
return objectParser;
|
|
1426
1450
|
}
|
|
1427
1451
|
function suggestTupleSync(context, prefix, parsers) {
|
|
1428
1452
|
const suggestions = [];
|
|
@@ -1594,7 +1618,7 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1594
1618
|
consumed: allConsumed
|
|
1595
1619
|
};
|
|
1596
1620
|
};
|
|
1597
|
-
|
|
1621
|
+
const tupleParser = {
|
|
1598
1622
|
$mode: combinedMode,
|
|
1599
1623
|
$valueType: [],
|
|
1600
1624
|
$stateType: [],
|
|
@@ -1768,6 +1792,31 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1768
1792
|
return label ? `tuple(${JSON.stringify(label)}, ${parsersStr})` : `tuple(${parsersStr})`;
|
|
1769
1793
|
}
|
|
1770
1794
|
};
|
|
1795
|
+
const tupleNormalizers = [];
|
|
1796
|
+
for (let i = 0; i < parsers.length; i++) {
|
|
1797
|
+
const p = parsers[i];
|
|
1798
|
+
if (typeof p.normalizeValue === "function") tupleNormalizers.push([i, p.normalizeValue.bind(p)]);
|
|
1799
|
+
}
|
|
1800
|
+
if (tupleNormalizers.length > 0) Object.defineProperty(tupleParser, "normalizeValue", {
|
|
1801
|
+
value(arr) {
|
|
1802
|
+
if (!Array.isArray(arr)) return arr;
|
|
1803
|
+
let changed = false;
|
|
1804
|
+
let result;
|
|
1805
|
+
for (const [idx, normalize] of tupleNormalizers) if (idx < arr.length && Object.hasOwn(arr, idx)) try {
|
|
1806
|
+
const original = arr[idx];
|
|
1807
|
+
const normalized = normalize(original);
|
|
1808
|
+
if (normalized !== original) {
|
|
1809
|
+
if (!result) result = [...arr];
|
|
1810
|
+
result[idx] = normalized;
|
|
1811
|
+
changed = true;
|
|
1812
|
+
}
|
|
1813
|
+
} catch {}
|
|
1814
|
+
return changed ? result : arr;
|
|
1815
|
+
},
|
|
1816
|
+
configurable: true,
|
|
1817
|
+
enumerable: false
|
|
1818
|
+
});
|
|
1819
|
+
return tupleParser;
|
|
1771
1820
|
}
|
|
1772
1821
|
function merge(...args) {
|
|
1773
1822
|
const label = typeof args[0] === "string" ? args[0] : void 0;
|
|
@@ -1915,7 +1964,7 @@ function merge(...args) {
|
|
|
1915
1964
|
};
|
|
1916
1965
|
};
|
|
1917
1966
|
const mergedFieldParsers = collectChildFieldParsers(parsers);
|
|
1918
|
-
|
|
1967
|
+
const mergeParser = {
|
|
1919
1968
|
$mode: combinedMode,
|
|
1920
1969
|
$valueType: [],
|
|
1921
1970
|
$stateType: [],
|
|
@@ -2134,6 +2183,7 @@ function merge(...args) {
|
|
|
2134
2183
|
};
|
|
2135
2184
|
}
|
|
2136
2185
|
};
|
|
2186
|
+
return mergeParser;
|
|
2137
2187
|
}
|
|
2138
2188
|
/**
|
|
2139
2189
|
* Builds a dependency registry from the pre-parsed context state and returns
|
|
@@ -2597,6 +2647,11 @@ function group(label, parser, options = {}) {
|
|
|
2597
2647
|
configurable: true,
|
|
2598
2648
|
enumerable: false
|
|
2599
2649
|
});
|
|
2650
|
+
if (typeof parser.normalizeValue === "function") Object.defineProperty(groupParser, "normalizeValue", {
|
|
2651
|
+
value: parser.normalizeValue.bind(parser),
|
|
2652
|
+
configurable: true,
|
|
2653
|
+
enumerable: false
|
|
2654
|
+
});
|
|
2600
2655
|
return groupParser;
|
|
2601
2656
|
}
|
|
2602
2657
|
/**
|
package/dist/dependency.cjs
CHANGED
|
@@ -199,6 +199,25 @@ function deriveFromAsync(options) {
|
|
|
199
199
|
function isAsyncModeParser(parser) {
|
|
200
200
|
return parser.$mode === "async";
|
|
201
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Shared helper for derived value parser `normalize()` implementations.
|
|
204
|
+
* Builds the inner parser from the factory and delegates to its
|
|
205
|
+
* `normalize()` if available, falling back to the original value on error.
|
|
206
|
+
*/
|
|
207
|
+
function normalizeWithDerivedParser(value, getParser) {
|
|
208
|
+
let derivedParser;
|
|
209
|
+
try {
|
|
210
|
+
derivedParser = getParser();
|
|
211
|
+
} catch {
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
if (derivedParser && typeof derivedParser.normalize === "function") try {
|
|
215
|
+
return derivedParser.normalize(value);
|
|
216
|
+
} catch {
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
return value;
|
|
220
|
+
}
|
|
202
221
|
function createSyncDerivedFromParser(sourceId, options) {
|
|
203
222
|
const alldependencyIds = options.dependencies.map((dep) => dep[dependencyId]);
|
|
204
223
|
return {
|
|
@@ -271,6 +290,9 @@ function createSyncDerivedFromParser(sourceId, options) {
|
|
|
271
290
|
if (isAsyncModeParser(derivedParser) || !derivedParser.suggest) return;
|
|
272
291
|
yield* derivedParser.suggest(prefix);
|
|
273
292
|
},
|
|
293
|
+
normalize(value) {
|
|
294
|
+
return normalizeWithDerivedParser(value, () => options.factory(...options.defaultValues()));
|
|
295
|
+
},
|
|
274
296
|
*[suggestWithDependency](prefix, dependencyValue) {
|
|
275
297
|
let derivedParser;
|
|
276
298
|
try {
|
|
@@ -345,6 +367,9 @@ function createAsyncDerivedFromParserFromAsyncFactory(sourceId, options) {
|
|
|
345
367
|
}
|
|
346
368
|
return derivedParser.format(value);
|
|
347
369
|
},
|
|
370
|
+
normalize(value) {
|
|
371
|
+
return normalizeWithDerivedParser(value, () => options.factory(...options.defaultValues()));
|
|
372
|
+
},
|
|
348
373
|
async *suggest(prefix) {
|
|
349
374
|
let derivedParser;
|
|
350
375
|
try {
|
|
@@ -428,6 +453,9 @@ function createAsyncDerivedFromParserFromSyncFactory(sourceId, options) {
|
|
|
428
453
|
}
|
|
429
454
|
return derivedParser.format(value);
|
|
430
455
|
},
|
|
456
|
+
normalize(value) {
|
|
457
|
+
return normalizeWithDerivedParser(value, () => options.factory(...options.defaultValues()));
|
|
458
|
+
},
|
|
431
459
|
async *suggest(prefix) {
|
|
432
460
|
let derivedParser;
|
|
433
461
|
try {
|
|
@@ -521,6 +549,9 @@ function createSyncDerivedParser(sourceId, options) {
|
|
|
521
549
|
}
|
|
522
550
|
return derivedParser.format(value);
|
|
523
551
|
},
|
|
552
|
+
normalize(value) {
|
|
553
|
+
return normalizeWithDerivedParser(value, () => options.factory(options.defaultValue()));
|
|
554
|
+
},
|
|
524
555
|
*suggest(prefix) {
|
|
525
556
|
let derivedParser;
|
|
526
557
|
try {
|
|
@@ -681,6 +712,9 @@ function createAsyncDerivedParserFromSyncFactory(sourceId, options) {
|
|
|
681
712
|
}
|
|
682
713
|
return derivedParser.format(value);
|
|
683
714
|
},
|
|
715
|
+
normalize(value) {
|
|
716
|
+
return normalizeWithDerivedParser(value, () => options.factory(options.defaultValue()));
|
|
717
|
+
},
|
|
684
718
|
async *suggest(prefix) {
|
|
685
719
|
let derivedParser;
|
|
686
720
|
try {
|
package/dist/dependency.js
CHANGED
|
@@ -199,6 +199,25 @@ function deriveFromAsync(options) {
|
|
|
199
199
|
function isAsyncModeParser(parser) {
|
|
200
200
|
return parser.$mode === "async";
|
|
201
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Shared helper for derived value parser `normalize()` implementations.
|
|
204
|
+
* Builds the inner parser from the factory and delegates to its
|
|
205
|
+
* `normalize()` if available, falling back to the original value on error.
|
|
206
|
+
*/
|
|
207
|
+
function normalizeWithDerivedParser(value, getParser) {
|
|
208
|
+
let derivedParser;
|
|
209
|
+
try {
|
|
210
|
+
derivedParser = getParser();
|
|
211
|
+
} catch {
|
|
212
|
+
return value;
|
|
213
|
+
}
|
|
214
|
+
if (derivedParser && typeof derivedParser.normalize === "function") try {
|
|
215
|
+
return derivedParser.normalize(value);
|
|
216
|
+
} catch {
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
return value;
|
|
220
|
+
}
|
|
202
221
|
function createSyncDerivedFromParser(sourceId, options) {
|
|
203
222
|
const alldependencyIds = options.dependencies.map((dep) => dep[dependencyId]);
|
|
204
223
|
return {
|
|
@@ -271,6 +290,9 @@ function createSyncDerivedFromParser(sourceId, options) {
|
|
|
271
290
|
if (isAsyncModeParser(derivedParser) || !derivedParser.suggest) return;
|
|
272
291
|
yield* derivedParser.suggest(prefix);
|
|
273
292
|
},
|
|
293
|
+
normalize(value) {
|
|
294
|
+
return normalizeWithDerivedParser(value, () => options.factory(...options.defaultValues()));
|
|
295
|
+
},
|
|
274
296
|
*[suggestWithDependency](prefix, dependencyValue) {
|
|
275
297
|
let derivedParser;
|
|
276
298
|
try {
|
|
@@ -345,6 +367,9 @@ function createAsyncDerivedFromParserFromAsyncFactory(sourceId, options) {
|
|
|
345
367
|
}
|
|
346
368
|
return derivedParser.format(value);
|
|
347
369
|
},
|
|
370
|
+
normalize(value) {
|
|
371
|
+
return normalizeWithDerivedParser(value, () => options.factory(...options.defaultValues()));
|
|
372
|
+
},
|
|
348
373
|
async *suggest(prefix) {
|
|
349
374
|
let derivedParser;
|
|
350
375
|
try {
|
|
@@ -428,6 +453,9 @@ function createAsyncDerivedFromParserFromSyncFactory(sourceId, options) {
|
|
|
428
453
|
}
|
|
429
454
|
return derivedParser.format(value);
|
|
430
455
|
},
|
|
456
|
+
normalize(value) {
|
|
457
|
+
return normalizeWithDerivedParser(value, () => options.factory(...options.defaultValues()));
|
|
458
|
+
},
|
|
431
459
|
async *suggest(prefix) {
|
|
432
460
|
let derivedParser;
|
|
433
461
|
try {
|
|
@@ -521,6 +549,9 @@ function createSyncDerivedParser(sourceId, options) {
|
|
|
521
549
|
}
|
|
522
550
|
return derivedParser.format(value);
|
|
523
551
|
},
|
|
552
|
+
normalize(value) {
|
|
553
|
+
return normalizeWithDerivedParser(value, () => options.factory(options.defaultValue()));
|
|
554
|
+
},
|
|
524
555
|
*suggest(prefix) {
|
|
525
556
|
let derivedParser;
|
|
526
557
|
try {
|
|
@@ -681,6 +712,9 @@ function createAsyncDerivedParserFromSyncFactory(sourceId, options) {
|
|
|
681
712
|
}
|
|
682
713
|
return derivedParser.format(value);
|
|
683
714
|
},
|
|
715
|
+
normalize(value) {
|
|
716
|
+
return normalizeWithDerivedParser(value, () => options.factory(options.defaultValue()));
|
|
717
|
+
},
|
|
684
718
|
async *suggest(prefix) {
|
|
685
719
|
let derivedParser;
|
|
686
720
|
try {
|
package/dist/facade.cjs
CHANGED
|
@@ -563,6 +563,7 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
|
|
|
563
563
|
args = argsOrOptions;
|
|
564
564
|
options = optionsParam ?? {};
|
|
565
565
|
}
|
|
566
|
+
require_validate.validateProgramName(programName);
|
|
566
567
|
const { colors, maxWidth, showDefault, showChoices, sectionOrder, aboveError = "usage", onError = () => {
|
|
567
568
|
throw new RunParserError("Failed to parse command line arguments.");
|
|
568
569
|
}, stderr = console.error, stdout = console.log, brief, description, examples, author, bugs, footer } = options;
|
package/dist/facade.d.cts
CHANGED
|
@@ -272,11 +272,13 @@ interface RunOptions<THelp, TError> {
|
|
|
272
272
|
* @param options Configuration options for output formatting and callbacks.
|
|
273
273
|
* @returns The parsed result value, or the return value of `onHelp`/`onError`
|
|
274
274
|
* callbacks.
|
|
275
|
-
* @throws {TypeError} If `
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
275
|
+
* @throws {TypeError} If `programName` (or `program.metadata.name`) is not
|
|
276
|
+
* a string, is empty, is whitespace-only, or contains control
|
|
277
|
+
* characters. Also thrown if `options.version.value` is not a
|
|
278
|
+
* non-empty string without ASCII control characters, or if any
|
|
279
|
+
* meta command/option name is empty, whitespace-only, contains
|
|
280
|
+
* whitespace or control characters, or (for option names) lacks a
|
|
281
|
+
* valid prefix (`--`, `-`, `/`, or `+`).
|
|
280
282
|
* @throws {RunParserError} When parsing fails and no `onError` callback is
|
|
281
283
|
* provided.
|
|
282
284
|
* @since 0.10.0 Added support for {@link Program} objects.
|
package/dist/facade.d.ts
CHANGED
|
@@ -272,11 +272,13 @@ interface RunOptions<THelp, TError> {
|
|
|
272
272
|
* @param options Configuration options for output formatting and callbacks.
|
|
273
273
|
* @returns The parsed result value, or the return value of `onHelp`/`onError`
|
|
274
274
|
* callbacks.
|
|
275
|
-
* @throws {TypeError} If `
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
*
|
|
275
|
+
* @throws {TypeError} If `programName` (or `program.metadata.name`) is not
|
|
276
|
+
* a string, is empty, is whitespace-only, or contains control
|
|
277
|
+
* characters. Also thrown if `options.version.value` is not a
|
|
278
|
+
* non-empty string without ASCII control characters, or if any
|
|
279
|
+
* meta command/option name is empty, whitespace-only, contains
|
|
280
|
+
* whitespace or control characters, or (for option names) lacks a
|
|
281
|
+
* valid prefix (`--`, `-`, `/`, or `+`).
|
|
280
282
|
* @throws {RunParserError} When parsing fails and no `onError` callback is
|
|
281
283
|
* provided.
|
|
282
284
|
* @since 0.10.0 Added support for {@link Program} objects.
|
package/dist/facade.js
CHANGED
|
@@ -2,7 +2,7 @@ import { injectAnnotations } from "./annotations.js";
|
|
|
2
2
|
import { commandLine, formatMessage, lineBreak, message, optionName, text, value } from "./message.js";
|
|
3
3
|
import { bash, fish, nu, pwsh, zsh } from "./completion.js";
|
|
4
4
|
import { dispatchByMode } from "./mode-dispatch.js";
|
|
5
|
-
import { validateCommandNames, validateMetaNameCollisions, validateOptionNames } from "./validate.js";
|
|
5
|
+
import { validateCommandNames, validateMetaNameCollisions, validateOptionNames, validateProgramName } from "./validate.js";
|
|
6
6
|
import { extractCommandNames, extractLiteralValues, extractOptionNames, formatUsage } from "./usage.js";
|
|
7
7
|
import { formatDocPage } from "./doc.js";
|
|
8
8
|
import { group, longestMatch, object } from "./constructs.js";
|
|
@@ -563,6 +563,7 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
|
|
|
563
563
|
args = argsOrOptions;
|
|
564
564
|
options = optionsParam ?? {};
|
|
565
565
|
}
|
|
566
|
+
validateProgramName(programName);
|
|
566
567
|
const { colors, maxWidth, showDefault, showChoices, sectionOrder, aboveError = "usage", onError = () => {
|
|
567
568
|
throw new RunParserError("Failed to parse command line arguments.");
|
|
568
569
|
}, stderr = console.error, stdout = console.log, brief, description, examples, author, bugs, footer } = options;
|
package/dist/modifiers.cjs
CHANGED
|
@@ -118,7 +118,7 @@ function optional(parser) {
|
|
|
118
118
|
const wrappedDependencyMarker = innerHasWrappedDependency ? { [require_dependency.wrappedDependencySourceMarker]: parser[require_dependency.wrappedDependencySourceMarker] } : innerHasDirectDependency ? { [require_dependency.wrappedDependencySourceMarker]: syncParser.initialState } : {};
|
|
119
119
|
const hasWrappedDependencySource = require_dependency.wrappedDependencySourceMarker in wrappedDependencyMarker;
|
|
120
120
|
const wrappedPendingState = hasWrappedDependencySource ? wrappedDependencyMarker[require_dependency.wrappedDependencySourceMarker] : void 0;
|
|
121
|
-
|
|
121
|
+
const optionalParser = {
|
|
122
122
|
$mode: parser.$mode,
|
|
123
123
|
$valueType: [],
|
|
124
124
|
$stateType: [],
|
|
@@ -175,6 +175,18 @@ function optional(parser) {
|
|
|
175
175
|
return syncParser.getDocFragments(innerState, defaultValue);
|
|
176
176
|
}
|
|
177
177
|
};
|
|
178
|
+
if (typeof parser.normalizeValue === "function") {
|
|
179
|
+
const innerNormalize = parser.normalizeValue.bind(parser);
|
|
180
|
+
Object.defineProperty(optionalParser, "normalizeValue", {
|
|
181
|
+
value(v) {
|
|
182
|
+
if (v == null) return v;
|
|
183
|
+
return innerNormalize(v);
|
|
184
|
+
},
|
|
185
|
+
configurable: true,
|
|
186
|
+
enumerable: false
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
return optionalParser;
|
|
178
190
|
}
|
|
179
191
|
/**
|
|
180
192
|
* Error type for structured error messages in {@link withDefault} default value callbacks.
|
|
@@ -257,12 +269,19 @@ function withDefault(parser, defaultValue, options) {
|
|
|
257
269
|
return require_mode_dispatch.dispatchByMode(parser.$mode, () => parseOptionalStyleSync(context, syncParser), () => parseOptionalStyleAsync(context, parser));
|
|
258
270
|
},
|
|
259
271
|
complete(state) {
|
|
272
|
+
function evaluateDefault() {
|
|
273
|
+
const raw = typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
|
274
|
+
if (typeof parser.normalizeValue === "function") try {
|
|
275
|
+
return parser.normalizeValue(raw);
|
|
276
|
+
} catch {}
|
|
277
|
+
return raw;
|
|
278
|
+
}
|
|
260
279
|
if (!Array.isArray(state)) {
|
|
261
280
|
if (require_dependency.transformsDependencyValue(parser)) {
|
|
262
281
|
const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(void 0), () => parser.complete(void 0));
|
|
263
282
|
const handleInnerResult = (res) => {
|
|
264
283
|
if (require_dependency.isDependencySourceState(res)) try {
|
|
265
|
-
const value =
|
|
284
|
+
const value = evaluateDefault();
|
|
266
285
|
return require_dependency.createDependencySourceState({
|
|
267
286
|
success: true,
|
|
268
287
|
value
|
|
@@ -274,7 +293,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
274
293
|
};
|
|
275
294
|
}
|
|
276
295
|
try {
|
|
277
|
-
const value =
|
|
296
|
+
const value = evaluateDefault();
|
|
278
297
|
return {
|
|
279
298
|
success: true,
|
|
280
299
|
value
|
|
@@ -289,7 +308,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
289
308
|
return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, handleInnerResult);
|
|
290
309
|
}
|
|
291
310
|
if (require_dependency.isWrappedDependencySource(parser)) try {
|
|
292
|
-
const value =
|
|
311
|
+
const value = evaluateDefault();
|
|
293
312
|
const pendingState = parser[require_dependency.wrappedDependencySourceMarker];
|
|
294
313
|
return require_dependency.createDependencySourceState({
|
|
295
314
|
success: true,
|
|
@@ -306,7 +325,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
306
325
|
return innerResult;
|
|
307
326
|
}
|
|
308
327
|
try {
|
|
309
|
-
const value =
|
|
328
|
+
const value = evaluateDefault();
|
|
310
329
|
return {
|
|
311
330
|
success: true,
|
|
312
331
|
value
|
|
@@ -323,7 +342,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
323
342
|
const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(state), () => parser.complete(state));
|
|
324
343
|
const handleInnerResult = (res) => {
|
|
325
344
|
if (require_dependency.isDependencySourceState(res)) try {
|
|
326
|
-
const value =
|
|
345
|
+
const value = evaluateDefault();
|
|
327
346
|
return require_dependency.createDependencySourceState({
|
|
328
347
|
success: true,
|
|
329
348
|
value
|
|
@@ -335,7 +354,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
335
354
|
};
|
|
336
355
|
}
|
|
337
356
|
try {
|
|
338
|
-
const value =
|
|
357
|
+
const value = evaluateDefault();
|
|
339
358
|
return {
|
|
340
359
|
success: true,
|
|
341
360
|
value
|
|
@@ -350,7 +369,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
350
369
|
return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, handleInnerResult);
|
|
351
370
|
}
|
|
352
371
|
try {
|
|
353
|
-
const value =
|
|
372
|
+
const value = evaluateDefault();
|
|
354
373
|
return require_dependency.createDependencySourceState({
|
|
355
374
|
success: true,
|
|
356
375
|
value
|
|
@@ -373,7 +392,11 @@ function withDefault(parser, defaultValue, options) {
|
|
|
373
392
|
kind: "available",
|
|
374
393
|
state: state.state[0]
|
|
375
394
|
};
|
|
376
|
-
|
|
395
|
+
let docDefault = getDocDefaultValue(upperDefaultValue);
|
|
396
|
+
if (docDefault != null && typeof parser.normalizeValue === "function") try {
|
|
397
|
+
docDefault = parser.normalizeValue(docDefault);
|
|
398
|
+
} catch {}
|
|
399
|
+
const fragments = syncParser.getDocFragments(innerState, docDefault);
|
|
377
400
|
if (options?.message) {
|
|
378
401
|
const modifiedFragments = fragments.fragments.map((fragment) => {
|
|
379
402
|
if (fragment.type === "entry") return {
|
|
@@ -397,6 +420,20 @@ function withDefault(parser, defaultValue, options) {
|
|
|
397
420
|
configurable: true,
|
|
398
421
|
enumerable: false
|
|
399
422
|
});
|
|
423
|
+
if (typeof parser.normalizeValue === "function") {
|
|
424
|
+
const innerNormalize = parser.normalizeValue.bind(parser);
|
|
425
|
+
Object.defineProperty(withDefaultParser, "normalizeValue", {
|
|
426
|
+
value(v) {
|
|
427
|
+
try {
|
|
428
|
+
return innerNormalize(v);
|
|
429
|
+
} catch {
|
|
430
|
+
return v;
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
configurable: true,
|
|
434
|
+
enumerable: false
|
|
435
|
+
});
|
|
436
|
+
}
|
|
400
437
|
return withDefaultParser;
|
|
401
438
|
}
|
|
402
439
|
/**
|
|
@@ -510,6 +547,7 @@ function map(parser, transform) {
|
|
|
510
547
|
return parser.getDocFragments(state, void 0);
|
|
511
548
|
}
|
|
512
549
|
};
|
|
550
|
+
delete mappedParser.normalizeValue;
|
|
513
551
|
if ("placeholder" in parser) Object.defineProperty(mappedParser, "placeholder", {
|
|
514
552
|
get() {
|
|
515
553
|
try {
|
|
@@ -828,6 +866,27 @@ function multiple(parser, options = {}) {
|
|
|
828
866
|
configurable: true,
|
|
829
867
|
enumerable: false
|
|
830
868
|
});
|
|
869
|
+
if (typeof parser.normalizeValue === "function") {
|
|
870
|
+
const innerNormalize = parser.normalizeValue.bind(parser);
|
|
871
|
+
Object.defineProperty(resultParser, "normalizeValue", {
|
|
872
|
+
value(values) {
|
|
873
|
+
if (!Array.isArray(values)) return values;
|
|
874
|
+
let changed = false;
|
|
875
|
+
const result = values.map((v) => {
|
|
876
|
+
try {
|
|
877
|
+
const n = innerNormalize(v);
|
|
878
|
+
if (n !== v) changed = true;
|
|
879
|
+
return n;
|
|
880
|
+
} catch {
|
|
881
|
+
return v;
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
return changed ? result : values;
|
|
885
|
+
},
|
|
886
|
+
configurable: true,
|
|
887
|
+
enumerable: false
|
|
888
|
+
});
|
|
889
|
+
}
|
|
831
890
|
return resultParser;
|
|
832
891
|
}
|
|
833
892
|
/**
|
|
@@ -916,6 +975,11 @@ function nonEmpty(parser) {
|
|
|
916
975
|
configurable: true,
|
|
917
976
|
enumerable: false
|
|
918
977
|
});
|
|
978
|
+
if (typeof parser.normalizeValue === "function") Object.defineProperty(nonEmptyParser, "normalizeValue", {
|
|
979
|
+
value: parser.normalizeValue.bind(parser),
|
|
980
|
+
configurable: true,
|
|
981
|
+
enumerable: false
|
|
982
|
+
});
|
|
919
983
|
return nonEmptyParser;
|
|
920
984
|
}
|
|
921
985
|
|