command-line-director 1.0.2 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG CHANGED
@@ -4,6 +4,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [1.0.3] - 2025-01-12
8
+
9
+ ### Fixes
10
+ - Bump versions
11
+
7
12
  ## [1.0.2] - 2025-30-11
8
13
 
9
14
  ### Fixes
@@ -0,0 +1,121 @@
1
+ declare class ArgumentLookup {
2
+ values: string[];
3
+ keyValues: Map<string, string | number | boolean | null>;
4
+ constructor();
5
+ }
6
+
7
+ declare enum CommandLineArgumentDataType {
8
+ Boolean = 0,
9
+ Number = 1,
10
+ String = 2
11
+ }
12
+
13
+ declare enum CommandLineArgumentType {
14
+ KeyValue = 0,
15
+ Value = 1
16
+ }
17
+
18
+ declare class CommandLineArgument {
19
+ propertyName: string;
20
+ required: boolean;
21
+ argumentName: string | null;
22
+ alias: string | null;
23
+ dataType: CommandLineArgumentDataType;
24
+ type: CommandLineArgumentType;
25
+ defaultValue: string | number | boolean | null;
26
+ allowedValues: string[] | number[] | boolean[] | null;
27
+ regularExpression: RegExp | null;
28
+ description: string | null;
29
+ constructor(propertyName: string, required: boolean, argumentName: string | null, alias: string | null, dataType: CommandLineArgumentDataType | null, type: CommandLineArgumentType, defaultValue?: string | number | boolean | null, allowedValues?: string[] | number[] | boolean[] | null, regularExpression?: RegExp | null, description?: string | null);
30
+ toString(): string;
31
+ }
32
+
33
+ declare class Command {
34
+ identifier: string;
35
+ commandLineArguments: Array<CommandLineArgument>;
36
+ values: Map<string, string | number | boolean | null>;
37
+ constructor(identifier: string, commandLineArguments: Array<CommandLineArgument>, values: Map<string, string | number | boolean | null>);
38
+ }
39
+
40
+ declare class CommandLine {
41
+ identifier: string;
42
+ title: string;
43
+ description: string;
44
+ commandLineArguments: CommandLineArgument[];
45
+ /**
46
+ * CommandLine constructor
47
+ * @param identifier - string
48
+ * @param commandLineArguments - array
49
+ * @thows Error
50
+ */
51
+ constructor(identifier: string, title: string, description: string, commandLineArguments: CommandLineArgument[]);
52
+ /**
53
+ * Create lookup table for process arguments
54
+ * @result object
55
+ */
56
+ private parseDataType;
57
+ /**
58
+ * Create command from argument lookup
59
+ * @param argument lookup
60
+ * @result command
61
+ * @throws Error
62
+ */
63
+ commandFromLookup(argumentLookup: ArgumentLookup): Command;
64
+ }
65
+
66
+ declare class CommandLineArgumentFactory {
67
+ /**s
68
+ * Create string argument
69
+ * Examples:
70
+ * --dir="/var/www/test"
71
+ * -d="/var/www/test"
72
+ *
73
+ * @param propertyName - string
74
+ * @param description - string
75
+ * @param required - boolean
76
+ * @param argumentName - string
77
+ * @param alias - string
78
+ * @param defaultValue - any
79
+ * @param allowedValues
80
+ * @param regularExpression
81
+ */
82
+ keyValueArgument(propertyName: string, description: string, required: boolean, argumentName: string, alias: string, defaultValue?: string | number | boolean | null, allowedValues?: string[] | number[] | boolean[] | null, regularExpression?: RegExp | null): CommandLineArgument;
83
+ /**
84
+ * Create boolean argument
85
+ * Examples:
86
+ * --force
87
+ * -f
88
+ *
89
+ * @param propertyName - string
90
+ * @param description - string
91
+ * @param argumentName - string
92
+ * @param alias - string
93
+ */
94
+ flagArgument(propertyName: string, description: string, argumentName: string, alias: string): CommandLineArgument;
95
+ /**
96
+ * Create command
97
+ * Examples:
98
+ * init
99
+ *
100
+ * @param propertyName - string
101
+ * @param description
102
+ * @param required - boolean
103
+ * @param allowedValues
104
+ * @param regularExpression
105
+ */
106
+ valueArgument(propertyName: string, description: string, required: boolean, allowedValues?: string[] | number[] | boolean[] | null, regularExpression?: RegExp | null): CommandLineArgument;
107
+ }
108
+
109
+ declare class CommandLineDirector {
110
+ private title;
111
+ private description;
112
+ private commandLines;
113
+ constructor(title: string, description: string, commandLines: CommandLine[]);
114
+ private removeQuotes;
115
+ private createArgumentLookupFromProcessArguments;
116
+ parseArguments(cmdArguments: string[], verbose: boolean): Command | null;
117
+ parse(verbose?: boolean): Command | null;
118
+ generateHelp(): string;
119
+ }
120
+
121
+ export { ArgumentLookup, Command, CommandLine, CommandLineArgument, CommandLineArgumentDataType, CommandLineArgumentFactory, CommandLineArgumentType, CommandLineDirector };
package/dist/index.js ADDED
@@ -0,0 +1,393 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ ArgumentLookup: () => ArgumentLookup,
24
+ Command: () => Command,
25
+ CommandLine: () => CommandLine,
26
+ CommandLineArgument: () => CommandLineArgument,
27
+ CommandLineArgumentDataType: () => CommandLineArgumentDataType,
28
+ CommandLineArgumentFactory: () => CommandLineArgumentFactory,
29
+ CommandLineArgumentType: () => CommandLineArgumentType,
30
+ CommandLineDirector: () => CommandLineDirector
31
+ });
32
+ module.exports = __toCommonJS(src_exports);
33
+
34
+ // src/argument-lookup.ts
35
+ var ArgumentLookup = class {
36
+ constructor() {
37
+ this.values = [];
38
+ this.keyValues = /* @__PURE__ */ new Map();
39
+ }
40
+ };
41
+
42
+ // src/command.ts
43
+ var Command = class {
44
+ constructor(identifier, commandLineArguments, values) {
45
+ this.identifier = identifier;
46
+ this.commandLineArguments = commandLineArguments;
47
+ this.values = values;
48
+ }
49
+ };
50
+
51
+ // src/command-line-argument-data-type.ts
52
+ var CommandLineArgumentDataType = /* @__PURE__ */ ((CommandLineArgumentDataType2) => {
53
+ CommandLineArgumentDataType2[CommandLineArgumentDataType2["Boolean"] = 0] = "Boolean";
54
+ CommandLineArgumentDataType2[CommandLineArgumentDataType2["Number"] = 1] = "Number";
55
+ CommandLineArgumentDataType2[CommandLineArgumentDataType2["String"] = 2] = "String";
56
+ return CommandLineArgumentDataType2;
57
+ })(CommandLineArgumentDataType || {});
58
+
59
+ // src/command-line-argument-type.ts
60
+ var CommandLineArgumentType = /* @__PURE__ */ ((CommandLineArgumentType2) => {
61
+ CommandLineArgumentType2[CommandLineArgumentType2["KeyValue"] = 0] = "KeyValue";
62
+ CommandLineArgumentType2[CommandLineArgumentType2["Value"] = 1] = "Value";
63
+ return CommandLineArgumentType2;
64
+ })(CommandLineArgumentType || {});
65
+
66
+ // src/command-line.ts
67
+ var CommandLine = class {
68
+ /**
69
+ * CommandLine constructor
70
+ * @param identifier - string
71
+ * @param commandLineArguments - array
72
+ * @thows Error
73
+ */
74
+ constructor(identifier, title, description, commandLineArguments) {
75
+ this.identifier = identifier;
76
+ this.title = title;
77
+ this.description = description;
78
+ this.commandLineArguments = [];
79
+ if (Array.isArray(commandLineArguments)) {
80
+ this.commandLineArguments = commandLineArguments;
81
+ } else {
82
+ throw new Error("Invalid argument 'commandLineArguments', value should be of type 'array'");
83
+ }
84
+ }
85
+ /**
86
+ * Create lookup table for process arguments
87
+ * @result object
88
+ */
89
+ parseDataType(value, dataType) {
90
+ let parsedValue = null;
91
+ if (value !== void 0 && value !== null) {
92
+ switch (dataType) {
93
+ case 0 /* Boolean */:
94
+ parsedValue = Boolean(value);
95
+ break;
96
+ case 1 /* Number */:
97
+ parsedValue = Number(value);
98
+ if (isNaN(parsedValue)) {
99
+ parsedValue = null;
100
+ }
101
+ break;
102
+ case 2 /* String */:
103
+ parsedValue = String(value);
104
+ break;
105
+ default: {
106
+ throw new Error(`Unknown data type '${dataType}.`);
107
+ }
108
+ }
109
+ if (parsedValue === null) {
110
+ throw new Error(`Could not parse value '${value}' to data type '${dataType}.`);
111
+ }
112
+ }
113
+ return parsedValue;
114
+ }
115
+ /**
116
+ * Create command from argument lookup
117
+ * @param argument lookup
118
+ * @result command
119
+ * @throws Error
120
+ */
121
+ commandFromLookup(argumentLookup) {
122
+ const result = /* @__PURE__ */ new Map();
123
+ const values = argumentLookup.values;
124
+ const keyValues = argumentLookup.keyValues;
125
+ let valueIndex = 0;
126
+ this.commandLineArguments.forEach((arg) => {
127
+ var _a, _b, _c;
128
+ if (arg.type === 0 /* KeyValue */) {
129
+ if (arg.argumentName && keyValues.has(arg.argumentName)) {
130
+ result.set(arg.propertyName, (_a = keyValues.get(arg.argumentName)) != null ? _a : null);
131
+ } else if (arg.alias && keyValues.has(arg.alias)) {
132
+ result.set(arg.propertyName, (_b = keyValues.get(arg.alias)) != null ? _b : null);
133
+ }
134
+ } else if (arg.type === 1 /* Value */) {
135
+ if (values.length > valueIndex) {
136
+ result.set(arg.propertyName, values[valueIndex]);
137
+ valueIndex++;
138
+ }
139
+ } else {
140
+ throw new Error("Invalid command line argument type");
141
+ }
142
+ if (result.has(arg.propertyName) === false && arg.defaultValue !== null) {
143
+ result.set(arg.propertyName, arg.defaultValue);
144
+ }
145
+ if (result.has(arg.propertyName)) {
146
+ result.set(arg.propertyName, this.parseDataType((_c = result.get(arg.propertyName)) != null ? _c : null, arg.dataType));
147
+ }
148
+ });
149
+ this.commandLineArguments.forEach(function(arg) {
150
+ var _a;
151
+ if (arg.required === true && (result.get(arg.propertyName) === void 0 || result.get(arg.propertyName) === null)) {
152
+ throw new Error(`Required field '${arg.propertyName}' is missing.`);
153
+ }
154
+ if (arg.allowedValues && arg.allowedValues.length > 0 && (arg.required === true || result.has(arg.propertyName) && result.get(arg.propertyName) !== null)) {
155
+ if (((_a = arg.allowedValues) == null ? void 0 : _a.indexOf(result.get(arg.propertyName))) === -1) {
156
+ throw new Error(`Value for field '${arg.propertyName}' is not allowed.`);
157
+ }
158
+ }
159
+ ;
160
+ if (arg.regularExpression && (arg.required === true || result.has(arg.propertyName) && result.get(arg.propertyName) !== null)) {
161
+ if (!arg.regularExpression.test(result.get(arg.propertyName))) {
162
+ throw new Error(`Value for field field '${arg.propertyName}' is invalid.`);
163
+ }
164
+ }
165
+ });
166
+ return new Command(this.identifier, this.commandLineArguments, result);
167
+ }
168
+ };
169
+
170
+ // src/command-line-argument.ts
171
+ var CommandLineArgument = class {
172
+ constructor(propertyName, required, argumentName, alias, dataType, type, defaultValue = null, allowedValues = null, regularExpression = null, description = null) {
173
+ if (!propertyName) {
174
+ throw new Error("'propertyName' parameter not defined.");
175
+ }
176
+ if (required === void 0 || required === null) {
177
+ throw new Error("'required' parameter not defined.");
178
+ }
179
+ if (type !== 0 /* KeyValue */ && type !== 1 /* Value */) {
180
+ throw new Error("'type' parameter invalid.");
181
+ }
182
+ if (!argumentName && type === 0 /* KeyValue */) {
183
+ throw new Error("'argumentName' parameter not defined.");
184
+ }
185
+ if (allowedValues && Array.isArray(allowedValues) === false) {
186
+ throw new Error("'allowedValues' parameter should be an array.");
187
+ }
188
+ this.propertyName = propertyName;
189
+ this.required = Boolean(required);
190
+ this.argumentName = argumentName;
191
+ this.alias = alias !== void 0 ? alias : "";
192
+ this.dataType = dataType != null ? dataType : 2 /* String */;
193
+ this.type = type;
194
+ this.defaultValue = defaultValue;
195
+ this.allowedValues = allowedValues != null ? allowedValues : [];
196
+ this.regularExpression = regularExpression != null ? regularExpression : null;
197
+ this.description = description;
198
+ }
199
+ toString() {
200
+ let help = "";
201
+ let title = "";
202
+ if (this.argumentName) {
203
+ title = ` ${this.argumentName},${this.alias}`;
204
+ } else if (this.allowedValues) {
205
+ title = ` ${this.allowedValues}`;
206
+ } else {
207
+ title = " <string>";
208
+ }
209
+ help += title;
210
+ for (let i = 0; i < 24 - title.length; i++) {
211
+ help += " ";
212
+ }
213
+ help += ` ${this.required ? "R" : "O"} | ${this.dataType}`;
214
+ if (this.argumentName && this.allowedValues) {
215
+ help += ` | VALUES = ${this.allowedValues}`;
216
+ }
217
+ if (this.regularExpression) {
218
+ help += ` | PATTERN = ${this.regularExpression.toString()}`;
219
+ }
220
+ help += ` | ${this.description}`;
221
+ return help;
222
+ }
223
+ };
224
+
225
+ // src/command-line-argument-factory.ts
226
+ var CommandLineArgumentFactory = class {
227
+ /**s
228
+ * Create string argument
229
+ * Examples:
230
+ * --dir="/var/www/test"
231
+ * -d="/var/www/test"
232
+ *
233
+ * @param propertyName - string
234
+ * @param description - string
235
+ * @param required - boolean
236
+ * @param argumentName - string
237
+ * @param alias - string
238
+ * @param defaultValue - any
239
+ * @param allowedValues
240
+ * @param regularExpression
241
+ */
242
+ keyValueArgument(propertyName, description, required, argumentName, alias, defaultValue = null, allowedValues = null, regularExpression = null) {
243
+ return new CommandLineArgument(
244
+ propertyName,
245
+ required,
246
+ argumentName,
247
+ alias,
248
+ 2 /* String */,
249
+ 0 /* KeyValue */,
250
+ defaultValue,
251
+ allowedValues,
252
+ regularExpression,
253
+ description
254
+ );
255
+ }
256
+ /**
257
+ * Create boolean argument
258
+ * Examples:
259
+ * --force
260
+ * -f
261
+ *
262
+ * @param propertyName - string
263
+ * @param description - string
264
+ * @param argumentName - string
265
+ * @param alias - string
266
+ */
267
+ flagArgument(propertyName, description, argumentName, alias) {
268
+ return new CommandLineArgument(
269
+ propertyName,
270
+ false,
271
+ argumentName,
272
+ alias,
273
+ 0 /* Boolean */,
274
+ 0 /* KeyValue */,
275
+ false,
276
+ null,
277
+ null,
278
+ description
279
+ );
280
+ }
281
+ /**
282
+ * Create command
283
+ * Examples:
284
+ * init
285
+ *
286
+ * @param propertyName - string
287
+ * @param description
288
+ * @param required - boolean
289
+ * @param allowedValues
290
+ * @param regularExpression
291
+ */
292
+ valueArgument(propertyName, description, required, allowedValues = null, regularExpression = null) {
293
+ return new CommandLineArgument(
294
+ propertyName,
295
+ required,
296
+ null,
297
+ null,
298
+ 2 /* String */,
299
+ 1 /* Value */,
300
+ null,
301
+ allowedValues,
302
+ regularExpression,
303
+ description
304
+ );
305
+ }
306
+ };
307
+
308
+ // src/command-line-director.ts
309
+ var CommandLineDirector = class {
310
+ constructor(title, description, commandLines) {
311
+ this.title = title;
312
+ this.description = description;
313
+ if (Array.isArray(commandLines)) {
314
+ this.commandLines = commandLines;
315
+ } else {
316
+ throw new Error("Invalid argument 'commandLines', value should be of type 'array'");
317
+ }
318
+ }
319
+ removeQuotes(text) {
320
+ return text.replace(/['"]+/g, "");
321
+ }
322
+ createArgumentLookupFromProcessArguments(cmdArguments) {
323
+ const lookup = new ArgumentLookup();
324
+ cmdArguments.forEach((cmdArgument) => {
325
+ const keyValue = cmdArgument.split("=");
326
+ if (typeof keyValue[0] === "string" && keyValue[0].charAt(0) === "-") {
327
+ const value = keyValue.length === 2 ? this.removeQuotes(keyValue[1]) : true;
328
+ const keyName = keyValue[0];
329
+ lookup.keyValues.set(keyName, value);
330
+ } else {
331
+ lookup.values.push(this.removeQuotes(cmdArgument));
332
+ }
333
+ });
334
+ return lookup;
335
+ }
336
+ parseArguments(cmdArguments, verbose) {
337
+ const lookup = this.createArgumentLookupFromProcessArguments(cmdArguments);
338
+ let command = null;
339
+ for (let i = 0; i < this.commandLines.length; i++) {
340
+ const commandLine = this.commandLines[i];
341
+ try {
342
+ command = commandLine.commandFromLookup(lookup);
343
+ break;
344
+ } catch (error) {
345
+ if (verbose) {
346
+ if (error instanceof Error) {
347
+ console.info(error.message);
348
+ } else {
349
+ console.info(String(error));
350
+ }
351
+ }
352
+ command = null;
353
+ }
354
+ }
355
+ ;
356
+ return command;
357
+ }
358
+ parse(verbose = false) {
359
+ return this.parseArguments(process.argv.slice(2), verbose);
360
+ }
361
+ generateHelp() {
362
+ let help = "\r\n=======================================================================\r\n";
363
+ help += `\r
364
+ ${this.title.toUpperCase()}\r
365
+ ${this.description}\r
366
+ \r
367
+ `;
368
+ for (const commandLine of this.commandLines) {
369
+ help += "-----------------------------------------------------------------------\r\n";
370
+ help += `${commandLine.title.toUpperCase()} - ${commandLine.description}\r
371
+ \r
372
+ `;
373
+ for (const argument of commandLine.commandLineArguments) {
374
+ help += `${argument.toString()}\r
375
+ `;
376
+ }
377
+ help += "\r\n";
378
+ }
379
+ help += "=======================================================================\r\n";
380
+ return help;
381
+ }
382
+ };
383
+ // Annotate the CommonJS export names for ESM import in node:
384
+ 0 && (module.exports = {
385
+ ArgumentLookup,
386
+ Command,
387
+ CommandLine,
388
+ CommandLineArgument,
389
+ CommandLineArgumentDataType,
390
+ CommandLineArgumentFactory,
391
+ CommandLineArgumentType,
392
+ CommandLineDirector
393
+ });