| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ function addCommand(cli: CAC | Command, name: string, option: CLIOption<any>) { | |||
| 22 | 22 | `Expected a single value for option "${command}", received [${received}]`, | |
| 23 | 23 | ) | |
| 24 | 24 | } | |
| 25 | + value = removeQuotes(value) | ||
| 25 | 26 | if (option.transform) { | |
| 26 | 27 | return option.transform(value) | |
| 27 | 28 | } | |
@@ -196,11 +197,36 @@ export function createCLI(options: CliParseOptions = {}): CAC { | |||
| 196 | 197 | return cli | |
| 197 | 198 | } | |
| 198 | 199 | ||
| 200 | + function removeQuotes<T>(str: T): T { | ||
| 201 | + if (typeof str !== 'string') { | ||
| 202 | + if (Array.isArray(str)) { | ||
| 203 | + return str.map(removeQuotes) as unknown as T | ||
| 204 | + } | ||
| 205 | + return str | ||
| 206 | + } | ||
| 207 | + if (str.startsWith('"') && str.endsWith('"')) { | ||
| 208 | + return str.slice(1, -1) as unknown as T | ||
| 209 | + } | ||
| 210 | + if (str.startsWith(`'`) && str.endsWith(`'`)) { | ||
| 211 | + return str.slice(1, -1) as unknown as T | ||
| 212 | + } | ||
| 213 | + return str | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + function splitArgv(argv: string): string[] { | ||
| 217 | + const reg = /(['"])(?:(?!\1).)+\1/g | ||
| 218 | + argv = argv.replace(reg, match => match.replace(/\s/g, '\x00')) | ||
| 219 | + return argv.split(' ').map((arg: string) => { | ||
| 220 | + arg = arg.replace(/\0/g, ' ') | ||
| 221 | + return removeQuotes(arg) | ||
| 222 | + }) | ||
| 223 | + } | ||
| 224 | + | ||
| 199 | 225 | export function parseCLI(argv: string | string[], config: CliParseOptions = {}): { | |
| 200 | 226 | filter: string[] | |
| 201 | 227 | options: CliOptions | |
| 202 | 228 | } { | |
| 203 | - const arrayArgs = typeof argv === 'string' ? argv.split(' ') : argv | ||
| 229 | + const arrayArgs = typeof argv === 'string' ? splitArgv(argv) : argv | ||
| 204 | 230 | if (arrayArgs[0] !== 'vitest') { | |
| 205 | 231 | throw new Error(`Expected "vitest" as the first argument, received "${arrayArgs[0]}"`) | |
| 206 | 232 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -411,6 +411,51 @@ test('public parseCLI works correctly', () => { | |||
| 411 | 411 | }, | |
| 412 | 412 | }) | |
| 413 | 413 | ||
| 414 | + expect(parseCLI('vitest --project="space 1"')).toEqual({ | ||
| 415 | + filter: [], | ||
| 416 | + options: { | ||
| 417 | + 'project': ['space 1'], | ||
| 418 | + '--': [], | ||
| 419 | + 'color': true, | ||
| 420 | + }, | ||
| 421 | + }) | ||
| 422 | + | ||
| 423 | + expect(parseCLI('vitest "--project=space 1"')).toEqual({ | ||
| 424 | + filter: [], | ||
| 425 | + options: { | ||
| 426 | + 'project': ['space 1'], | ||
| 427 | + '--': [], | ||
| 428 | + 'color': true, | ||
| 429 | + }, | ||
| 430 | + }) | ||
| 431 | + | ||
| 432 | + expect(parseCLI('vitest --project "space 1"')).toEqual({ | ||
| 433 | + filter: [], | ||
| 434 | + options: { | ||
| 435 | + 'project': ['space 1'], | ||
| 436 | + '--': [], | ||
| 437 | + 'color': true, | ||
| 438 | + }, | ||
| 439 | + }) | ||
| 440 | + | ||
| 441 | + expect(parseCLI('vitest --project="space 1" --project="space 2"')).toEqual({ | ||
| 442 | + filter: [], | ||
| 443 | + options: { | ||
| 444 | + 'project': ['space 1', 'space 2'], | ||
| 445 | + '--': [], | ||
| 446 | + 'color': true, | ||
| 447 | + }, | ||
| 448 | + }) | ||
| 449 | + | ||
| 450 | + expect(parseCLI('vitest ./test-1.js ./test-2.js --project="space 1" --project="space 2" --project="space 3"')).toEqual({ | ||
| 451 | + filter: ['./test-1.js', './test-2.js'], | ||
| 452 | + options: { | ||
| 453 | + 'project': ['space 1', 'space 2', 'space 3'], | ||
| 454 | + '--': [], | ||
| 455 | + 'color': true, | ||
| 456 | + }, | ||
| 457 | + }) | ||
| 458 | + | ||
| 414 | 459 | expect(parseCLI('vitest --exclude=docs --exclude=demo')).toEqual({ | |
| 415 | 460 | filter: [], | |
| 416 | 461 | options: { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments