( lexResult: LexResult, command: Command.Any, commandPath: ReadonlyArray<string> = [] )
| 47 | |
| 48 | /** @internal */ |
| 49 | export const parseArgs = ( |
| 50 | lexResult: LexResult, |
| 51 | command: Command.Any, |
| 52 | commandPath: ReadonlyArray<string> = [] |
| 53 | ): Effect.Effect<ParsedTokens, never, Environment> => |
| 54 | Effect.gen(function*() { |
| 55 | const { tokens, trailingOperands: afterEndOfOptions } = lexResult |
| 56 | const newCommandPath = [...commandPath, command.name] |
| 57 | |
| 58 | const commandImpl = toImpl(command) |
| 59 | const singles = commandImpl.config.flags.flatMap(Param.extractSingleParams) |
| 60 | const flagParams = singles.filter(Param.isFlagParam) |
| 61 | const flagRegistry = createFlagRegistry(flagParams) |
| 62 | |
| 63 | const inheritedSingles = commandImpl.contextConfig.flags.flatMap(Param.extractSingleParams) |
| 64 | const inheritedFlagParams = inheritedSingles.filter(Param.isFlagParam) |
| 65 | const inheritedFlagRegistry = createFlagRegistry(inheritedFlagParams) |
| 66 | const inheritedNames = new Set(inheritedFlagParams.map((param) => param.name)) |
| 67 | |
| 68 | const context: CommandContext = { |
| 69 | command, |
| 70 | commandPath: newCommandPath, |
| 71 | flagRegistry, |
| 72 | inheritedFlagRegistry, |
| 73 | localFlagNames: flagParams.filter((param) => !inheritedNames.has(param.name)).map((param) => param.name) |
| 74 | } |
| 75 | |
| 76 | const result = scanCommandLevel(tokens, context) |
| 77 | |
| 78 | if (result._tag === "Leaf") { |
| 79 | return { |
| 80 | flags: result.flags, |
| 81 | arguments: [...result.arguments, ...afterEndOfOptions], |
| 82 | subcommand: Option.none(), |
| 83 | ...(result.errors.length > 0 && { errors: result.errors }) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | const subLex: LexResult = { tokens: result.childTokens, trailingOperands: afterEndOfOptions } |
| 88 | const subParsed = yield* parseArgs( |
| 89 | subLex, |
| 90 | result.sub, |
| 91 | newCommandPath |
| 92 | ) |
| 93 | |
| 94 | const allErrors = [...result.errors, ...(subParsed.errors ?? [])] |
| 95 | return { |
| 96 | flags: result.flags, |
| 97 | arguments: [], |
| 98 | subcommand: Option.some({ name: result.sub.name, parsedInput: subParsed }), |
| 99 | ...(allErrors.length > 0 && { errors: allErrors }) |
| 100 | } |
| 101 | }) |
| 102 | |
| 103 | /* ========================================================================== */ |
| 104 | /* Types */ |
nothing calls this directly
no test coverage detected