@optique/core 0.8.0 → 0.8.2
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 +31 -0
- package/dist/constructs.js +31 -0
- package/dist/facade.cjs +6 -5
- package/dist/facade.js +6 -5
- package/dist/modifiers.d.cts +2 -2
- package/dist/modifiers.d.ts +2 -2
- package/package.json +1 -1
package/dist/constructs.cjs
CHANGED
|
@@ -4,6 +4,27 @@ const require_suggestion = require('./suggestion.cjs');
|
|
|
4
4
|
|
|
5
5
|
//#region src/constructs.ts
|
|
6
6
|
/**
|
|
7
|
+
* Checks if the given token is an option name that requires a value
|
|
8
|
+
* (i.e., has a metavar) within the given usage terms.
|
|
9
|
+
* @param usage The usage terms to search through.
|
|
10
|
+
* @param token The token to check.
|
|
11
|
+
* @returns `true` if the token is an option that requires a value, `false` otherwise.
|
|
12
|
+
*/
|
|
13
|
+
function isOptionRequiringValue(usage, token) {
|
|
14
|
+
function traverse(terms) {
|
|
15
|
+
if (!terms || !Array.isArray(terms)) return false;
|
|
16
|
+
for (const term of terms) if (term.type === "option") {
|
|
17
|
+
if (term.metavar && term.names.includes(token)) return true;
|
|
18
|
+
} else if (term.type === "optional" || term.type === "multiple") {
|
|
19
|
+
if (traverse(term.terms)) return true;
|
|
20
|
+
} else if (term.type === "exclusive") {
|
|
21
|
+
for (const exclusiveUsage of term.terms) if (traverse(exclusiveUsage)) return true;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return traverse(usage);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
7
28
|
* Extracts required (non-optional) usage terms from a usage array.
|
|
8
29
|
* @param usage The usage to extract required terms from
|
|
9
30
|
* @returns Usage containing only required (non-optional) terms
|
|
@@ -442,6 +463,16 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
442
463
|
},
|
|
443
464
|
suggest(context, prefix) {
|
|
444
465
|
const suggestions = [];
|
|
466
|
+
if (context.buffer.length > 0) {
|
|
467
|
+
const lastToken = context.buffer[context.buffer.length - 1];
|
|
468
|
+
for (const [field, parser] of parserPairs) if (isOptionRequiringValue(parser.usage, lastToken)) {
|
|
469
|
+
const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
|
|
470
|
+
return Array.from(parser.suggest({
|
|
471
|
+
...context,
|
|
472
|
+
state: fieldState
|
|
473
|
+
}, prefix));
|
|
474
|
+
}
|
|
475
|
+
}
|
|
445
476
|
for (const [field, parser] of parserPairs) {
|
|
446
477
|
const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
|
|
447
478
|
const fieldSuggestions = parser.suggest({
|
package/dist/constructs.js
CHANGED
|
@@ -4,6 +4,27 @@ import { createErrorWithSuggestions, deduplicateSuggestions } from "./suggestion
|
|
|
4
4
|
|
|
5
5
|
//#region src/constructs.ts
|
|
6
6
|
/**
|
|
7
|
+
* Checks if the given token is an option name that requires a value
|
|
8
|
+
* (i.e., has a metavar) within the given usage terms.
|
|
9
|
+
* @param usage The usage terms to search through.
|
|
10
|
+
* @param token The token to check.
|
|
11
|
+
* @returns `true` if the token is an option that requires a value, `false` otherwise.
|
|
12
|
+
*/
|
|
13
|
+
function isOptionRequiringValue(usage, token) {
|
|
14
|
+
function traverse(terms) {
|
|
15
|
+
if (!terms || !Array.isArray(terms)) return false;
|
|
16
|
+
for (const term of terms) if (term.type === "option") {
|
|
17
|
+
if (term.metavar && term.names.includes(token)) return true;
|
|
18
|
+
} else if (term.type === "optional" || term.type === "multiple") {
|
|
19
|
+
if (traverse(term.terms)) return true;
|
|
20
|
+
} else if (term.type === "exclusive") {
|
|
21
|
+
for (const exclusiveUsage of term.terms) if (traverse(exclusiveUsage)) return true;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return traverse(usage);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
7
28
|
* Extracts required (non-optional) usage terms from a usage array.
|
|
8
29
|
* @param usage The usage to extract required terms from
|
|
9
30
|
* @returns Usage containing only required (non-optional) terms
|
|
@@ -442,6 +463,16 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
442
463
|
},
|
|
443
464
|
suggest(context, prefix) {
|
|
444
465
|
const suggestions = [];
|
|
466
|
+
if (context.buffer.length > 0) {
|
|
467
|
+
const lastToken = context.buffer[context.buffer.length - 1];
|
|
468
|
+
for (const [field, parser] of parserPairs) if (isOptionRequiringValue(parser.usage, lastToken)) {
|
|
469
|
+
const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
|
|
470
|
+
return Array.from(parser.suggest({
|
|
471
|
+
...context,
|
|
472
|
+
state: fieldState
|
|
473
|
+
}, prefix));
|
|
474
|
+
}
|
|
475
|
+
}
|
|
445
476
|
for (const [field, parser] of parserPairs) {
|
|
446
477
|
const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
|
|
447
478
|
const fieldSuggestions = parser.suggest({
|
package/dist/facade.cjs
CHANGED
|
@@ -343,7 +343,7 @@ function classifyResult(result, args) {
|
|
|
343
343
|
* Handles shell completion requests.
|
|
344
344
|
* @since 0.6.0
|
|
345
345
|
*/
|
|
346
|
-
function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode) {
|
|
346
|
+
function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName) {
|
|
347
347
|
const shellName = completionArgs[0] || "";
|
|
348
348
|
const args = completionArgs.slice(1);
|
|
349
349
|
if (!shellName) {
|
|
@@ -375,7 +375,8 @@ function handleCompletion(completionArgs, programName, parser, completionParser,
|
|
|
375
375
|
return onError(1);
|
|
376
376
|
}
|
|
377
377
|
if (args.length === 0) {
|
|
378
|
-
const
|
|
378
|
+
const usePlural = completionName === "plural";
|
|
379
|
+
const completionArg = completionMode === "option" ? usePlural ? "--completions" : "--completion" : usePlural ? "completions" : "completion";
|
|
379
380
|
const script = shell.generateScript(programName, [completionArg, shellName]);
|
|
380
381
|
stdout(script);
|
|
381
382
|
} else {
|
|
@@ -457,7 +458,7 @@ function run(parser, programName, args, options = {}) {
|
|
|
457
458
|
} : createCompletionParser(completion, programName, availableShells, completionName);
|
|
458
459
|
if (options.completion) {
|
|
459
460
|
const hasHelpOption = args.includes("--help");
|
|
460
|
-
if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode);
|
|
461
|
+
if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
|
|
461
462
|
if (completionMode === "option" || completionMode === "both") for (let i = 0; i < args.length; i++) {
|
|
462
463
|
const arg = args[i];
|
|
463
464
|
const singularMatch = completionName === "singular" || completionName === "both" ? arg.startsWith("--completion=") : false;
|
|
@@ -465,14 +466,14 @@ function run(parser, programName, args, options = {}) {
|
|
|
465
466
|
if (singularMatch || pluralMatch) {
|
|
466
467
|
const shell = arg.slice(arg.indexOf("=") + 1);
|
|
467
468
|
const completionArgs = args.slice(i + 1);
|
|
468
|
-
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode);
|
|
469
|
+
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
|
|
469
470
|
} else {
|
|
470
471
|
const singularMatchExact = completionName === "singular" || completionName === "both" ? arg === "--completion" : false;
|
|
471
472
|
const pluralMatchExact = completionName === "plural" || completionName === "both" ? arg === "--completions" : false;
|
|
472
473
|
if ((singularMatchExact || pluralMatchExact) && i + 1 < args.length) {
|
|
473
474
|
const shell = args[i + 1];
|
|
474
475
|
const completionArgs = args.slice(i + 2);
|
|
475
|
-
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode);
|
|
476
|
+
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
|
|
476
477
|
}
|
|
477
478
|
}
|
|
478
479
|
}
|
package/dist/facade.js
CHANGED
|
@@ -343,7 +343,7 @@ function classifyResult(result, args) {
|
|
|
343
343
|
* Handles shell completion requests.
|
|
344
344
|
* @since 0.6.0
|
|
345
345
|
*/
|
|
346
|
-
function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode) {
|
|
346
|
+
function handleCompletion(completionArgs, programName, parser, completionParser, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName) {
|
|
347
347
|
const shellName = completionArgs[0] || "";
|
|
348
348
|
const args = completionArgs.slice(1);
|
|
349
349
|
if (!shellName) {
|
|
@@ -375,7 +375,8 @@ function handleCompletion(completionArgs, programName, parser, completionParser,
|
|
|
375
375
|
return onError(1);
|
|
376
376
|
}
|
|
377
377
|
if (args.length === 0) {
|
|
378
|
-
const
|
|
378
|
+
const usePlural = completionName === "plural";
|
|
379
|
+
const completionArg = completionMode === "option" ? usePlural ? "--completions" : "--completion" : usePlural ? "completions" : "completion";
|
|
379
380
|
const script = shell.generateScript(programName, [completionArg, shellName]);
|
|
380
381
|
stdout(script);
|
|
381
382
|
} else {
|
|
@@ -457,7 +458,7 @@ function run(parser, programName, args, options = {}) {
|
|
|
457
458
|
} : createCompletionParser(completion, programName, availableShells, completionName);
|
|
458
459
|
if (options.completion) {
|
|
459
460
|
const hasHelpOption = args.includes("--help");
|
|
460
|
-
if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode);
|
|
461
|
+
if ((completionMode === "command" || completionMode === "both") && args.length >= 1 && ((completionName === "singular" || completionName === "both" ? args[0] === "completion" : false) || (completionName === "plural" || completionName === "both" ? args[0] === "completions" : false)) && !hasHelpOption) return handleCompletion(args.slice(1), programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
|
|
461
462
|
if (completionMode === "option" || completionMode === "both") for (let i = 0; i < args.length; i++) {
|
|
462
463
|
const arg = args[i];
|
|
463
464
|
const singularMatch = completionName === "singular" || completionName === "both" ? arg.startsWith("--completion=") : false;
|
|
@@ -465,14 +466,14 @@ function run(parser, programName, args, options = {}) {
|
|
|
465
466
|
if (singularMatch || pluralMatch) {
|
|
466
467
|
const shell = arg.slice(arg.indexOf("=") + 1);
|
|
467
468
|
const completionArgs = args.slice(i + 1);
|
|
468
|
-
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode);
|
|
469
|
+
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
|
|
469
470
|
} else {
|
|
470
471
|
const singularMatchExact = completionName === "singular" || completionName === "both" ? arg === "--completion" : false;
|
|
471
472
|
const pluralMatchExact = completionName === "plural" || completionName === "both" ? arg === "--completions" : false;
|
|
472
473
|
if ((singularMatchExact || pluralMatchExact) && i + 1 < args.length) {
|
|
473
474
|
const shell = args[i + 1];
|
|
474
475
|
const completionArgs = args.slice(i + 2);
|
|
475
|
-
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode);
|
|
476
|
+
return handleCompletion([shell, ...completionArgs], programName, parser, completionParsers.completionCommand, stdout, stderr, onCompletion, onError, availableShells, colors, maxWidth, completionMode, completionName);
|
|
476
477
|
}
|
|
477
478
|
}
|
|
478
479
|
}
|
package/dist/modifiers.d.cts
CHANGED
|
@@ -81,7 +81,7 @@ declare class WithDefaultError extends Error {
|
|
|
81
81
|
* or the default value if the wrapped parser fails to match
|
|
82
82
|
* (union type {@link TValue} | {@link TDefault}).
|
|
83
83
|
*/
|
|
84
|
-
declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
|
|
84
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
|
|
85
85
|
/**
|
|
86
86
|
* Creates a parser that makes another parser use a default value when it fails
|
|
87
87
|
* to match or consume input. This is similar to {@link optional}, but instead
|
|
@@ -100,7 +100,7 @@ declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<T
|
|
|
100
100
|
* (union type {@link TValue} | {@link TDefault}).
|
|
101
101
|
* @since 0.5.0
|
|
102
102
|
*/
|
|
103
|
-
declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
|
|
103
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
|
|
104
104
|
/**
|
|
105
105
|
* Creates a parser that transforms the result value of another parser using
|
|
106
106
|
* a mapping function. This enables value transformation while preserving
|
package/dist/modifiers.d.ts
CHANGED
|
@@ -81,7 +81,7 @@ declare class WithDefaultError extends Error {
|
|
|
81
81
|
* or the default value if the wrapped parser fails to match
|
|
82
82
|
* (union type {@link TValue} | {@link TDefault}).
|
|
83
83
|
*/
|
|
84
|
-
declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
|
|
84
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
|
|
85
85
|
/**
|
|
86
86
|
* Creates a parser that makes another parser use a default value when it fails
|
|
87
87
|
* to match or consume input. This is similar to {@link optional}, but instead
|
|
@@ -100,7 +100,7 @@ declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<T
|
|
|
100
100
|
* (union type {@link TValue} | {@link TDefault}).
|
|
101
101
|
* @since 0.5.0
|
|
102
102
|
*/
|
|
103
|
-
declare function withDefault<TValue, TState, TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
|
|
103
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
|
|
104
104
|
/**
|
|
105
105
|
* Creates a parser that transforms the result value of another parser using
|
|
106
106
|
* a mapping function. This enables value transformation while preserving
|