( tree: CommandTree, argv: string[], )
| 12 | } |
| 13 | |
| 14 | export function resolveCommand( |
| 15 | tree: CommandTree, |
| 16 | argv: string[], |
| 17 | ): { command: CommandConstructor, path: string[] } | undefined { |
| 18 | const path: string[] = [] |
| 19 | let node: CommandNode | undefined |
| 20 | let lastMatch: { command: CommandConstructor, path: string[] } | undefined |
| 21 | |
| 22 | for (let i = 0; i < argv.length; i++) { |
| 23 | const token = argv[i] |
| 24 | if (token === undefined || token.startsWith('-')) |
| 25 | break |
| 26 | |
| 27 | const next = path.length === 0 ? tree[token] : node?.subcommands[token] |
| 28 | if (!next) |
| 29 | break |
| 30 | |
| 31 | node = next |
| 32 | path.push(token) |
| 33 | |
| 34 | if (node.command) { |
| 35 | lastMatch = { command: node.command, path: [...path] } |
| 36 | const nextToken = argv[i + 1] |
| 37 | if (nextToken === undefined || nextToken.startsWith('-') || !(nextToken in node.subcommands)) |
| 38 | return lastMatch |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | return lastMatch |
| 43 | } |
| 44 | |
| 45 | function editDistance(a: string, b: string): number { |
| 46 | const m = a.length |
no test coverage detected