| 51 | * @internal |
| 52 | */ |
| 53 | export const parseArgs = ( |
| 54 | lexResult: LexResult, |
| 55 | command: Command.Any, |
| 56 | commandPath: ReadonlyArray<string> = [] |
| 57 | ): Effect.Effect<ParsedTokens, never, Environment> => |
| 58 | Effect.gen(function*() { |
| 59 | const { tokens, trailingOperands: afterEndOfOptions } = lexResult |
| 60 | const newCommandPath = [...commandPath, command.name] |
| 61 | const result = scanCommandLevel(tokens, makeCommandContext(command, newCommandPath)) |
| 62 | |
| 63 | if (result._tag === "Leaf") { |
| 64 | return { |
| 65 | flags: result.flags, |
| 66 | arguments: [...result.arguments, ...afterEndOfOptions], |
| 67 | subcommand: Option.none(), |
| 68 | ...(result.errors.length > 0 && { errors: result.errors }) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | const subLex: LexResult = { tokens: result.childTokens, trailingOperands: afterEndOfOptions } |
| 73 | const subParsed = yield* parseArgs( |
| 74 | subLex, |
| 75 | result.sub, |
| 76 | newCommandPath |
| 77 | ) |
| 78 | |
| 79 | const allErrors = [...result.errors, ...(subParsed.errors ?? [])] |
| 80 | return { |
| 81 | flags: result.flags, |
| 82 | arguments: [], |
| 83 | subcommand: Option.some({ name: result.sub.name, parsedInput: subParsed }), |
| 84 | ...(allErrors.length > 0 && { errors: allErrors }) |
| 85 | } |
| 86 | }) |
| 87 | |
| 88 | /** |
| 89 | * Returns the subset of `tokens` left for the selected command once its |