@optique/core 1.0.0-dev.1536 → 1.0.0-dev.1553
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 +113 -5
- package/dist/constructs.js +113 -5
- package/dist/dependency.cjs +34 -0
- package/dist/dependency.js +34 -0
- package/dist/facade.cjs +5 -3
- package/dist/facade.js +6 -4
- package/dist/index.cjs +0 -3
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/modifiers.cjs +81 -9
- package/dist/modifiers.js +81 -9
- package/dist/parser.d.cts +58 -0
- package/dist/parser.d.ts +58 -0
- package/dist/primitives.cjs +52 -0
- package/dist/primitives.js +52 -0
- package/dist/usage.cjs +0 -194
- package/dist/usage.d.cts +1 -75
- package/dist/usage.d.ts +1 -75
- package/dist/usage.js +1 -192
- package/dist/validate.cjs +24 -13
- package/dist/validate.js +24 -13
- 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/facade.js
CHANGED
|
@@ -3,7 +3,7 @@ import { commandLine, formatMessage, lineBreak, message, optionName, text, value
|
|
|
3
3
|
import { bash, fish, nu, pwsh, zsh } from "./completion.js";
|
|
4
4
|
import { dispatchByMode } from "./mode-dispatch.js";
|
|
5
5
|
import { validateCommandNames, validateMetaNameCollisions, validateOptionNames } from "./validate.js";
|
|
6
|
-
import { extractCommandNames,
|
|
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";
|
|
9
9
|
import { multiple, optional, withDefault } from "./modifiers.js";
|
|
@@ -196,6 +196,8 @@ function combineWithHelpVersion(originalParser, helpParsers, versionParsers, com
|
|
|
196
196
|
$stateType: [],
|
|
197
197
|
priority: 200,
|
|
198
198
|
usage: helpParsers.helpOption.usage,
|
|
199
|
+
leadingNames: helpParsers.helpOption.leadingNames,
|
|
200
|
+
acceptingAnyToken: false,
|
|
199
201
|
initialState: null,
|
|
200
202
|
parse(context) {
|
|
201
203
|
const { buffer, optionsTerminated } = context;
|
|
@@ -275,6 +277,8 @@ function combineWithHelpVersion(originalParser, helpParsers, versionParsers, com
|
|
|
275
277
|
$stateType: [],
|
|
276
278
|
priority: 200,
|
|
277
279
|
usage: versionParsers.versionOption.usage,
|
|
280
|
+
leadingNames: versionParsers.versionOption.leadingNames,
|
|
281
|
+
acceptingAnyToken: false,
|
|
278
282
|
initialState: null,
|
|
279
283
|
parse(context) {
|
|
280
284
|
const { buffer, optionsTerminated } = context;
|
|
@@ -620,9 +624,7 @@ function runParser(parserOrProgram, programNameOrArgs, argsOrOptions, optionsPar
|
|
|
620
624
|
completionCommandNames
|
|
621
625
|
]);
|
|
622
626
|
validateMetaNameCollisions({
|
|
623
|
-
|
|
624
|
-
leadingCommands: extractLeadingCommandNames(parser.usage, true),
|
|
625
|
-
leadingLiterals: extractLeadingLiteralValues(parser.usage),
|
|
627
|
+
leadingNames: parser.leadingNames,
|
|
626
628
|
allOptions: extractOptionNames(parser.usage, true),
|
|
627
629
|
allCommands: extractCommandNames(parser.usage, true),
|
|
628
630
|
allLiterals: extractLiteralValues(parser.usage)
|
package/dist/index.cjs
CHANGED
|
@@ -53,9 +53,6 @@ exports.ensureNonEmptyString = require_nonempty.ensureNonEmptyString;
|
|
|
53
53
|
exports.envVar = require_message.envVar;
|
|
54
54
|
exports.extractArgumentMetavars = require_usage.extractArgumentMetavars;
|
|
55
55
|
exports.extractCommandNames = require_usage.extractCommandNames;
|
|
56
|
-
exports.extractLeadingCommandNames = require_usage.extractLeadingCommandNames;
|
|
57
|
-
exports.extractLeadingLiteralValues = require_usage.extractLeadingLiteralValues;
|
|
58
|
-
exports.extractLeadingOptionNames = require_usage.extractLeadingOptionNames;
|
|
59
56
|
exports.extractLiteralValues = require_usage.extractLiteralValues;
|
|
60
57
|
exports.extractOptionNames = require_usage.extractOptionNames;
|
|
61
58
|
exports.fail = require_primitives.fail;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Annotations, ParseOptions, annotationKey, getAnnotations } from "./annotations.cjs";
|
|
2
2
|
import { NonEmptyString, ensureNonEmptyString, isNonEmptyString } from "./nonempty.cjs";
|
|
3
3
|
import { Message, MessageFormatOptions, MessageTerm, ValueSetOptions, commandLine, envVar, formatMessage, lineBreak, link, message, metavar, optionName, optionNames, text, value, valueSet, values } from "./message.cjs";
|
|
4
|
-
import { HiddenVisibility, OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, cloneUsage, cloneUsageTerm, extractArgumentMetavars, extractCommandNames,
|
|
4
|
+
import { HiddenVisibility, OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, cloneUsage, cloneUsageTerm, extractArgumentMetavars, extractCommandNames, extractLiteralValues, extractOptionNames, formatUsage, formatUsageTerm, isDocHidden, isSuggestionHidden, isUsageHidden, mergeHidden, normalizeUsage } from "./usage.cjs";
|
|
5
5
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, ShowChoicesOptions, ShowDefaultOptions, cloneDocEntry, deduplicateDocEntries, deduplicateDocFragments, formatDocPage, isDocEntryHidden } from "./doc.cjs";
|
|
6
6
|
import { ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CidrOptions, CidrValue, DeferredMap, DomainOptions, EmailOptions, FloatOptions, HostnameOptions, IntegerOptionsBigInt, IntegerOptionsNumber, IpOptions, Ipv4Options, Ipv6Options, LocaleOptions, MacAddressOptions, PortOptionsBigInt, PortOptionsNumber, PortRangeOptionsBigInt, PortRangeOptionsNumber, PortRangeValueBigInt, PortRangeValueNumber, SocketAddressOptions, SocketAddressValue, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, checkBooleanOption, checkEnumOption, choice, cidr, domain, email, float, hostname, integer, ip, ipv4, ipv6, isValueParser, locale, macAddress, port, portRange, socketAddress, string, url, uuid } from "./valueparser.cjs";
|
|
7
7
|
import { ConditionalErrorOptions, ConditionalOptions, DuplicateOptionError, GroupOptions, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, TupleOptions, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.cjs";
|
|
@@ -12,4 +12,4 @@ import { CombineModes, DocState, InferMode, InferValue, Mode, ModeIterable, Mode
|
|
|
12
12
|
import { ShellCompletion, bash, fish, nu, pwsh, zsh } from "./completion.cjs";
|
|
13
13
|
import { ParserValuePlaceholder, SourceContext } from "./context.cjs";
|
|
14
14
|
import { CommandSubConfig, ContextOptionsParam, ExtractRequiredOptions, OptionSubConfig, RunOptions, RunParserError, RunWithOptions, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync } from "./facade.cjs";
|
|
15
|
-
export { type Annotations, AnyDependencySource, ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CidrOptions, CidrValue, CombineMode, CombineModes, CombinedDependencyMode, CommandErrorOptions, CommandOptions, CommandSubConfig, ConditionalErrorOptions, ConditionalOptions, ContextOptionsParam, DeferredMap, DeferredParseState, DependencyError, DependencyMode, DependencyRegistry, DependencySource, DependencySourceState, DependencyValue, DependencyValues, DeriveAsyncOptions, DeriveFromAsyncOptions, DeriveFromOptions, DeriveFromSyncOptions, DeriveOptions, DeriveSyncOptions, DerivedValueParser, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DomainOptions, DuplicateOptionError, EmailOptions, ExtractRequiredOptions, FlagErrorOptions, FlagOptions, FloatOptions, GroupOptions, HiddenVisibility, HostnameOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, IpOptions, Ipv4Options, Ipv6Options, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MacAddressOptions, MergeOptions, type Message, type MessageFormatOptions, type MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OptionState, OptionSubConfig, OrErrorOptions, OrOptions, type ParseOptions, Parser, ParserContext, ParserResult, ParserValuePlaceholder, PassThroughFormat, PassThroughOptions, PendingDependencySourceState, PortOptionsBigInt, PortOptionsNumber, PortRangeOptionsBigInt, PortRangeOptionsNumber, PortRangeValueBigInt, PortRangeValueNumber, ResolvedDependency, Result, RunOptions, RunParserError, RunWithOptions, ShellCompletion, ShowChoicesOptions, ShowDefaultOptions, SocketAddressOptions, SocketAddressValue, SourceContext, StringOptions, SubstituteParserValue, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, type ValueSetOptions, WithDefaultError, WithDefaultOptions, annotationKey, argument, bash, checkBooleanOption, checkEnumOption, choice, cidr, cloneDocEntry, cloneUsage, cloneUsageTerm, command, commandLine, concat, conditional, constant, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, deduplicateDocEntries, deduplicateDocFragments, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, domain, email, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames,
|
|
15
|
+
export { type Annotations, AnyDependencySource, ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CidrOptions, CidrValue, CombineMode, CombineModes, CombinedDependencyMode, CommandErrorOptions, CommandOptions, CommandSubConfig, ConditionalErrorOptions, ConditionalOptions, ContextOptionsParam, DeferredMap, DeferredParseState, DependencyError, DependencyMode, DependencyRegistry, DependencySource, DependencySourceState, DependencyValue, DependencyValues, DeriveAsyncOptions, DeriveFromAsyncOptions, DeriveFromOptions, DeriveFromSyncOptions, DeriveOptions, DeriveSyncOptions, DerivedValueParser, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DomainOptions, DuplicateOptionError, EmailOptions, ExtractRequiredOptions, FlagErrorOptions, FlagOptions, FloatOptions, GroupOptions, HiddenVisibility, HostnameOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, IpOptions, Ipv4Options, Ipv6Options, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MacAddressOptions, MergeOptions, type Message, type MessageFormatOptions, type MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OptionState, OptionSubConfig, OrErrorOptions, OrOptions, type ParseOptions, Parser, ParserContext, ParserResult, ParserValuePlaceholder, PassThroughFormat, PassThroughOptions, PendingDependencySourceState, PortOptionsBigInt, PortOptionsNumber, PortRangeOptionsBigInt, PortRangeOptionsNumber, PortRangeValueBigInt, PortRangeValueNumber, ResolvedDependency, Result, RunOptions, RunParserError, RunWithOptions, ShellCompletion, ShowChoicesOptions, ShowDefaultOptions, SocketAddressOptions, SocketAddressValue, SourceContext, StringOptions, SubstituteParserValue, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, type ValueSetOptions, WithDefaultError, WithDefaultOptions, annotationKey, argument, bash, checkBooleanOption, checkEnumOption, choice, cidr, cloneDocEntry, cloneUsage, cloneUsageTerm, command, commandLine, concat, conditional, constant, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, deduplicateDocEntries, deduplicateDocFragments, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, domain, email, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractLiteralValues, extractOptionNames, fail, fish, flag, float, formatDependencyError, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getAnnotations, getDefaultValuesFunction, getDependencyIds, getDocPage, getDocPageAsync, getDocPageSync, group, hostname, integer, ip, ipv4, ipv6, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isDocEntryHidden, isDocHidden, isNonEmptyString, isPendingDependencySourceState, isSuggestionHidden, isUsageHidden, isValueParser, isWrappedDependencySource, lineBreak, link, locale, longestMatch, macAddress, map, merge, mergeHidden, message, metavar, multiple, nonEmpty, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, parseWithDependency, passThrough, pendingDependencySourceStateMarker, port, portRange, pwsh, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync, socketAddress, string, suggest, suggestAsync, suggestSync, suggestWithDependency, text, transformsDependencyValue, transformsDependencyValueMarker, tuple, url, uuid, value, valueSet, values, withDefault, wrappedDependencySourceMarker, zsh };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Annotations, ParseOptions, annotationKey, getAnnotations } from "./annotations.js";
|
|
2
2
|
import { NonEmptyString, ensureNonEmptyString, isNonEmptyString } from "./nonempty.js";
|
|
3
3
|
import { Message, MessageFormatOptions, MessageTerm, ValueSetOptions, commandLine, envVar, formatMessage, lineBreak, link, message, metavar, optionName, optionNames, text, value, valueSet, values } from "./message.js";
|
|
4
|
-
import { HiddenVisibility, OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, cloneUsage, cloneUsageTerm, extractArgumentMetavars, extractCommandNames,
|
|
4
|
+
import { HiddenVisibility, OptionName, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, cloneUsage, cloneUsageTerm, extractArgumentMetavars, extractCommandNames, extractLiteralValues, extractOptionNames, formatUsage, formatUsageTerm, isDocHidden, isSuggestionHidden, isUsageHidden, mergeHidden, normalizeUsage } from "./usage.js";
|
|
5
5
|
import { DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, ShowChoicesOptions, ShowDefaultOptions, cloneDocEntry, deduplicateDocEntries, deduplicateDocFragments, formatDocPage, isDocEntryHidden } from "./doc.js";
|
|
6
6
|
import { ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CidrOptions, CidrValue, DeferredMap, DomainOptions, EmailOptions, FloatOptions, HostnameOptions, IntegerOptionsBigInt, IntegerOptionsNumber, IpOptions, Ipv4Options, Ipv6Options, LocaleOptions, MacAddressOptions, PortOptionsBigInt, PortOptionsNumber, PortRangeOptionsBigInt, PortRangeOptionsNumber, PortRangeValueBigInt, PortRangeValueNumber, SocketAddressOptions, SocketAddressValue, StringOptions, UrlOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, checkBooleanOption, checkEnumOption, choice, cidr, domain, email, float, hostname, integer, ip, ipv4, ipv6, isValueParser, locale, macAddress, port, portRange, socketAddress, string, url, uuid } from "./valueparser.js";
|
|
7
7
|
import { ConditionalErrorOptions, ConditionalOptions, DuplicateOptionError, GroupOptions, LongestMatchErrorOptions, LongestMatchOptions, MergeOptions, NoMatchContext, ObjectErrorOptions, ObjectOptions, OrErrorOptions, OrOptions, TupleOptions, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
|
|
@@ -12,4 +12,4 @@ import { CombineModes, DocState, InferMode, InferValue, Mode, ModeIterable, Mode
|
|
|
12
12
|
import { ShellCompletion, bash, fish, nu, pwsh, zsh } from "./completion.js";
|
|
13
13
|
import { ParserValuePlaceholder, SourceContext } from "./context.js";
|
|
14
14
|
import { CommandSubConfig, ContextOptionsParam, ExtractRequiredOptions, OptionSubConfig, RunOptions, RunParserError, RunWithOptions, SubstituteParserValue, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync } from "./facade.js";
|
|
15
|
-
export { type Annotations, AnyDependencySource, ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CidrOptions, CidrValue, CombineMode, CombineModes, CombinedDependencyMode, CommandErrorOptions, CommandOptions, CommandSubConfig, ConditionalErrorOptions, ConditionalOptions, ContextOptionsParam, DeferredMap, DeferredParseState, DependencyError, DependencyMode, DependencyRegistry, DependencySource, DependencySourceState, DependencyValue, DependencyValues, DeriveAsyncOptions, DeriveFromAsyncOptions, DeriveFromOptions, DeriveFromSyncOptions, DeriveOptions, DeriveSyncOptions, DerivedValueParser, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DomainOptions, DuplicateOptionError, EmailOptions, ExtractRequiredOptions, FlagErrorOptions, FlagOptions, FloatOptions, GroupOptions, HiddenVisibility, HostnameOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, IpOptions, Ipv4Options, Ipv6Options, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MacAddressOptions, MergeOptions, type Message, type MessageFormatOptions, type MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OptionState, OptionSubConfig, OrErrorOptions, OrOptions, type ParseOptions, Parser, ParserContext, ParserResult, ParserValuePlaceholder, PassThroughFormat, PassThroughOptions, PendingDependencySourceState, PortOptionsBigInt, PortOptionsNumber, PortRangeOptionsBigInt, PortRangeOptionsNumber, PortRangeValueBigInt, PortRangeValueNumber, ResolvedDependency, Result, RunOptions, RunParserError, RunWithOptions, ShellCompletion, ShowChoicesOptions, ShowDefaultOptions, SocketAddressOptions, SocketAddressValue, SourceContext, StringOptions, SubstituteParserValue, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, type ValueSetOptions, WithDefaultError, WithDefaultOptions, annotationKey, argument, bash, checkBooleanOption, checkEnumOption, choice, cidr, cloneDocEntry, cloneUsage, cloneUsageTerm, command, commandLine, concat, conditional, constant, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, deduplicateDocEntries, deduplicateDocFragments, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, domain, email, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames,
|
|
15
|
+
export { type Annotations, AnyDependencySource, ArgumentErrorOptions, ArgumentOptions, ChoiceOptions, ChoiceOptionsBase, ChoiceOptionsNumber, ChoiceOptionsString, CidrOptions, CidrValue, CombineMode, CombineModes, CombinedDependencyMode, CommandErrorOptions, CommandOptions, CommandSubConfig, ConditionalErrorOptions, ConditionalOptions, ContextOptionsParam, DeferredMap, DeferredParseState, DependencyError, DependencyMode, DependencyRegistry, DependencySource, DependencySourceState, DependencyValue, DependencyValues, DeriveAsyncOptions, DeriveFromAsyncOptions, DeriveFromOptions, DeriveFromSyncOptions, DeriveOptions, DeriveSyncOptions, DerivedValueParser, DocEntry, DocFragment, DocFragments, DocPage, DocPageFormatOptions, DocSection, DocState, DomainOptions, DuplicateOptionError, EmailOptions, ExtractRequiredOptions, FlagErrorOptions, FlagOptions, FloatOptions, GroupOptions, HiddenVisibility, HostnameOptions, InferMode, InferValue, IntegerOptionsBigInt, IntegerOptionsNumber, IpOptions, Ipv4Options, Ipv6Options, LocaleOptions, LongestMatchErrorOptions, LongestMatchOptions, MacAddressOptions, MergeOptions, type Message, type MessageFormatOptions, type MessageTerm, Mode, ModeIterable, ModeValue, MultipleErrorOptions, MultipleOptions, NoMatchContext, NonEmptyString, ObjectErrorOptions, ObjectOptions, OptionErrorOptions, OptionName, OptionOptions, OptionState, OptionSubConfig, OrErrorOptions, OrOptions, type ParseOptions, Parser, ParserContext, ParserResult, ParserValuePlaceholder, PassThroughFormat, PassThroughOptions, PendingDependencySourceState, PortOptionsBigInt, PortOptionsNumber, PortRangeOptionsBigInt, PortRangeOptionsNumber, PortRangeValueBigInt, PortRangeValueNumber, ResolvedDependency, Result, RunOptions, RunParserError, RunWithOptions, ShellCompletion, ShowChoicesOptions, ShowDefaultOptions, SocketAddressOptions, SocketAddressValue, SourceContext, StringOptions, SubstituteParserValue, Suggestion, TupleOptions, UrlOptions, Usage, UsageFormatOptions, UsageTerm, UsageTermFormatOptions, Uuid, UuidOptions, ValueParser, ValueParserResult, type ValueSetOptions, WithDefaultError, WithDefaultOptions, annotationKey, argument, bash, checkBooleanOption, checkEnumOption, choice, cidr, cloneDocEntry, cloneUsage, cloneUsageTerm, command, commandLine, concat, conditional, constant, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, deduplicateDocEntries, deduplicateDocFragments, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, domain, email, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractLiteralValues, extractOptionNames, fail, fish, flag, float, formatDependencyError, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getAnnotations, getDefaultValuesFunction, getDependencyIds, getDocPage, getDocPageAsync, getDocPageSync, group, hostname, integer, ip, ipv4, ipv6, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isDocEntryHidden, isDocHidden, isNonEmptyString, isPendingDependencySourceState, isSuggestionHidden, isUsageHidden, isValueParser, isWrappedDependencySource, lineBreak, link, locale, longestMatch, macAddress, map, merge, mergeHidden, message, metavar, multiple, nonEmpty, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, parseWithDependency, passThrough, pendingDependencySourceStateMarker, port, portRange, pwsh, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync, socketAddress, string, suggest, suggestAsync, suggestSync, suggestWithDependency, text, transformsDependencyValue, transformsDependencyValueMarker, tuple, url, uuid, value, valueSet, values, withDefault, wrappedDependencySourceMarker, zsh };
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { annotationKey, getAnnotations } from "./annotations.js";
|
|
|
2
2
|
import { commandLine, envVar, formatMessage, lineBreak, link, message, metavar, optionName, optionNames, text, value, valueSet, values } from "./message.js";
|
|
3
3
|
import { bash, fish, nu, pwsh, zsh } from "./completion.js";
|
|
4
4
|
import { DependencyRegistry, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, formatDependencyError, getDefaultValuesFunction, getDependencyIds, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isPendingDependencySourceState, isWrappedDependencySource, parseWithDependency, pendingDependencySourceStateMarker, suggestWithDependency, transformsDependencyValue, transformsDependencyValueMarker, wrappedDependencySourceMarker } from "./dependency.js";
|
|
5
|
-
import { cloneUsage, cloneUsageTerm, extractArgumentMetavars, extractCommandNames,
|
|
5
|
+
import { cloneUsage, cloneUsageTerm, extractArgumentMetavars, extractCommandNames, extractLiteralValues, extractOptionNames, formatUsage, formatUsageTerm, isDocHidden, isSuggestionHidden, isUsageHidden, mergeHidden, normalizeUsage } from "./usage.js";
|
|
6
6
|
import { cloneDocEntry, deduplicateDocEntries, deduplicateDocFragments, formatDocPage, isDocEntryHidden } from "./doc.js";
|
|
7
7
|
import { DuplicateOptionError, concat, conditional, group, longestMatch, merge, object, or, tuple } from "./constructs.js";
|
|
8
8
|
import { WithDefaultError, map, multiple, nonEmpty, optional, withDefault } from "./modifiers.js";
|
|
@@ -12,4 +12,4 @@ import { argument, command, constant, fail, flag, option, passThrough } from "./
|
|
|
12
12
|
import { getDocPage, getDocPageAsync, getDocPageSync, parse, parseAsync, parseSync, suggest, suggestAsync, suggestSync } from "./parser.js";
|
|
13
13
|
import { RunParserError, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync } from "./facade.js";
|
|
14
14
|
|
|
15
|
-
export { DependencyRegistry, DuplicateOptionError, RunParserError, WithDefaultError, annotationKey, argument, bash, checkBooleanOption, checkEnumOption, choice, cidr, cloneDocEntry, cloneUsage, cloneUsageTerm, command, commandLine, concat, conditional, constant, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, deduplicateDocEntries, deduplicateDocFragments, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, domain, email, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames,
|
|
15
|
+
export { DependencyRegistry, DuplicateOptionError, RunParserError, WithDefaultError, annotationKey, argument, bash, checkBooleanOption, checkEnumOption, choice, cidr, cloneDocEntry, cloneUsage, cloneUsageTerm, command, commandLine, concat, conditional, constant, createDeferredParseState, createDependencySourceState, createPendingDependencySourceState, deduplicateDocEntries, deduplicateDocFragments, defaultValues, deferredParseMarker, dependency, dependencyId, dependencyIds, dependencySourceMarker, dependencySourceStateMarker, deriveFrom, deriveFromAsync, deriveFromSync, derivedValueParserMarker, domain, email, ensureNonEmptyString, envVar, extractArgumentMetavars, extractCommandNames, extractLiteralValues, extractOptionNames, fail, fish, flag, float, formatDependencyError, formatDocPage, formatMessage, formatUsage, formatUsageTerm, getAnnotations, getDefaultValuesFunction, getDependencyIds, getDocPage, getDocPageAsync, getDocPageSync, group, hostname, integer, ip, ipv4, ipv6, isDeferredParseState, isDependencySource, isDependencySourceState, isDerivedValueParser, isDocEntryHidden, isDocHidden, isNonEmptyString, isPendingDependencySourceState, isSuggestionHidden, isUsageHidden, isValueParser, isWrappedDependencySource, lineBreak, link, locale, longestMatch, macAddress, map, merge, mergeHidden, message, metavar, multiple, nonEmpty, normalizeUsage, nu, object, option, optionName, optionNames, optional, or, parse, parseAsync, parseSync, parseWithDependency, passThrough, pendingDependencySourceStateMarker, port, portRange, pwsh, runParser, runParserAsync, runParserSync, runWith, runWithAsync, runWithSync, socketAddress, string, suggest, suggestAsync, suggestSync, suggestWithDependency, text, transformsDependencyValue, transformsDependencyValueMarker, tuple, url, uuid, value, valueSet, values, withDefault, wrappedDependencySourceMarker, zsh };
|
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: [],
|
|
@@ -128,6 +128,8 @@ function optional(parser) {
|
|
|
128
128
|
type: "optional",
|
|
129
129
|
terms: parser.usage
|
|
130
130
|
}],
|
|
131
|
+
leadingNames: parser.leadingNames,
|
|
132
|
+
acceptingAnyToken: false,
|
|
131
133
|
initialState: void 0,
|
|
132
134
|
...wrappedDependencyMarker,
|
|
133
135
|
...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: adaptShouldDeferCompletion(parser.shouldDeferCompletion.bind(parser)) } : {},
|
|
@@ -173,6 +175,18 @@ function optional(parser) {
|
|
|
173
175
|
return syncParser.getDocFragments(innerState, defaultValue);
|
|
174
176
|
}
|
|
175
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;
|
|
176
190
|
}
|
|
177
191
|
/**
|
|
178
192
|
* Error type for structured error messages in {@link withDefault} default value callbacks.
|
|
@@ -246,6 +260,8 @@ function withDefault(parser, defaultValue, options) {
|
|
|
246
260
|
type: "optional",
|
|
247
261
|
terms: parser.usage
|
|
248
262
|
}],
|
|
263
|
+
leadingNames: parser.leadingNames,
|
|
264
|
+
acceptingAnyToken: false,
|
|
249
265
|
initialState: void 0,
|
|
250
266
|
...wrappedDependencyMarker,
|
|
251
267
|
...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: adaptShouldDeferCompletion(parser.shouldDeferCompletion.bind(parser)) } : {},
|
|
@@ -253,12 +269,19 @@ function withDefault(parser, defaultValue, options) {
|
|
|
253
269
|
return require_mode_dispatch.dispatchByMode(parser.$mode, () => parseOptionalStyleSync(context, syncParser), () => parseOptionalStyleAsync(context, parser));
|
|
254
270
|
},
|
|
255
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
|
+
}
|
|
256
279
|
if (!Array.isArray(state)) {
|
|
257
280
|
if (require_dependency.transformsDependencyValue(parser)) {
|
|
258
281
|
const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(void 0), () => parser.complete(void 0));
|
|
259
282
|
const handleInnerResult = (res) => {
|
|
260
283
|
if (require_dependency.isDependencySourceState(res)) try {
|
|
261
|
-
const value =
|
|
284
|
+
const value = evaluateDefault();
|
|
262
285
|
return require_dependency.createDependencySourceState({
|
|
263
286
|
success: true,
|
|
264
287
|
value
|
|
@@ -270,7 +293,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
270
293
|
};
|
|
271
294
|
}
|
|
272
295
|
try {
|
|
273
|
-
const value =
|
|
296
|
+
const value = evaluateDefault();
|
|
274
297
|
return {
|
|
275
298
|
success: true,
|
|
276
299
|
value
|
|
@@ -285,7 +308,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
285
308
|
return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, handleInnerResult);
|
|
286
309
|
}
|
|
287
310
|
if (require_dependency.isWrappedDependencySource(parser)) try {
|
|
288
|
-
const value =
|
|
311
|
+
const value = evaluateDefault();
|
|
289
312
|
const pendingState = parser[require_dependency.wrappedDependencySourceMarker];
|
|
290
313
|
return require_dependency.createDependencySourceState({
|
|
291
314
|
success: true,
|
|
@@ -302,7 +325,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
302
325
|
return innerResult;
|
|
303
326
|
}
|
|
304
327
|
try {
|
|
305
|
-
const value =
|
|
328
|
+
const value = evaluateDefault();
|
|
306
329
|
return {
|
|
307
330
|
success: true,
|
|
308
331
|
value
|
|
@@ -319,7 +342,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
319
342
|
const innerResult = require_mode_dispatch.dispatchByMode(parser.$mode, () => syncParser.complete(state), () => parser.complete(state));
|
|
320
343
|
const handleInnerResult = (res) => {
|
|
321
344
|
if (require_dependency.isDependencySourceState(res)) try {
|
|
322
|
-
const value =
|
|
345
|
+
const value = evaluateDefault();
|
|
323
346
|
return require_dependency.createDependencySourceState({
|
|
324
347
|
success: true,
|
|
325
348
|
value
|
|
@@ -331,7 +354,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
331
354
|
};
|
|
332
355
|
}
|
|
333
356
|
try {
|
|
334
|
-
const value =
|
|
357
|
+
const value = evaluateDefault();
|
|
335
358
|
return {
|
|
336
359
|
success: true,
|
|
337
360
|
value
|
|
@@ -346,7 +369,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
346
369
|
return require_mode_dispatch.mapModeValue(parser.$mode, innerResult, handleInnerResult);
|
|
347
370
|
}
|
|
348
371
|
try {
|
|
349
|
-
const value =
|
|
372
|
+
const value = evaluateDefault();
|
|
350
373
|
return require_dependency.createDependencySourceState({
|
|
351
374
|
success: true,
|
|
352
375
|
value
|
|
@@ -369,7 +392,11 @@ function withDefault(parser, defaultValue, options) {
|
|
|
369
392
|
kind: "available",
|
|
370
393
|
state: state.state[0]
|
|
371
394
|
};
|
|
372
|
-
|
|
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);
|
|
373
400
|
if (options?.message) {
|
|
374
401
|
const modifiedFragments = fragments.fragments.map((fragment) => {
|
|
375
402
|
if (fragment.type === "entry") return {
|
|
@@ -393,6 +420,20 @@ function withDefault(parser, defaultValue, options) {
|
|
|
393
420
|
configurable: true,
|
|
394
421
|
enumerable: false
|
|
395
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
|
+
}
|
|
396
437
|
return withDefaultParser;
|
|
397
438
|
}
|
|
398
439
|
/**
|
|
@@ -506,6 +547,7 @@ function map(parser, transform) {
|
|
|
506
547
|
return parser.getDocFragments(state, void 0);
|
|
507
548
|
}
|
|
508
549
|
};
|
|
550
|
+
delete mappedParser.normalizeValue;
|
|
509
551
|
if ("placeholder" in parser) Object.defineProperty(mappedParser, "placeholder", {
|
|
510
552
|
get() {
|
|
511
553
|
try {
|
|
@@ -650,6 +692,8 @@ function multiple(parser, options = {}) {
|
|
|
650
692
|
terms: parser.usage,
|
|
651
693
|
min
|
|
652
694
|
}],
|
|
695
|
+
leadingNames: parser.leadingNames,
|
|
696
|
+
acceptingAnyToken: min > 0 && (parser.acceptingAnyToken ?? false),
|
|
653
697
|
initialState: [],
|
|
654
698
|
parse(context) {
|
|
655
699
|
return require_mode_dispatch.dispatchByMode(parser.$mode, () => parseSync(context), () => parseAsync(context));
|
|
@@ -822,6 +866,27 @@ function multiple(parser, options = {}) {
|
|
|
822
866
|
configurable: true,
|
|
823
867
|
enumerable: false
|
|
824
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
|
+
}
|
|
825
890
|
return resultParser;
|
|
826
891
|
}
|
|
827
892
|
/**
|
|
@@ -886,6 +951,8 @@ function nonEmpty(parser) {
|
|
|
886
951
|
$stateType: parser.$stateType,
|
|
887
952
|
priority: parser.priority,
|
|
888
953
|
usage: parser.usage,
|
|
954
|
+
leadingNames: parser.leadingNames,
|
|
955
|
+
acceptingAnyToken: parser.acceptingAnyToken,
|
|
889
956
|
initialState: parser.initialState,
|
|
890
957
|
...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: parser.shouldDeferCompletion.bind(parser) } : {},
|
|
891
958
|
parse(context) {
|
|
@@ -908,6 +975,11 @@ function nonEmpty(parser) {
|
|
|
908
975
|
configurable: true,
|
|
909
976
|
enumerable: false
|
|
910
977
|
});
|
|
978
|
+
if (typeof parser.normalizeValue === "function") Object.defineProperty(nonEmptyParser, "normalizeValue", {
|
|
979
|
+
value: parser.normalizeValue.bind(parser),
|
|
980
|
+
configurable: true,
|
|
981
|
+
enumerable: false
|
|
982
|
+
});
|
|
911
983
|
return nonEmptyParser;
|
|
912
984
|
}
|
|
913
985
|
|
package/dist/modifiers.js
CHANGED
|
@@ -118,7 +118,7 @@ function optional(parser) {
|
|
|
118
118
|
const wrappedDependencyMarker = innerHasWrappedDependency ? { [wrappedDependencySourceMarker]: parser[wrappedDependencySourceMarker] } : innerHasDirectDependency ? { [wrappedDependencySourceMarker]: syncParser.initialState } : {};
|
|
119
119
|
const hasWrappedDependencySource = wrappedDependencySourceMarker in wrappedDependencyMarker;
|
|
120
120
|
const wrappedPendingState = hasWrappedDependencySource ? wrappedDependencyMarker[wrappedDependencySourceMarker] : void 0;
|
|
121
|
-
|
|
121
|
+
const optionalParser = {
|
|
122
122
|
$mode: parser.$mode,
|
|
123
123
|
$valueType: [],
|
|
124
124
|
$stateType: [],
|
|
@@ -128,6 +128,8 @@ function optional(parser) {
|
|
|
128
128
|
type: "optional",
|
|
129
129
|
terms: parser.usage
|
|
130
130
|
}],
|
|
131
|
+
leadingNames: parser.leadingNames,
|
|
132
|
+
acceptingAnyToken: false,
|
|
131
133
|
initialState: void 0,
|
|
132
134
|
...wrappedDependencyMarker,
|
|
133
135
|
...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: adaptShouldDeferCompletion(parser.shouldDeferCompletion.bind(parser)) } : {},
|
|
@@ -173,6 +175,18 @@ function optional(parser) {
|
|
|
173
175
|
return syncParser.getDocFragments(innerState, defaultValue);
|
|
174
176
|
}
|
|
175
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;
|
|
176
190
|
}
|
|
177
191
|
/**
|
|
178
192
|
* Error type for structured error messages in {@link withDefault} default value callbacks.
|
|
@@ -246,6 +260,8 @@ function withDefault(parser, defaultValue, options) {
|
|
|
246
260
|
type: "optional",
|
|
247
261
|
terms: parser.usage
|
|
248
262
|
}],
|
|
263
|
+
leadingNames: parser.leadingNames,
|
|
264
|
+
acceptingAnyToken: false,
|
|
249
265
|
initialState: void 0,
|
|
250
266
|
...wrappedDependencyMarker,
|
|
251
267
|
...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: adaptShouldDeferCompletion(parser.shouldDeferCompletion.bind(parser)) } : {},
|
|
@@ -253,12 +269,19 @@ function withDefault(parser, defaultValue, options) {
|
|
|
253
269
|
return dispatchByMode(parser.$mode, () => parseOptionalStyleSync(context, syncParser), () => parseOptionalStyleAsync(context, parser));
|
|
254
270
|
},
|
|
255
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
|
+
}
|
|
256
279
|
if (!Array.isArray(state)) {
|
|
257
280
|
if (transformsDependencyValue(parser)) {
|
|
258
281
|
const innerResult = dispatchByMode(parser.$mode, () => syncParser.complete(void 0), () => parser.complete(void 0));
|
|
259
282
|
const handleInnerResult = (res) => {
|
|
260
283
|
if (isDependencySourceState(res)) try {
|
|
261
|
-
const value =
|
|
284
|
+
const value = evaluateDefault();
|
|
262
285
|
return createDependencySourceState({
|
|
263
286
|
success: true,
|
|
264
287
|
value
|
|
@@ -270,7 +293,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
270
293
|
};
|
|
271
294
|
}
|
|
272
295
|
try {
|
|
273
|
-
const value =
|
|
296
|
+
const value = evaluateDefault();
|
|
274
297
|
return {
|
|
275
298
|
success: true,
|
|
276
299
|
value
|
|
@@ -285,7 +308,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
285
308
|
return mapModeValue(parser.$mode, innerResult, handleInnerResult);
|
|
286
309
|
}
|
|
287
310
|
if (isWrappedDependencySource(parser)) try {
|
|
288
|
-
const value =
|
|
311
|
+
const value = evaluateDefault();
|
|
289
312
|
const pendingState = parser[wrappedDependencySourceMarker];
|
|
290
313
|
return createDependencySourceState({
|
|
291
314
|
success: true,
|
|
@@ -302,7 +325,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
302
325
|
return innerResult;
|
|
303
326
|
}
|
|
304
327
|
try {
|
|
305
|
-
const value =
|
|
328
|
+
const value = evaluateDefault();
|
|
306
329
|
return {
|
|
307
330
|
success: true,
|
|
308
331
|
value
|
|
@@ -319,7 +342,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
319
342
|
const innerResult = dispatchByMode(parser.$mode, () => syncParser.complete(state), () => parser.complete(state));
|
|
320
343
|
const handleInnerResult = (res) => {
|
|
321
344
|
if (isDependencySourceState(res)) try {
|
|
322
|
-
const value =
|
|
345
|
+
const value = evaluateDefault();
|
|
323
346
|
return createDependencySourceState({
|
|
324
347
|
success: true,
|
|
325
348
|
value
|
|
@@ -331,7 +354,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
331
354
|
};
|
|
332
355
|
}
|
|
333
356
|
try {
|
|
334
|
-
const value =
|
|
357
|
+
const value = evaluateDefault();
|
|
335
358
|
return {
|
|
336
359
|
success: true,
|
|
337
360
|
value
|
|
@@ -346,7 +369,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
346
369
|
return mapModeValue(parser.$mode, innerResult, handleInnerResult);
|
|
347
370
|
}
|
|
348
371
|
try {
|
|
349
|
-
const value =
|
|
372
|
+
const value = evaluateDefault();
|
|
350
373
|
return createDependencySourceState({
|
|
351
374
|
success: true,
|
|
352
375
|
value
|
|
@@ -369,7 +392,11 @@ function withDefault(parser, defaultValue, options) {
|
|
|
369
392
|
kind: "available",
|
|
370
393
|
state: state.state[0]
|
|
371
394
|
};
|
|
372
|
-
|
|
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);
|
|
373
400
|
if (options?.message) {
|
|
374
401
|
const modifiedFragments = fragments.fragments.map((fragment) => {
|
|
375
402
|
if (fragment.type === "entry") return {
|
|
@@ -393,6 +420,20 @@ function withDefault(parser, defaultValue, options) {
|
|
|
393
420
|
configurable: true,
|
|
394
421
|
enumerable: false
|
|
395
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
|
+
}
|
|
396
437
|
return withDefaultParser;
|
|
397
438
|
}
|
|
398
439
|
/**
|
|
@@ -506,6 +547,7 @@ function map(parser, transform) {
|
|
|
506
547
|
return parser.getDocFragments(state, void 0);
|
|
507
548
|
}
|
|
508
549
|
};
|
|
550
|
+
delete mappedParser.normalizeValue;
|
|
509
551
|
if ("placeholder" in parser) Object.defineProperty(mappedParser, "placeholder", {
|
|
510
552
|
get() {
|
|
511
553
|
try {
|
|
@@ -650,6 +692,8 @@ function multiple(parser, options = {}) {
|
|
|
650
692
|
terms: parser.usage,
|
|
651
693
|
min
|
|
652
694
|
}],
|
|
695
|
+
leadingNames: parser.leadingNames,
|
|
696
|
+
acceptingAnyToken: min > 0 && (parser.acceptingAnyToken ?? false),
|
|
653
697
|
initialState: [],
|
|
654
698
|
parse(context) {
|
|
655
699
|
return dispatchByMode(parser.$mode, () => parseSync(context), () => parseAsync(context));
|
|
@@ -822,6 +866,27 @@ function multiple(parser, options = {}) {
|
|
|
822
866
|
configurable: true,
|
|
823
867
|
enumerable: false
|
|
824
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
|
+
}
|
|
825
890
|
return resultParser;
|
|
826
891
|
}
|
|
827
892
|
/**
|
|
@@ -886,6 +951,8 @@ function nonEmpty(parser) {
|
|
|
886
951
|
$stateType: parser.$stateType,
|
|
887
952
|
priority: parser.priority,
|
|
888
953
|
usage: parser.usage,
|
|
954
|
+
leadingNames: parser.leadingNames,
|
|
955
|
+
acceptingAnyToken: parser.acceptingAnyToken,
|
|
889
956
|
initialState: parser.initialState,
|
|
890
957
|
...typeof parser.shouldDeferCompletion === "function" ? { shouldDeferCompletion: parser.shouldDeferCompletion.bind(parser) } : {},
|
|
891
958
|
parse(context) {
|
|
@@ -908,6 +975,11 @@ function nonEmpty(parser) {
|
|
|
908
975
|
configurable: true,
|
|
909
976
|
enumerable: false
|
|
910
977
|
});
|
|
978
|
+
if (typeof parser.normalizeValue === "function") Object.defineProperty(nonEmptyParser, "normalizeValue", {
|
|
979
|
+
value: parser.normalizeValue.bind(parser),
|
|
980
|
+
configurable: true,
|
|
981
|
+
enumerable: false
|
|
982
|
+
});
|
|
911
983
|
return nonEmptyParser;
|
|
912
984
|
}
|
|
913
985
|
|
package/dist/parser.d.cts
CHANGED
|
@@ -105,6 +105,44 @@ interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
|
105
105
|
* to use it in command-line interfaces.
|
|
106
106
|
*/
|
|
107
107
|
readonly usage: Usage;
|
|
108
|
+
/**
|
|
109
|
+
* Names that this parser could match at the first buffer position.
|
|
110
|
+
* Used by `runParser()` to detect collisions with built-in meta
|
|
111
|
+
* features (help, version, completion).
|
|
112
|
+
*
|
|
113
|
+
* Each built-in combinator computes this from its structural semantics.
|
|
114
|
+
* Custom parser implementations must include every fixed token that
|
|
115
|
+
* the parser accepts at `argv[0]` — command names, option names, and
|
|
116
|
+
* literal values alike. For example, a parser whose usage declares
|
|
117
|
+
* `{ type: "literal", value: "serve" }` should include `"serve"` in
|
|
118
|
+
* this set. Parsers that accept *any* token (like `argument()`) should
|
|
119
|
+
* return an empty set and set {@link acceptingAnyToken} to `true`
|
|
120
|
+
* instead.
|
|
121
|
+
*
|
|
122
|
+
* @since 1.0.0
|
|
123
|
+
*/
|
|
124
|
+
readonly leadingNames: ReadonlySet<string>;
|
|
125
|
+
/**
|
|
126
|
+
* Whether this parser unconditionally consumes any positional token at
|
|
127
|
+
* the first buffer position. A parser with this flag accepts any
|
|
128
|
+
* non-option token but may still reject option-like tokens (those
|
|
129
|
+
* starting with `"-"`).
|
|
130
|
+
*
|
|
131
|
+
* In shared-buffer compositions (`tuple()`, `object()`, `merge()`,
|
|
132
|
+
* `concat()`), a catch-all parser blocks positional names (command
|
|
133
|
+
* names) from lower-priority siblings but does not block option-like
|
|
134
|
+
* names. In `conditional()`, option-like names from the default
|
|
135
|
+
* branch remain reachable even when the discriminator is a catch-all.
|
|
136
|
+
*
|
|
137
|
+
* Only `argument()` is inherently accepting-any-token; combinators
|
|
138
|
+
* like `or()` and `map()` propagate this from their children.
|
|
139
|
+
* Wrappers that can succeed without consuming (`optional()`,
|
|
140
|
+
* `withDefault()`, `multiple()` with `min = 0`) always set this
|
|
141
|
+
* to `false`.
|
|
142
|
+
*
|
|
143
|
+
* @since 1.0.0
|
|
144
|
+
*/
|
|
145
|
+
readonly acceptingAnyToken: boolean;
|
|
108
146
|
/**
|
|
109
147
|
* The initial state for this parser. This is used to initialize the
|
|
110
148
|
* state when parsing starts.
|
|
@@ -187,6 +225,26 @@ interface Parser<M extends Mode = "sync", TValue = unknown, TState = unknown> {
|
|
|
187
225
|
* @since 1.0.0
|
|
188
226
|
*/
|
|
189
227
|
shouldDeferCompletion?(state: TState): boolean;
|
|
228
|
+
/**
|
|
229
|
+
* Normalizes a parsed value according to the underlying value parser's
|
|
230
|
+
* configuration. When present, {@link withDefault} calls this method
|
|
231
|
+
* on default values so that runtime defaults match the representation
|
|
232
|
+
* that the value parser's `parse()` would produce.
|
|
233
|
+
*
|
|
234
|
+
* Primitive parsers ({@link option}, {@link argument}) implement this
|
|
235
|
+
* by delegating to {@link ValueParser.normalize}. Combinator wrappers
|
|
236
|
+
* ({@link optional}, {@link withDefault}) forward it from inner parsers.
|
|
237
|
+
*
|
|
238
|
+
* Exclusive combinators ({@link or}, `longestMatch()`) and
|
|
239
|
+
* multi-source combinators (`merge()`) intentionally do *not*
|
|
240
|
+
* implement this method because the active branch or key ownership
|
|
241
|
+
* is unknown at default time.
|
|
242
|
+
*
|
|
243
|
+
* @param value The value to normalize.
|
|
244
|
+
* @returns The normalized value.
|
|
245
|
+
* @since 1.0.0
|
|
246
|
+
*/
|
|
247
|
+
normalizeValue?(value: TValue): TValue;
|
|
190
248
|
}
|
|
191
249
|
/**
|
|
192
250
|
* The context of the parser, which includes the input buffer and the state.
|