forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandLineParameter.ts
More file actions
620 lines (538 loc) · 20.5 KB
/
CommandLineParameter.ts
File metadata and controls
620 lines (538 loc) · 20.5 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import {
IBaseCommandLineDefinition,
ICommandLineFlagDefinition,
ICommandLineStringDefinition,
ICommandLineStringListDefinition,
ICommandLineIntegerDefinition,
ICommandLineChoiceDefinition,
IBaseCommandLineDefinitionWithArgument
} from './CommandLineDefinition';
/**
* Identifies the kind of a CommandLineParameter.
* @public
*/
export enum CommandLineParameterKind {
/** Indicates a CommandLineChoiceParameter */
Choice,
/** Indicates a CommandLineFlagParameter */
Flag,
/** Indicates a CommandLineIntegerParameter */
Integer,
/** Indicates a CommandLineStringParameter */
String,
/** Indicates a CommandLineStringListParameter */
StringList
}
/**
* The base class for the various command-line parameter types.
* @public
*/
export abstract class CommandLineParameter {
// Example: "--do-something"
private static _longNameRegExp: RegExp = /^-(-[a-z0-9]+)+$/;
// Example: "-d"
private static _shortNameRegExp: RegExp = /^-[a-zA-Z]$/;
// "Environment variable names used by the utilities in the Shell and Utilities volume of
// IEEE Std 1003.1-2001 consist solely of uppercase letters, digits, and the '_' (underscore)
// from the characters defined in Portable Character Set and do not begin with a digit."
// Example: "THE_SETTING"
private static _environmentVariableRegExp: RegExp = /^[A-Z_][A-Z0-9_]*$/;
/**
* A unique internal key used to retrieve the value from the parser's dictionary.
* @internal
*/
public _parserKey: string;
/** {@inheritDoc IBaseCommandLineDefinition.parameterLongName} */
public readonly longName: string;
/** {@inheritDoc IBaseCommandLineDefinition.parameterShortName} */
public readonly shortName: string | undefined;
/** {@inheritDoc IBaseCommandLineDefinition.description} */
public readonly description: string;
/** {@inheritDoc IBaseCommandLineDefinition.required} */
public readonly required: boolean;
/** {@inheritDoc IBaseCommandLineDefinition.environmentVariable} */
public readonly environmentVariable: string | undefined;
/** @internal */
constructor(definition: IBaseCommandLineDefinition) {
this.longName = definition.parameterLongName;
this.shortName = definition.parameterShortName;
this.description = definition.description;
this.required = !!definition.required;
this.environmentVariable = definition.environmentVariable;
if (!CommandLineParameter._longNameRegExp.test(this.longName)) {
throw new Error(`Invalid name: "${this.longName}". The parameter long name must be`
+ ` lower-case and use dash delimiters (e.g. "--do-a-thing")`);
}
if (this.shortName) {
if (!CommandLineParameter._shortNameRegExp.test(this.shortName)) {
throw new Error(`Invalid name: "${this.shortName}". The parameter short name must be`
+ ` a dash followed by a single upper-case or lower-case letter (e.g. "-a")`);
}
}
if (this.environmentVariable) {
if (this.required) {
throw new Error(`An "environmentVariable" cannot be specified for "${this.longName}"`
+ ` because it is a required parameter`);
}
if (!CommandLineParameter._environmentVariableRegExp.test(this.environmentVariable)) {
throw new Error(`Invalid environment variable name: "${this.environmentVariable}". The name must`
+ ` consist only of upper-case letters, numbers, and underscores. It may not start with a number.`);
}
}
}
/**
* Called internally by CommandLineParameterProvider._processParsedData()
* @internal
*/
public abstract _setValue(data: any): void; // tslint:disable-line:no-any
/**
* Returns additional text used by the help formatter.
* @internal
*/
public _getSupplementaryNotes(supplementaryNotes: string[]): void { // virtual
if (this.environmentVariable !== undefined) {
supplementaryNotes.push('This parameter may alternatively specified via the ' + this.environmentVariable
+ ' environment variable.');
}
}
/**
* Indicates the type of parameter.
*/
public abstract get kind(): CommandLineParameterKind;
/**
* Append the parsed values to the provided string array.
* @remarks
* Sometimes a command line parameter is not used directly, but instead gets passed through to another
* tool that will use it. For example if our parameter comes in as "--max-count 3", then we might want to
* call `child_process.spawn()` and append ["--max-count", "3"] to the args array for that tool.
* appendToArgList() appends zero or more strings to the provided array, based on the input command-line
* that we parsed.
*
* If the parameter was omitted from our command-line and has no default value, then
* nothing will be appended. If the short name was used, the long name will be appended instead.
* @param argList - the parsed strings will be appended to this string array
*/
public abstract appendToArgList(argList: string[]): void;
/**
* Internal usage only. Used to report unexpected output from the argparse library.
*/
protected reportInvalidData(data: any): never { // tslint:disable-line:no-any
throw new Error(`Unexpected data object for parameter "${this.longName}": `
+ JSON.stringify(data));
}
protected validateDefaultValue(hasDefaultValue: boolean): void {
if (this.required && hasDefaultValue) {
// If a parameter is "required", then the user understands that they always need to
// specify a value for this parameter (either via the command line or via an environment variable).
// It would be confusing to allow a default value that sometimes allows the "required" parameter
// to be omitted. If you sometimes don't have a suitable default value, then the better approach
// is to throw a custom error explaining why the parameter is required in that case.
throw new Error(`A default value cannot be specified for "${this.longName}"`
+ ` because it is a "required" parameter`);
}
}
}
/**
* The common base class for parameters types that receive an argument.
*
* @remarks
* An argument is an accompanying command-line token, such as "123" in the
* example "--max-count 123".
* @public
*/
export abstract class CommandLineParameterWithArgument extends CommandLineParameter {
// Matches the first character that *isn't* part of a valid upper-case argument name such as "URL_2"
private static _invalidArgumentNameRegExp: RegExp = /[^A-Z_0-9]/;
/** {@inheritDoc IBaseCommandLineDefinitionWithArgument.argumentName} */
public readonly argumentName: string;
/** @internal */
constructor(definition: IBaseCommandLineDefinitionWithArgument) {
super(definition);
if (definition.argumentName === '') {
throw new Error('The argument name cannot be an empty string. (For the default name, specify undefined.)');
}
if (definition.argumentName.toUpperCase() !== definition.argumentName) {
throw new Error(`Invalid name: "${definition.argumentName}". The argument name must be all upper case.`);
}
const match: RegExpMatchArray | null = definition.argumentName.match(
CommandLineParameterWithArgument._invalidArgumentNameRegExp);
if (match) {
throw new Error(`The argument name "${definition.argumentName}" contains an invalid character "${match[0]}".`
+ ` Only upper-case letters, numbers, and underscores are allowed.`);
}
this.argumentName = definition.argumentName;
}
}
/**
* The data type returned by {@link CommandLineParameterProvider.defineChoiceParameter}.
* @public
*/
export class CommandLineChoiceParameter extends CommandLineParameter {
/** {@inheritDoc ICommandLineChoiceDefinition.alternatives} */
public readonly alternatives: ReadonlyArray<string>;
/** {@inheritDoc ICommandLineStringDefinition.defaultValue} */
public readonly defaultValue: string | undefined;
private _value: string | undefined = undefined;
/** @internal */
constructor(definition: ICommandLineChoiceDefinition) {
super(definition);
if (definition.alternatives.length <= 1) {
throw new Error(`When defining a choice parameter, the alternatives list must contain at least one value.`);
}
if (definition.defaultValue && definition.alternatives.indexOf(definition.defaultValue) === -1) {
throw new Error(`The specified default value "${definition.defaultValue}"`
+ ` is not one of the available options: ${definition.alternatives.toString()}`);
}
this.alternatives = definition.alternatives;
this.defaultValue = definition.defaultValue;
this.validateDefaultValue(!!this.defaultValue);
}
/** {@inheritDoc CommandLineParameter.kind} */
public get kind(): CommandLineParameterKind {
return CommandLineParameterKind.Choice;
}
/**
* {@inheritDoc CommandLineParameter._setValue}
* @internal
*/
// tslint:disable-next-line:no-any
public _setValue(data: any): void { // abstract
if (data !== null && data !== undefined) {
if (typeof data !== 'string') {
this.reportInvalidData(data);
}
this._value = data;
return;
}
if (this.environmentVariable !== undefined) {
// Try reading the environment variable
const environmentValue: string | undefined = process.env[this.environmentVariable];
if (environmentValue !== undefined && environmentValue !== '') {
if (this.alternatives.indexOf(environmentValue) < 0) {
const choices: string = '"' + this.alternatives.join('", "') + '"';
throw new Error(`Invalid value "${environmentValue}" for the environment variable`
+ ` ${this.environmentVariable}. Valid choices are: ${choices}`);
}
this._value = environmentValue;
return;
}
}
if (this.defaultValue !== undefined) {
this._value = this.defaultValue;
return;
}
this._value = undefined;
}
/**
* {@inheritDoc CommandLineParameter._getSupplementaryNotes}
* @internal
*/
public _getSupplementaryNotes(supplementaryNotes: string[]): void { // virtual
super._getSupplementaryNotes(supplementaryNotes);
if (this.defaultValue !== undefined) {
supplementaryNotes.push(`The default value is "${this.defaultValue}".`);
}
}
/**
* Returns the argument value for a choice parameter that was parsed from the command line.
*
* @remarks
* The return value will be `undefined` if the command-line has not been parsed yet,
* or if the parameter was omitted and has no default value.
*/
public get value(): string | undefined {
return this._value;
}
/** {@inheritDoc CommandLineParameter.appendToArgList} @override */
public appendToArgList(argList: string[]): void {
if (this.value !== undefined) {
argList.push(this.longName);
argList.push(this.value);
}
}
}
/**
* The data type returned by {@link CommandLineParameterProvider.defineFlagParameter}.
* @public
*/
export class CommandLineFlagParameter extends CommandLineParameter {
private _value: boolean = false;
/** @internal */
constructor(definition: ICommandLineFlagDefinition) {
super(definition);
}
/** {@inheritDoc CommandLineParameter.kind} */
public get kind(): CommandLineParameterKind {
return CommandLineParameterKind.Flag;
}
/**
* {@inheritDoc CommandLineParameter._setValue}
* @internal
*/
// tslint:disable-next-line:no-any
public _setValue(data: any): void { // abstract
if (data !== null && data !== undefined) {
if (typeof data !== 'boolean') {
this.reportInvalidData(data);
}
this._value = data;
return;
}
if (this.environmentVariable !== undefined) {
// Try reading the environment variable
const environmentValue: string | undefined = process.env[this.environmentVariable];
if (environmentValue !== undefined && environmentValue !== '') {
if (environmentValue !== '0' && environmentValue !== '1') {
throw new Error(`Invalid value "${environmentValue}" for the environment variable`
+ ` ${this.environmentVariable}. Valid choices are 0 or 1.`);
}
this._value = environmentValue === '1';
return;
}
}
this._value = false;
}
/**
* Returns a boolean indicating whether the parameter was included in the command line.
*
* @remarks
* The return value will be false if the command-line has not been parsed yet,
* or if the flag was not used.
*/
public get value(): boolean {
return this._value;
}
/** {@inheritDoc CommandLineParameter.appendToArgList} @override */
public appendToArgList(argList: string[]): void {
if (this.value) {
argList.push(this.longName);
}
}
}
/**
* The data type returned by {@link CommandLineParameterProvider.defineIntegerParameter}.
* @public
*/
export class CommandLineIntegerParameter extends CommandLineParameterWithArgument {
/** {@inheritDoc ICommandLineStringDefinition.defaultValue} */
public readonly defaultValue: number | undefined;
private _value: number | undefined = undefined;
/** @internal */
constructor(definition: ICommandLineIntegerDefinition) {
super(definition);
this.defaultValue = definition.defaultValue;
this.validateDefaultValue(!!this.defaultValue);
}
/** {@inheritDoc CommandLineParameter.kind} */
public get kind(): CommandLineParameterKind {
return CommandLineParameterKind.Integer;
}
/**
* {@inheritDoc CommandLineParameter._setValue}
* @internal
*/
// tslint:disable-next-line:no-any
public _setValue(data: any): void { // abstract
if (data !== null && data !== undefined) {
if (typeof data !== 'number') {
this.reportInvalidData(data);
}
this._value = data;
return;
}
if (this.environmentVariable !== undefined) {
// Try reading the environment variable
const environmentValue: string | undefined = process.env[this.environmentVariable];
if (environmentValue !== undefined && environmentValue !== '') {
const parsed: number = parseInt(environmentValue, 10);
if (isNaN(parsed) || environmentValue.indexOf('.') >= 0) {
throw new Error(`Invalid value "${environmentValue}" for the environment variable`
+ ` ${this.environmentVariable}. It must be an integer value.`);
}
this._value = parsed;
return;
}
}
if (this.defaultValue !== undefined) {
this._value = this.defaultValue;
return;
}
this._value = undefined;
}
/**
* {@inheritDoc CommandLineParameter._getSupplementaryNotes}
* @internal
*/
public _getSupplementaryNotes(supplementaryNotes: string[]): void { // virtual
super._getSupplementaryNotes(supplementaryNotes);
if (this.defaultValue !== undefined) {
supplementaryNotes.push(`The default value is ${this.defaultValue}.`);
}
}
/**
* Returns the argument value for an integer parameter that was parsed from the command line.
*
* @remarks
* The return value will be undefined if the command-line has not been parsed yet,
* or if the parameter was omitted and has no default value.
*/
public get value(): number | undefined {
return this._value;
}
/** {@inheritDoc CommandLineParameter.appendToArgList} @override */
public appendToArgList(argList: string[]): void {
if (this.value !== undefined) {
argList.push(this.longName);
argList.push(this.value.toString());
}
}
}
/**
* The data type returned by {@link CommandLineParameterProvider.defineStringParameter}.
* @public
*/
export class CommandLineStringParameter extends CommandLineParameterWithArgument {
/** {@inheritDoc ICommandLineStringDefinition.defaultValue} */
public readonly defaultValue: string | undefined;
private _value: string | undefined = undefined;
/** @internal */
constructor(definition: ICommandLineStringDefinition) {
super(definition);
this.defaultValue = definition.defaultValue;
this.validateDefaultValue(!!this.defaultValue);
}
/** {@inheritDoc CommandLineParameter.kind} */
public get kind(): CommandLineParameterKind {
return CommandLineParameterKind.String;
}
/**
* {@inheritDoc CommandLineParameter._setValue}
* @internal
*/
// tslint:disable-next-line:no-any
public _setValue(data: any): void { // abstract
if (data !== null && data !== undefined) {
if (typeof data !== 'string') {
this.reportInvalidData(data);
}
this._value = data;
return;
}
if (this.environmentVariable !== undefined) {
// Try reading the environment variable
const environmentValue: string | undefined = process.env[this.environmentVariable];
if (environmentValue !== undefined) {
// NOTE: If the environment variable is defined as an empty string,
// here we will accept the empty string as our value. (For number/flag we don't do that.)
this._value = environmentValue;
return;
}
}
if (this.defaultValue !== undefined) {
this._value = this.defaultValue;
return;
}
this._value = undefined;
}
/**
* {@inheritDoc CommandLineParameter._getSupplementaryNotes}
* @internal
*/
public _getSupplementaryNotes(supplementaryNotes: string[]): void { // virtual
super._getSupplementaryNotes(supplementaryNotes);
if (this.defaultValue !== undefined) {
if (this.defaultValue.length < 160) {
supplementaryNotes.push(`The default value is ${JSON.stringify(this.defaultValue)}.`);
}
}
}
/**
* Returns the argument value for a string parameter that was parsed from the command line.
*
* @remarks
* The return value will be undefined if the command-line has not been parsed yet,
* or if the parameter was omitted and has no default value.
*/
public get value(): string | undefined {
return this._value;
}
/** {@inheritDoc CommandLineParameter.appendToArgList} @override */
public appendToArgList(argList: string[]): void {
if (this.value !== undefined) {
argList.push(this.longName);
argList.push(this.value);
}
}
}
/**
* The data type returned by {@link CommandLineParameterProvider.defineStringListParameter}.
* @public
*/
export class CommandLineStringListParameter extends CommandLineParameterWithArgument {
private _values: string[] = [];
/** @internal */
constructor(definition: ICommandLineStringListDefinition) {
super(definition);
}
/** {@inheritDoc CommandLineParameter.kind} */
public get kind(): CommandLineParameterKind {
return CommandLineParameterKind.StringList;
}
/**
* {@inheritDoc CommandLineParameter._setValue}
* @internal
*/
// tslint:disable-next-line:no-any
public _setValue(data: any): void { // abstract
if (data !== null && data !== undefined) {
if (!Array.isArray(data)) {
this.reportInvalidData(data);
}
for (const arrayItem of data) {
if (typeof(arrayItem) !== 'string') {
this.reportInvalidData(data);
}
}
this._values = data;
return;
}
if (this.environmentVariable !== undefined) {
// Try reading the environment variable
const environmentValue: string | undefined = process.env[this.environmentVariable];
if (environmentValue !== undefined) {
// NOTE: If the environment variable is defined as an empty string,
// here we will accept the empty string as our value. (For number/flag we don't do that.)
// In the current implementation, the environment variable for a "string list" can only
// store a single item. If we wanted to allow multiple items (and still have a conventional-seeming
// environment), we would ask the caller to provide an appropriate delimiter. Getting involved
// with escaping here seems unwise, since there are so many shell escaping mechanisms that could
// potentially confuse the experience.
this._values = [ environmentValue ];
return;
}
}
// (No default value for string lists)
this._values = [];
}
/**
* Returns the string arguments for a string list parameter that was parsed from the command line.
*
* @remarks
* The array will be empty if the command-line has not been parsed yet,
* or if the parameter was omitted and has no default value.
*/
public get values(): ReadonlyArray<string> {
return this._values;
}
/** {@inheritDoc CommandLineParameter.appendToArgList} @override */
public appendToArgList(argList: string[]): void {
if (this.values.length > 0) {
for (const value of this.values) {
argList.push(this.longName);
argList.push(value);
}
}
}
}