MCPcopy Create free account
hub / github.com/Effect-TS/effect / parseInternal

Function parseInternal

packages/cli/src/internal/options.ts:1186–1372  ·  view source on GitHub ↗
(
  self: Instruction,
  args: HashMap.HashMap<string, ReadonlyArray<string>>,
  config: CliConfig.CliConfig
)

Source from the content-addressed store, hash-verified

1184const keyValueSplitter = /=(.*)/
1185
1186const parseInternal = (
1187 self: Instruction,
1188 args: HashMap.HashMap<string, ReadonlyArray<string>>,
1189 config: CliConfig.CliConfig
1190): Effect.Effect<
1191 unknown,
1192 ValidationError.ValidationError,
1193 FileSystem.FileSystem | Path.Path | Terminal.Terminal
1194> => {
1195 switch (self._tag) {
1196 case "Empty": {
1197 return Effect.void
1198 }
1199 case "Single": {
1200 const singleNames = Arr.filterMap(getNames(self), (name) => HashMap.get(args, name))
1201 if (Arr.isNonEmptyReadonlyArray(singleNames)) {
1202 const head = Arr.headNonEmpty(singleNames)
1203 const tail = Arr.tailNonEmpty(singleNames)
1204 if (Arr.isEmptyReadonlyArray(tail)) {
1205 if (Arr.isEmptyReadonlyArray(head)) {
1206 return InternalPrimitive.validate(self.primitiveType, Option.none(), config).pipe(
1207 Effect.mapError((e) => InternalValidationError.invalidValue(InternalHelpDoc.p(e)))
1208 )
1209 }
1210 if (
1211 Arr.isNonEmptyReadonlyArray(head) &&
1212 Arr.isEmptyReadonlyArray(Arr.tailNonEmpty(head))
1213 ) {
1214 const value = Arr.headNonEmpty(head)
1215 return InternalPrimitive.validate(self.primitiveType, Option.some(value), config).pipe(
1216 Effect.mapError((e) => InternalValidationError.invalidValue(InternalHelpDoc.p(e)))
1217 )
1218 }
1219 return Effect.fail(
1220 InternalValidationError.multipleValuesDetected(InternalHelpDoc.empty, head)
1221 )
1222 }
1223 const error = InternalHelpDoc.p(
1224 `More than one reference to option '${self.fullName}' detected`
1225 )
1226 return Effect.fail(InternalValidationError.invalidValue(error))
1227 }
1228 const error = InternalHelpDoc.p(`Expected to find option: '${self.fullName}'`)
1229 return Effect.fail(InternalValidationError.missingValue(error))
1230 }
1231 case "KeyValueMap": {
1232 const extractKeyValue = (
1233 value: string
1234 ): Effect.Effect<[string, string], ValidationError.ValidationError> => {
1235 const split = value.trim().split(keyValueSplitter, 2)
1236 if (Arr.isNonEmptyReadonlyArray(split) && split.length === 2 && split[1] !== "") {
1237 return Effect.succeed(split as unknown as [string, string])
1238 }
1239 const error = InternalHelpDoc.p(`Expected a key/value pair but received '${value}'`)
1240 return Effect.fail(InternalValidationError.invalidArgument(error))
1241 }
1242 return parseInternal(self.argumentOption, args, config).pipe(Effect.matchEffect({
1243 onFailure: (e) =>

Callers 2

options.tsFile · 0.70
wizardInternalFunction · 0.70

Calls 14

extractKeyValueFunction · 0.85
validateMinMaxFunction · 0.85
StringInterface · 0.85
validateMethod · 0.80
getNamesFunction · 0.70
getIdentifierInternalFunction · 0.70
getMethod · 0.65
pipeMethod · 0.65
failMethod · 0.65
mapMethod · 0.65
fMethod · 0.65
zipMethod · 0.65

Tested by

no test coverage detected