-
Notifications
You must be signed in to change notification settings - Fork 15
/
option-string-builder.ts
156 lines (121 loc) · 4.94 KB
/
option-string-builder.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
import { StringUtils } from "andculturecode-javascript-core";
import { Echo } from "../modules/echo";
import shell from "shelljs";
import { Formatters } from "../modules/formatters";
// -----------------------------------------------------------------------------------------
// #region Public Functions
// -----------------------------------------------------------------------------------------
class OptionStringBuilder {
// -----------------------------------------------------------------------------------------
// #region Public Members
// -----------------------------------------------------------------------------------------
public option: string;
public shortFlag: string;
// #endregion Public Members
// -----------------------------------------------------------------------------------------
// #region Constructor
// -----------------------------------------------------------------------------------------
/**
* Constructor function to an `OptionStringBuilder` object based on the given option & short flag
*
* @param {string} [option=""] Full option or 'verbose' flag to trigger the option's behavior.
* If not provided, it assumes the shortFlag or option will be set with subsequent function calls.
* Note: "--" does not need to be provided, and will be stripped out.
* @param {string} [shortFlag=""] Short flag to trigger the option's behavior.
* If not provided, it assumes the option or shortFlag will be set with subsequent function calls.
* Note: "-" does not need to be provided, and will be stripped out.
* @returns {OptionStringBuilder}
*/
constructor(option?: string, shortFlag?: string) {
this.option = _transformOption(_stripDashes(option));
this.shortFlag = _transformShortFlag(_stripDashes(shortFlag));
}
// #endregion Constructor
// -----------------------------------------------------------------------------------------
// #region Public Functions
// -----------------------------------------------------------------------------------------
public toArray(): string[] {
_validateOrExit(this.option, this.shortFlag);
return _buildArray(this.option, this.shortFlag);
}
public toString(): string {
_validateOrExit(this.option, this.shortFlag);
return this.toArray().join(", ");
}
public withOption(option: string): OptionStringBuilder {
this.option = _transformOption(_stripDashes(option));
return this;
}
public withShortFlag(shortFlag: string): OptionStringBuilder {
this.shortFlag = _transformShortFlag(_stripDashes(shortFlag));
return this;
}
// #endregion Public Functions
}
// #endregion Public Functions
// -----------------------------------------------------------------------------------------
// #region Private Functions
// -----------------------------------------------------------------------------------------
const _buildArray = (option?: string, shortFlag?: string): string[] => {
const optionsArray = [];
if (StringUtils.hasValue(shortFlag)) {
optionsArray.push(shortFlag!);
}
if (StringUtils.hasValue(option)) {
optionsArray.push(option!);
}
return optionsArray;
};
const _transformOption = (option?: string): string => {
if (StringUtils.isEmpty(option)) {
return option ?? "";
}
return `--${option}`;
};
const _transformShortFlag = (shortFlag?: string): string => {
if (StringUtils.isEmpty(shortFlag)) {
return shortFlag ?? "";
}
return `-${shortFlag}`;
};
const _stripDashes = (input?: string): string => {
if (StringUtils.isEmpty(input)) {
return "";
}
// We know at this point that input has a value
input = input!;
// Strip out any dashes from the incoming option/shortFlag
if (input.startsWith("--")) {
return input.replace(/--/, "");
}
if (input.startsWith("-")) {
return input.replace(/-/, "");
}
return input;
};
const _validateFlagOrExit = (shortFlag = "") => {
shortFlag = _stripDashes(shortFlag);
if (StringUtils.hasValue(shortFlag) && shortFlag.length > 1) {
Echo.error(
`Short flag can only be 1 character in length. Received: ${Formatters.red(
shortFlag
)}`
);
shell.exit(1);
}
};
const _validateOrExit = (option = "", shortFlag = "") => {
option = _stripDashes(option);
shortFlag = _stripDashes(shortFlag);
if (StringUtils.isEmpty(option) && StringUtils.isEmpty(shortFlag)) {
Echo.error("Option or short flag is required");
shell.exit(1);
}
_validateFlagOrExit(shortFlag);
};
// #endregion Private Functions
// -----------------------------------------------------------------------------------------
// #region Exports
// -----------------------------------------------------------------------------------------
export { OptionStringBuilder };
// #endregion Exports