(rawArgs: string[])
| 1538 | }; |
| 1539 | |
| 1540 | function parseToolCommand(rawArgs: string[]): Record<string, unknown> { |
| 1541 | const {argv: cliArgs, restore} = shieldLiteralArgs(rawArgs, commandName => { |
| 1542 | const commandDef = mockCommands[commandName]; |
| 1543 | return commandDef ? getCommandArgSpec(commandDef.args) : undefined; |
| 1544 | }); |
| 1545 | |
| 1546 | let parsedArgs: Record<string, unknown> | undefined; |
| 1547 | const parser = yargs(cliArgs) |
| 1548 | .locale('en') |
| 1549 | .exitProcess(false) |
| 1550 | .strict() |
| 1551 | .fail((msg, err) => { |
| 1552 | throw err ?? new Error(msg); |
| 1553 | }); |
| 1554 | |
| 1555 | for (const [commandName, commandDef] of Object.entries(mockCommands)) { |
| 1556 | const {command, usage} = buildCommand(commandName, commandDef.args); |
| 1557 | parser.command( |
| 1558 | command, |
| 1559 | commandDef.description, |
| 1560 | y => { |
| 1561 | y.usage(usage); |
| 1562 | y.option('output-format', { |
| 1563 | choices: ['md', 'json'], |
| 1564 | default: 'md', |
| 1565 | }); |
| 1566 | registerCommandArgs(y, commandDef.args); |
| 1567 | }, |
| 1568 | argv => { |
| 1569 | const commandArgs: Record<string, unknown> = {}; |
| 1570 | for (const argName of Object.keys(commandDef.args)) { |
| 1571 | if (argName in argv) { |
| 1572 | commandArgs[argName] = restore(argv[argName]); |
| 1573 | } |
| 1574 | } |
| 1575 | parsedArgs = commandArgs; |
| 1576 | }, |
| 1577 | ); |
| 1578 | } |
| 1579 | |
| 1580 | parser.parse(); |
| 1581 | assert.ok(parsedArgs, `Command did not match: ${rawArgs.join(' ')}`); |
| 1582 | return parsedArgs; |
| 1583 | } |
| 1584 | |
| 1585 | it('parses an omitted variadic array arg without minItems as an empty array', () => { |
| 1586 | assert.deepStrictEqual(parseToolCommand(['allow_empty_array', '2']), { |
no test coverage detected