(
command: Command,
argv: string[],
)
| 59 | options: command.options || {}, |
| 60 | strict: true, |
| 61 | allowPositionals: true, |
| 62 | }); |
| 63 | const args = positionals.slice(4); |
| 64 | validateCommandArguments(command, args, options); |
| 65 | return { options, args }; |
| 66 | } catch (error) { |
| 67 | if (error instanceof InvalidCommandArgumentsError) { |
| 68 | throw error; |
| 69 | } |
| 70 | if (error instanceof TypeError) { |
| 71 | printCommandUsage(command); |
| 72 | throw new InvalidCommandArgumentsError(error.message); |
| 73 | } |
| 74 | throw error; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | function validateCommandArguments(command: Command, args: string[], values: { [name: string]: unknown }) { |
| 79 | if (values['help']) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (command.args) { |
| 84 | const hasVariableArgs = command.args.some((arg) => arg.endsWith('...')); |
| 85 | const hasExpectedArgs = hasVariableArgs |
| 86 | ? args.length >= command.args.length |
| 87 | : args.length === command.args.length; |
| 88 | if (!hasExpectedArgs) { |
| 89 | printCommandUsage(command); |
| 90 | throw new InvalidCommandArgumentsError(`Expected ${command.args.length} arguments, got ${args.length}`); |
| 91 | } |
| 92 | } |
no test coverage detected