-
Notifications
You must be signed in to change notification settings - Fork 620
/
Copy pathCommandLineStringParameter.ts
101 lines (87 loc) · 3.14 KB
/
CommandLineStringParameter.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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import type { ICommandLineStringDefinition } from './CommandLineDefinition';
import { CommandLineParameterWithArgument, CommandLineParameterKind } from './BaseClasses';
/**
* The data type returned by {@link CommandLineParameterProvider.(defineStringParameter:2)}.
* @public
*/
export interface IRequiredCommandLineStringParameter extends CommandLineStringParameter {
readonly value: string;
}
/**
* The data type returned by {@link CommandLineParameterProvider.(defineStringParameter:1)}.
* @public
*/
export class CommandLineStringParameter extends CommandLineParameterWithArgument {
/** {@inheritDoc ICommandLineStringDefinition.defaultValue} */
public readonly defaultValue: string | undefined;
private _value: string | undefined = undefined;
/** {@inheritDoc CommandLineParameter.kind} */
public readonly kind: CommandLineParameterKind.String = CommandLineParameterKind.String;
/** @internal */
public constructor(definition: ICommandLineStringDefinition) {
super(definition);
this.defaultValue = definition.defaultValue;
this.validateDefaultValue(!!this.defaultValue);
}
/**
* {@inheritDoc CommandLineParameter._setValue}
* @internal
*/
public _setValue(data: unknown): 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);
}
}
}