-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
argv_parser.ts
242 lines (225 loc) · 6.86 KB
/
argv_parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { Command } from 'commander'
import path from 'path'
import { dialects } from '@cucumber/gherkin'
import { SnippetInterface } from '../formatter/step_definition_snippet_builder/snippet_syntax'
// Using require instead of import so compiled typescript will have the desired folder structure
const { version } = require('../../package.json') // eslint-disable-line @typescript-eslint/no-var-requires
export interface IParsedArgvFormatRerunOptions {
separator?: string
}
export interface IParsedArgvFormatOptions {
colorsEnabled?: boolean
rerun?: IParsedArgvFormatRerunOptions
snippetInterface?: SnippetInterface
snippetSyntax?: string
[customKey: string]: any
}
export interface IParsedArgvOptions {
backtrace: boolean
config: string
dryRun: boolean
exit: boolean
failFast: boolean
format: string[]
formatOptions: IParsedArgvFormatOptions
i18nKeywords: string
i18nLanguages: boolean
language: string
name: string[]
order: string
parallel: number
predictableIds: boolean
profile: string[]
publish: boolean
publishQuiet: boolean
require: string[]
requireModule: string[]
retry: number
retryTagFilter: string
strict: boolean
tags: string
worldParameters: object
}
export interface IParsedArgv {
args: string[]
options: IParsedArgvOptions
}
const ArgvParser = {
collect<T>(val: T, memo: T[]): T[] {
memo.push(val)
return memo
},
mergeJson(option: string): (str: string, memo: object) => object {
return function (str: string, memo: object) {
let val: object
try {
val = JSON.parse(str)
} catch (error) {
const e: Error = error
throw new Error(`${option} passed invalid JSON: ${e.message}: ${str}`)
}
if (typeof val !== 'object' || Array.isArray(val)) {
throw new Error(`${option} must be passed JSON of an object: ${str}`)
}
return { ...memo, ...val }
}
},
mergeTags(value: string, memo: string): string {
return memo === '' ? `(${value})` : `${memo} and (${value})`
},
validateCountOption(value: string, optionName: string): number {
const numericValue = parseInt(value)
if (isNaN(numericValue) || numericValue < 0) {
throw new Error(`${optionName} must be a non negative integer`)
}
return numericValue
},
validateLanguage(value: string): string {
if (!Object.keys(dialects).includes(value)) {
throw new Error(`Unsupported ISO 639-1: ${value}`)
}
return value
},
validateRetryOptions(options: IParsedArgvOptions): void {
if (options.retryTagFilter !== '' && options.retry === 0) {
throw new Error(
'a positive --retry count must be specified when setting --retry-tag-filter'
)
}
},
parse(argv: string[]): IParsedArgv {
const program = new Command(path.basename(argv[1]))
program
.storeOptionsAsProperties(false)
.usage('[options] [<GLOB|DIR|FILE[:LINE]>...]')
.version(version, '-v, --version')
.option('-b, --backtrace', 'show full backtrace for errors')
.option('-c, --config <TYPE[:PATH]>', 'specify configuration file')
.option(
'-d, --dry-run',
'invoke formatters without executing steps',
false
)
.option(
'--exit',
'force shutdown of the event loop when the test run has finished: cucumber will call process.exit',
false
)
.option('--fail-fast', 'abort the run on first failure', false)
.option(
'-f, --format <TYPE[:PATH]>',
'specify the output format, optionally supply PATH to redirect formatter output (repeatable)',
ArgvParser.collect,
[]
)
.option(
'--format-options <JSON>',
'provide options for formatters (repeatable)',
ArgvParser.mergeJson('--format-options'),
{}
)
.option(
'--i18n-keywords <ISO 639-1>',
'list language keywords',
ArgvParser.validateLanguage,
''
)
.option('--i18n-languages', 'list languages', false)
.option(
'--language <ISO 639-1>',
'provide the default language for feature files',
'en'
)
.option(
'--name <REGEXP>',
'only execute the scenarios with name matching the expression (repeatable)',
ArgvParser.collect,
[]
)
.option('--no-strict', 'succeed even if there are pending steps')
.option(
'--order <TYPE[:SEED]>',
'run scenarios in the specified order. Type should be `defined` or `random`',
'defined'
)
.option(
'-p, --profile <NAME>',
'specify the profile to use (repeatable)',
ArgvParser.collect,
[]
)
.option(
'--parallel <NUMBER_OF_WORKERS>',
'run in parallel with the given number of workers',
(val) => ArgvParser.validateCountOption(val, '--parallel'),
0
)
.option(
'--predictable-ids',
'Use predictable ids in messages (option ignored if using parallel)',
false
)
.option(
'--publish',
'Publish a report to https://reports.cucumber.io',
false
)
.option(
'--publish-quiet',
"Don't print information banner about publishing reports",
false
)
.option(
'-r, --require <GLOB|DIR|FILE>',
'require files before executing features (repeatable)',
ArgvParser.collect,
[]
)
.option(
'--require-module <NODE_MODULE>',
'require node modules before requiring files (repeatable)',
ArgvParser.collect,
[]
)
.option(
'--retry <NUMBER_OF_RETRIES>',
'specify the number of times to retry failing test cases (default: 0)',
(val) => ArgvParser.validateCountOption(val, '--retry'),
0
)
.option(
'--retry-tag-filter <EXPRESSION>',
`only retries the features or scenarios with tags matching the expression (repeatable).
This option requires '--retry' to be specified.`,
ArgvParser.mergeTags,
''
)
.option(
'-t, --tags <EXPRESSION>',
'only execute the features or scenarios with tags matching the expression (repeatable)',
ArgvParser.mergeTags,
''
)
.option(
'--world-parameters <JSON>',
'provide parameters that will be passed to the world constructor (repeatable)',
ArgvParser.mergeJson('--world-parameters'),
{}
)
program.on('--help', () => {
/* eslint-disable no-console */
console.log(
' For more details please visit https://github.com/cucumber/cucumber-js/blob/master/docs/cli.md\n'
)
/* eslint-enable no-console */
})
program.parse(argv)
const options: IParsedArgvOptions = program.opts()
ArgvParser.validateRetryOptions(options)
return {
options,
args: program.args,
}
},
}
export default ArgvParser