* Parse argv
(
argv?: string[],
{
run = true,
}: {
/** Whether to run the action for matched command */
run?: boolean | undefined
} = {},
)
| 172 | * Parse argv |
| 173 | */ |
| 174 | parse( |
| 175 | argv?: string[], |
| 176 | { |
| 177 | run = true, |
| 178 | }: { |
| 179 | /** Whether to run the action for matched command */ |
| 180 | run?: boolean | undefined |
| 181 | } = {}, |
| 182 | ): ParsedArgv { |
| 183 | if (!argv) { |
| 184 | if (!runtimeProcessArgs) { |
| 185 | throw new Error( |
| 186 | 'No argv provided and runtime process argv is not available.', |
| 187 | ) |
| 188 | } |
| 189 | argv = runtimeProcessArgs |
| 190 | } |
| 191 | |
| 192 | this.rawArgs = argv |
| 193 | if (!this.name) { |
| 194 | this.name = argv[1] ? getFileName(argv[1]) : 'cli' |
| 195 | } |
| 196 | |
| 197 | let shouldParse = true |
| 198 | |
| 199 | // Search sub-commands |
| 200 | for (const command of this.commands) { |
| 201 | const parsed = this.mri(argv.slice(2), command) |
| 202 | |
| 203 | const commandName = parsed.args[0] |
| 204 | if (command.isMatched(commandName)) { |
| 205 | shouldParse = false |
| 206 | const parsedInfo = { |
| 207 | ...parsed, |
| 208 | args: parsed.args.slice(1), |
| 209 | } |
| 210 | this.setParsedInfo(parsedInfo, command, commandName) |
| 211 | this.dispatchEvent( |
| 212 | new CustomEvent(`command:${commandName}`, { detail: command }), |
| 213 | ) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | if (shouldParse) { |
| 218 | // Search the default command |
| 219 | for (const command of this.commands) { |
| 220 | if (command.isDefaultCommand) { |
| 221 | shouldParse = false |
| 222 | const parsed = this.mri(argv.slice(2), command) |
| 223 | this.setParsedInfo(parsed, command) |
| 224 | this.dispatchEvent(new CustomEvent('command:!', { detail: command })) |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (shouldParse) { |
| 230 | const parsed = this.mri(argv.slice(2)) |
| 231 | this.setParsedInfo(parsed) |
no test coverage detected