(rawArgs: string[])
| 1793 | }; |
| 1794 | |
| 1795 | function parseToolCommand(rawArgs: string[]): Record<string, unknown> { |
| 1796 | const {argv: cliArgs, restore} = shieldLiteralArgs(rawArgs, commandName => { |
| 1797 | const commandDef = mockCommands[commandName]; |
| 1798 | return commandDef ? getCommandArgSpec(commandDef.args) : undefined; |
| 1799 | }); |
| 1800 | |
| 1801 | let parsedArgs: Record<string, unknown> | undefined; |
| 1802 | const parser = yargs(cliArgs) |
| 1803 | .locale('en') |
| 1804 | .exitProcess(false) |
| 1805 | .strict() |
| 1806 | .fail((msg, err) => { |
| 1807 | throw err ?? new Error(msg); |
| 1808 | }); |
| 1809 | |
| 1810 | for (const [commandName, commandDef] of Object.entries(mockCommands)) { |
| 1811 | const {command, usage} = buildCommand(commandName, commandDef.args); |
| 1812 | parser.command( |
| 1813 | command, |
| 1814 | commandDef.description, |
| 1815 | y => { |
| 1816 | y.usage(usage); |
| 1817 | y.option('output-format', { |
| 1818 | choices: ['md', 'json'], |
| 1819 | default: 'md', |
| 1820 | }); |
| 1821 | registerCommandArgs(y, commandDef.args); |
| 1822 | }, |
| 1823 | argv => { |
| 1824 | const commandArgs: Record<string, unknown> = {}; |
| 1825 | for (const argName of Object.keys(commandDef.args)) { |
| 1826 | if (argName in argv) { |
| 1827 | commandArgs[argName] = restore(argv[argName]); |
| 1828 | } |
| 1829 | } |
| 1830 | parsedArgs = commandArgs; |
| 1831 | }, |
| 1832 | ); |
| 1833 | } |
| 1834 | |
| 1835 | parser.parse(); |
| 1836 | assert.ok(parsedArgs, `Command did not match: ${rawArgs.join(' ')}`); |
| 1837 | return parsedArgs; |
| 1838 | } |
| 1839 | |
| 1840 | it('parses an omitted variadic array arg without minItems as an empty array', () => { |
| 1841 | assert.deepStrictEqual(parseToolCommand(['allow_empty_array', '2']), { |
no test coverage detected