MCPcopy
hub / github.com/Effect-TS/effect / parseInternal

Function parseInternal

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

Source from the content-addressed store, hash-verified

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