( value: string, cursor: TokenCursor, context: CommandContext, state: ParseState )
| 814 | * - Report error if command expects subcommand but got unknown value |
| 815 | */ |
| 816 | const resolveFirstValue = ( |
| 817 | value: string, |
| 818 | cursor: TokenCursor, |
| 819 | context: CommandContext, |
| 820 | state: ParseState |
| 821 | ): FirstValueResult => { |
| 822 | const { command, commandPath, inheritedFlagRegistry, localFlagNames } = context |
| 823 | const subIndex = buildSubcommandIndex(command.subcommands) |
| 824 | const sub = subIndex.get(value) |
| 825 | |
| 826 | if (sub) { |
| 827 | const selectedPath = [...commandPath, sub.name] |
| 828 | |
| 829 | // Local flags are not inherited by subcommands. |
| 830 | const parentFlags = state.flags.snapshot() |
| 831 | for (const localFlagName of localFlagNames) { |
| 832 | const values = parentFlags[localFlagName] |
| 833 | if (values !== undefined && values.length > 0) { |
| 834 | state.errors.push( |
| 835 | new CliError.UnrecognizedOption({ |
| 836 | option: `--${localFlagName}`, |
| 837 | suggestions: [], |
| 838 | command: selectedPath |
| 839 | }) |
| 840 | ) |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | // npm-style: inherited parent flags can appear after subcommand name |
| 845 | const tail = consumeKnownFlags(cursor.rest(), inheritedFlagRegistry) |
| 846 | state.flags.merge(tail.flagMap) |
| 847 | state.errors.push(...tail.errors) |
| 848 | |
| 849 | return { |
| 850 | _tag: "Subcommand", |
| 851 | result: { |
| 852 | _tag: "Sub", |
| 853 | flags: state.flags.snapshot(), |
| 854 | sub, |
| 855 | childTokens: tail.remainder, |
| 856 | errors: state.errors |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | // Not a subcommand. Check if this looks like a typo. |
| 862 | const expectsArgs = toImpl(command).config.arguments.length > 0 |
| 863 | if (!expectsArgs && subIndex.size > 0) { |
| 864 | // Exclude unlisted subcommands so a typo cannot reveal a subcommand name |
| 865 | // that was intentionally kept out of --help. Unlisted commands still |
| 866 | // resolve via subIndex when invoked by exact name. |
| 867 | const visibleKeys: Array<string> = [] |
| 868 | for (const [key, sub] of subIndex) { |
| 869 | if (!sub.unlisted) visibleKeys.push(key) |
| 870 | } |
| 871 | const suggestions = suggest(value, visibleKeys) |
| 872 | state.errors.push( |
| 873 | new CliError.UnknownSubcommand({ |
no test coverage detected