( tokens: ReadonlyArray<Token>, command: Command.Any, registry: FlagRegistry )
| 626 | * @internal |
| 627 | */ |
| 628 | export const consumeGlobalFlags = ( |
| 629 | tokens: ReadonlyArray<Token>, |
| 630 | command: Command.Any, |
| 631 | registry: FlagRegistry |
| 632 | ): { flagMap: FlagMap; remainder: ReadonlyArray<Token>; errors: ReadonlyArray<CliError.InvalidValue> } => { |
| 633 | const flagMap = createEmptyFlagMap(registry.params) |
| 634 | const errors: Array<CliError.InvalidValue> = [] |
| 635 | |
| 636 | const consumeLevel = ( |
| 637 | tokens: ReadonlyArray<Token>, |
| 638 | command: Command.Any, |
| 639 | ignoredRegistries: ReadonlyArray<FlagRegistry> |
| 640 | ): ReadonlyArray<Token> => { |
| 641 | const localRegistry = createFlagRegistry(extractFlagParams(command)) |
| 642 | const inheritedRegistry = createFlagRegistry(extractContextFlagParams(command)) |
| 643 | const subIndex = buildSubcommandIndex(command.subcommands) |
| 644 | const cursor = makeCursor(tokens) |
| 645 | const remainder: Array<Token> = [] |
| 646 | let awaitingFirstValue = true |
| 647 | |
| 648 | for (let token = cursor.take(); token; token = cursor.take()) { |
| 649 | if (isFlagToken(token)) { |
| 650 | const ignored = resolveFromRegistries(token, ignoredRegistries) |
| 651 | if (ignored !== undefined) { |
| 652 | preserveFlag(remainder, cursor, token, ignored) |
| 653 | continue |
| 654 | } |
| 655 | |
| 656 | const inherited = resolveFlag(token, inheritedRegistry) |
| 657 | if (inherited !== undefined) { |
| 658 | preserveFlag(remainder, cursor, token, inherited) |
| 659 | continue |
| 660 | } |
| 661 | |
| 662 | const local = resolveFlag(token, localRegistry) |
| 663 | const global = resolveFlag(token, registry) |
| 664 | if (local !== undefined) { |
| 665 | if ( |
| 666 | global === undefined || !awaitingFirstValue || |
| 667 | !localFlagWouldPrecedeSubcommand(token, cursor.rest(), local, subIndex, [ |
| 668 | localRegistry, |
| 669 | inheritedRegistry, |
| 670 | registry |
| 671 | ]) |
| 672 | ) { |
| 673 | preserveFlag(remainder, cursor, token, local) |
| 674 | continue |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | if (global !== undefined) { |
| 679 | const consumed = consumeFlagValueWithTokens(cursor, token, global.param, global.negated) |
| 680 | if (consumed._tag === "Error") { |
| 681 | errors.push(consumed.error) |
| 682 | continue |
| 683 | } |
| 684 | if (consumed.value !== undefined) { |
| 685 | flagMap[global.param.name].push(consumed.value) |
nothing calls this directly
no test coverage detected