@jakubmazanec/args 2.0.16-next.fac374fa → 2.0.16-unstable.2468857d
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/build/ParsingError.js +13 -7
- package/build/ValidationError.js +18 -12
- package/build/internals/Checker.js +49 -95
- package/build/internals/CommandScope.js +42 -44
- package/build/internals/OptionScope.js +50 -68
- package/build/internals/castValue.js +22 -22
- package/build/internals/commandToArguments.js +16 -13
- package/build/internals/createOptionShortNameMap.js +14 -10
- package/build/internals/expandArguments.js +16 -12
- package/build/internals/isCommand.js +13 -5
- package/build/internals/isFalseString.js +11 -4
- package/build/internals/isLongOption.js +12 -4
- package/build/internals/isOptionLike.js +12 -4
- package/build/internals/isShortOption.js +12 -4
- package/build/internals/isShortOptionsGroup.js +13 -5
- package/build/internals/isTrueString.js +11 -4
- package/build/main.js +5 -5
- package/build/parseArguments.js +155 -231
- package/package.json +7 -7
- package/build/Arguments.js +0 -2
- package/build/Arguments.js.map +0 -7
- package/build/ArgumentsCommand.js +0 -2
- package/build/ArgumentsCommand.js.map +0 -7
- package/build/ArgumentsOptions.js +0 -2
- package/build/ArgumentsOptions.js.map +0 -7
- package/build/ArgumentsParameters.js +0 -2
- package/build/ArgumentsParameters.js.map +0 -7
- package/build/ArgumentsUnknownOptions.js +0 -2
- package/build/ArgumentsUnknownOptions.js.map +0 -7
- package/build/Argv.js +0 -2
- package/build/Argv.js.map +0 -7
- package/build/CommandConfig.js +0 -2
- package/build/CommandConfig.js.map +0 -7
- package/build/CommandsConfig.js +0 -2
- package/build/CommandsConfig.js.map +0 -7
- package/build/LongOptionName.js +0 -2
- package/build/LongOptionName.js.map +0 -7
- package/build/OptionConfig.js +0 -2
- package/build/OptionConfig.js.map +0 -7
- package/build/OptionsConfig.js +0 -2
- package/build/OptionsConfig.js.map +0 -7
- package/build/ParameterConfig.js +0 -2
- package/build/ParameterConfig.js.map +0 -7
- package/build/ParametersConfig.js +0 -2
- package/build/ParametersConfig.js.map +0 -7
- package/build/ParserConfig.js +0 -2
- package/build/ParserConfig.js.map +0 -7
- package/build/ParsingError.js.map +0 -7
- package/build/ShortOptionName.js +0 -2
- package/build/ShortOptionName.js.map +0 -7
- package/build/ValidationError.js.map +0 -7
- package/build/internals/Checker.js.map +0 -7
- package/build/internals/CommandScope.js.map +0 -7
- package/build/internals/NormalizedCommandConfig.js +0 -2
- package/build/internals/NormalizedCommandConfig.js.map +0 -7
- package/build/internals/NormalizedCommandsConfig.js +0 -2
- package/build/internals/NormalizedCommandsConfig.js.map +0 -7
- package/build/internals/OptionScope.js.map +0 -7
- package/build/internals/castValue.js.map +0 -7
- package/build/internals/commandToArguments.js.map +0 -7
- package/build/internals/createOptionShortNameMap.js.map +0 -7
- package/build/internals/expandArguments.js.map +0 -7
- package/build/internals/isCommand.js.map +0 -7
- package/build/internals/isFalseString.js.map +0 -7
- package/build/internals/isLongOption.js.map +0 -7
- package/build/internals/isOptionLike.js.map +0 -7
- package/build/internals/isShortOption.js.map +0 -7
- package/build/internals/isShortOptionsGroup.js.map +0 -7
- package/build/internals/isTrueString.js.map +0 -7
- package/build/internals.js +0 -16
- package/build/internals.js.map +0 -7
- package/build/main.js.map +0 -7
- package/build/parseArguments.js.map +0 -7
package/build/ParsingError.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
import { createCustomError } from "@jakubmazanec/error";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
|
|
3
|
+
//#region source/ParsingError.ts
|
|
4
|
+
/**
|
|
5
|
+
* A subclass of `Error` that indicates a parsing failure.
|
|
6
|
+
*/
|
|
7
|
+
const ParsingError = createCustomError("ParsingError", {
|
|
8
|
+
INVALID_PARAMETER: "Invalid parameter value \"{0}\" found. Allowed values for parameter \"{1}\" are: {2}.",
|
|
9
|
+
UNKNOWN_PARAMETER: "Unknown parameter \"{0}\" found. Unknown parameters are not allowed.",
|
|
10
|
+
INVALID_OPTION: "Invalid option value \"{0}\" found. Allowed values for option \"{1}\" are: {2}.",
|
|
11
|
+
UNKNOWN_OPTION: "Unknown option \"{0}\" found. Unknown options are not allowed."
|
|
8
12
|
});
|
|
9
|
-
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { ParsingError };
|
package/build/ValidationError.js
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
import { createCustomError } from "@jakubmazanec/error";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
|
|
3
|
+
//#region source/ValidationError.ts
|
|
4
|
+
/**
|
|
5
|
+
* A subclass of `Error` that indicates a validation issue.
|
|
6
|
+
*/
|
|
7
|
+
const ValidationError = createCustomError("ValidationError", {
|
|
8
|
+
INVALID_COMMAND: "Command \"{0}\" has invalid format. Only words consisting of letters, numbers or dashes separated by single space are allowed.",
|
|
9
|
+
MISSING_PARAMETER: "Parameter \"{0}\" is required but its value is undefined.",
|
|
10
|
+
INVALID_PARAMETER_DEFAULT_AND_CHOICES: "Parameter \"{0}\" has invalid default value \"{1}\". Default value must be one of the `choices` elements.",
|
|
11
|
+
INVALID_PARAMETER_ORDER: "Parameter \"{0}\" has invalid order. All parameters before must have a default value or be required too",
|
|
12
|
+
MISSING_OPTION: "Option \"{0}\" is required but its value is undefined.",
|
|
13
|
+
INVALID_OPTION_DEFAULT_AND_CHOICES: "Option \"{0}\" has invalid default value \"{1}\". Default value must be one of the `choices` elements.",
|
|
14
|
+
INVALID_OPTION_NAME: "Option \"{0}\" has invalid name. Only names consisting of letters, numbers or dashes are allowed.",
|
|
15
|
+
REPEATED_OPTION_NAME: "Option \"{0}\" has invalid short name \"{1}\". Short option names must be unique.",
|
|
16
|
+
INVALID_OPTION_ARITY: "Option \"{0}\" has invalid number of values. Found \"{1}\", require \"{2}\"."
|
|
13
17
|
});
|
|
14
|
-
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { ValidationError };
|
|
@@ -1,98 +1,52 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
import { ValidationError } from "../ValidationError.js";
|
|
3
2
|
import { isCommand } from "./isCommand.js";
|
|
4
3
|
import { isLongOption } from "./isLongOption.js";
|
|
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
|
-
if ((optionConfig.type === "string" || optionConfig.type === "number" && !optionConfig.count) && optionConfig.choices && optionConfig.defaultValue !== void 0 && !optionConfig.choices.includes(optionConfig.defaultValue)) {
|
|
55
|
-
this.addError(
|
|
56
|
-
new ValidationError("INVALID_OPTION_DEFAULT_AND_CHOICES", {
|
|
57
|
-
messageParameters: [optionName, optionConfig.defaultValue]
|
|
58
|
-
})
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
checkParsedParameters(parametersConfig, parsedParameters) {
|
|
64
|
-
for (let [i, parameterConfig] of parametersConfig.entries()) {
|
|
65
|
-
let parsedParameter = parsedParameters[i];
|
|
66
|
-
if (parameterConfig.required === true && parsedParameter === void 0) {
|
|
67
|
-
this.addError(
|
|
68
|
-
new ValidationError("MISSING_PARAMETER", {
|
|
69
|
-
messageParameters: [parameterConfig.label ?? i]
|
|
70
|
-
})
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
checkParsedOptions(optionsConfig, parsedOptions) {
|
|
76
|
-
for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {
|
|
77
|
-
let parsedOption = parsedOptions[optionName];
|
|
78
|
-
if (optionConfig.required && parsedOption === void 0) {
|
|
79
|
-
this.addError(
|
|
80
|
-
new ValidationError("MISSING_OPTION", {
|
|
81
|
-
messageParameters: [optionName]
|
|
82
|
-
})
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
if (parsedOption !== void 0 && (optionConfig.type === "string" && optionConfig.multiple || optionConfig.type === "number" && optionConfig.multiple) && typeof optionConfig.arity === "number" && Array.isArray(parsedOption) && parsedOption.length > 0 && parsedOption.length !== optionConfig.arity) {
|
|
86
|
-
this.addError(
|
|
87
|
-
new ValidationError("INVALID_OPTION_ARITY", {
|
|
88
|
-
messageParameters: [optionName, parsedOption.length, optionConfig.arity]
|
|
89
|
-
})
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
addError(error) {
|
|
95
|
-
this.errors.push(error);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
//# sourceMappingURL=Checker.js.map
|
|
4
|
+
|
|
5
|
+
//#region source/internals/Checker.ts
|
|
6
|
+
/**
|
|
7
|
+
* `Checker` is used for handling various checks before, during, and after parsing of the arguments.
|
|
8
|
+
*/
|
|
9
|
+
var Checker = class {
|
|
10
|
+
errors = [];
|
|
11
|
+
checkCommandsConfig(commandsConfig) {
|
|
12
|
+
for (let commandConfig of commandsConfig) if (!isCommand(commandConfig)) this.addError(new ValidationError("INVALID_COMMAND", { messageParameters: [commandConfig] }));
|
|
13
|
+
}
|
|
14
|
+
checkParametersConfig(parametersConfig) {
|
|
15
|
+
for (let [i, parameterConfig] of parametersConfig.entries()) {
|
|
16
|
+
if (parameterConfig.type !== "boolean" && parameterConfig.choices && parameterConfig.defaultValue !== void 0 && !parameterConfig.choices.includes(parameterConfig.defaultValue)) this.addError(new ValidationError("INVALID_PARAMETER_DEFAULT_AND_CHOICES", { messageParameters: [parameterConfig.label ?? i, parameterConfig.defaultValue] }));
|
|
17
|
+
if (i >= 1 && parameterConfig.required === true && !parametersConfig.slice(0, i).every((previousParameterConfig) => previousParameterConfig.required === true || previousParameterConfig.defaultValue !== void 0)) this.addError(new ValidationError("INVALID_PARAMETER_ORDER", { messageParameters: [parameterConfig.label ?? i] }));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
checkOptionsConfig(optionsConfig) {
|
|
21
|
+
let usedShortOptionNames = /* @__PURE__ */ new Set([]);
|
|
22
|
+
for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {
|
|
23
|
+
if (!isLongOption(`--${optionName}`)) this.addError(new ValidationError("INVALID_OPTION_NAME", { messageParameters: [optionName] }));
|
|
24
|
+
if (optionConfig.shortName && usedShortOptionNames.has(optionConfig.shortName)) this.addError(new ValidationError("REPEATED_OPTION_NAME", { messageParameters: [optionName, optionConfig.shortName] }));
|
|
25
|
+
else if (optionConfig.shortName) usedShortOptionNames.add(optionConfig.shortName);
|
|
26
|
+
if ((optionConfig.type === "string" || optionConfig.type === "number" && !optionConfig.count) && optionConfig.choices && optionConfig.defaultValue !== void 0 && !optionConfig.choices.includes(optionConfig.defaultValue)) this.addError(new ValidationError("INVALID_OPTION_DEFAULT_AND_CHOICES", { messageParameters: [optionName, optionConfig.defaultValue] }));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
checkParsedParameters(parametersConfig, parsedParameters) {
|
|
30
|
+
for (let [i, parameterConfig] of parametersConfig.entries()) {
|
|
31
|
+
let parsedParameter = parsedParameters[i];
|
|
32
|
+
if (parameterConfig.required === true && parsedParameter === void 0) this.addError(new ValidationError("MISSING_PARAMETER", { messageParameters: [parameterConfig.label ?? i] }));
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
checkParsedOptions(optionsConfig, parsedOptions) {
|
|
36
|
+
for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {
|
|
37
|
+
let parsedOption = parsedOptions[optionName];
|
|
38
|
+
if (optionConfig.required && parsedOption === void 0) this.addError(new ValidationError("MISSING_OPTION", { messageParameters: [optionName] }));
|
|
39
|
+
if (parsedOption !== void 0 && (optionConfig.type === "string" && optionConfig.multiple || optionConfig.type === "number" && optionConfig.multiple) && typeof optionConfig.arity === "number" && Array.isArray(parsedOption) && parsedOption.length > 0 && parsedOption.length !== optionConfig.arity) this.addError(new ValidationError("INVALID_OPTION_ARITY", { messageParameters: [
|
|
40
|
+
optionName,
|
|
41
|
+
parsedOption.length,
|
|
42
|
+
optionConfig.arity
|
|
43
|
+
] }));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
addError(error) {
|
|
47
|
+
this.errors.push(error);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
//#endregion
|
|
52
|
+
export { Checker };
|
|
@@ -1,44 +1,42 @@
|
|
|
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
|
-
//# sourceMappingURL=CommandScope.js.map
|
|
1
|
+
//#region source/internals/CommandScope.ts
|
|
2
|
+
/**
|
|
3
|
+
* `CommandScope` handles storing information about parsing of a command.
|
|
4
|
+
*/
|
|
5
|
+
var CommandScope = class {
|
|
6
|
+
args;
|
|
7
|
+
config;
|
|
8
|
+
constructor(arg, normalizedCommandsConfig) {
|
|
9
|
+
let normalizedArgument = arg.toLowerCase();
|
|
10
|
+
this.config = normalizedCommandsConfig.find((commandConfig) => commandConfig.args[0] === normalizedArgument) ?? [];
|
|
11
|
+
this.args = [normalizedArgument];
|
|
12
|
+
}
|
|
13
|
+
captureArg(arg) {
|
|
14
|
+
this.args.push(arg.toLowerCase());
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
isComplete() {
|
|
18
|
+
let isComplete = true;
|
|
19
|
+
for (let [i, arg] of this.config.args.entries()) if (this.args[i] !== arg) {
|
|
20
|
+
isComplete = false;
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
return isComplete;
|
|
24
|
+
}
|
|
25
|
+
isDone() {
|
|
26
|
+
let isDone = false;
|
|
27
|
+
for (let [i, arg] of this.config.args.entries()) if (this.args[i] !== arg) {
|
|
28
|
+
isDone = true;
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
return isDone;
|
|
32
|
+
}
|
|
33
|
+
getValue() {
|
|
34
|
+
return this.config.command;
|
|
35
|
+
}
|
|
36
|
+
getArgs() {
|
|
37
|
+
return [...this.args];
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
export { CommandScope };
|
|
@@ -1,69 +1,51 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
import _ from "lodash";
|
|
3
|
-
import { ParsingError } from "../ParsingError.js";
|
|
4
1
|
import { castValue } from "./castValue.js";
|
|
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
|
-
if (config.type === "boolean" && !config.multiple && !values.length) {
|
|
56
|
-
values = [true];
|
|
57
|
-
} else if (config.type === "number" && config.count && !values.length) {
|
|
58
|
-
values = [1];
|
|
59
|
-
}
|
|
60
|
-
if (config.multiple && previousParsedValue) {
|
|
61
|
-
values = [...previousParsedValue, ...values];
|
|
62
|
-
}
|
|
63
|
-
if (config.type === "number" && config.count && previousParsedValue) {
|
|
64
|
-
values = [values[0] + previousParsedValue];
|
|
65
|
-
}
|
|
66
|
-
return config.multiple ? values : values[0];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
//# sourceMappingURL=OptionScope.js.map
|
|
2
|
+
import { ParsingError } from "../ParsingError.js";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
|
|
5
|
+
//#region source/internals/OptionScope.ts
|
|
6
|
+
/**
|
|
7
|
+
* `CommandScope` handles storing information about parsing of an option..
|
|
8
|
+
*/
|
|
9
|
+
var OptionScope = class {
|
|
10
|
+
name;
|
|
11
|
+
values = [];
|
|
12
|
+
previousParsedValue;
|
|
13
|
+
config;
|
|
14
|
+
constructor(optionName, optionsConfig, allowUnknownOptions = false) {
|
|
15
|
+
this.name = optionName;
|
|
16
|
+
if (this.name.includes("-")) this.name = _.camelCase(this.name);
|
|
17
|
+
let optionConfig = optionsConfig[this.name];
|
|
18
|
+
if (!optionConfig) {
|
|
19
|
+
if (!allowUnknownOptions) throw new ParsingError("UNKNOWN_OPTION", { messageParameters: [this.name] });
|
|
20
|
+
optionConfig = { type: "string" };
|
|
21
|
+
}
|
|
22
|
+
this.config = optionConfig;
|
|
23
|
+
}
|
|
24
|
+
captureArg(arg) {
|
|
25
|
+
let value = castValue(arg, this.config.type);
|
|
26
|
+
if ((this.config.type === "number" && !this.config.count || this.config.type === "string") && this.config.choices !== void 0 && !this.config.choices.includes(value)) throw new ParsingError("INVALID_OPTION", { messageParameters: [
|
|
27
|
+
value,
|
|
28
|
+
this.name,
|
|
29
|
+
this.config.choices.map((choice) => `"${choice}"`).join(", ")
|
|
30
|
+
] });
|
|
31
|
+
if (this.config.multiple) this.values.push(value);
|
|
32
|
+
else this.values = [value];
|
|
33
|
+
}
|
|
34
|
+
isComplete() {
|
|
35
|
+
if (!this.config.multiple && this.values.length === 1) return true;
|
|
36
|
+
if (this.config.multiple && typeof this.config.arity === "number" && this.values.length + (this.previousParsedValue ?? []).length === this.config.arity) return true;
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
getValue() {
|
|
40
|
+
let { config, previousParsedValue } = this;
|
|
41
|
+
let { values } = this;
|
|
42
|
+
if (config.type === "boolean" && !config.multiple && !values.length) values = [true];
|
|
43
|
+
else if (config.type === "number" && config.count && !values.length) values = [1];
|
|
44
|
+
if (config.multiple && previousParsedValue) values = [...previousParsedValue, ...values];
|
|
45
|
+
if (config.type === "number" && config.count && previousParsedValue) values = [values[0] + previousParsedValue];
|
|
46
|
+
return config.multiple ? values : values[0];
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
//#endregion
|
|
51
|
+
export { OptionScope };
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
import { isFalseString } from "./isFalseString.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
2
|
+
|
|
3
|
+
//#region source/internals/castValue.ts
|
|
4
|
+
/**
|
|
5
|
+
* A function that casts string value to a specified type.
|
|
6
|
+
*
|
|
7
|
+
* @param value The value to be converted.
|
|
8
|
+
* @param type The type to convert the value into.
|
|
9
|
+
* */
|
|
10
|
+
function castValue(value, targetType) {
|
|
11
|
+
switch (targetType) {
|
|
12
|
+
case "boolean": {
|
|
13
|
+
let booleanString = String(value).toLowerCase();
|
|
14
|
+
return !(isFalseString(booleanString) || booleanString === "");
|
|
15
|
+
}
|
|
16
|
+
case "number": return Number.parseFloat(value);
|
|
17
|
+
case "string": return String(value);
|
|
18
|
+
case void 0: return String(value);
|
|
19
|
+
default: return String(value);
|
|
20
|
+
}
|
|
23
21
|
}
|
|
24
|
-
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { castValue };
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
//#region source/internals/commandToArguments.ts
|
|
2
|
+
/**
|
|
3
|
+
* Converts a string representing a command into the arguments, i.e. array of strings.
|
|
4
|
+
*
|
|
5
|
+
* @param value The value to convert.
|
|
6
|
+
* @return Arguments.
|
|
7
|
+
*/
|
|
8
|
+
function commandToArguments(value) {
|
|
9
|
+
let rawArgs = value.split(" ");
|
|
10
|
+
let args = [];
|
|
11
|
+
for (let [, rawValue] of rawArgs.entries()) if (rawValue === "" && args.at(-1)) args[args.length - 1] += " ";
|
|
12
|
+
else args.push(rawValue);
|
|
13
|
+
return args;
|
|
13
14
|
}
|
|
14
|
-
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { commandToArguments };
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
//#region source/internals/createOptionShortNameMap.ts
|
|
2
|
+
/**
|
|
3
|
+
* Converts options config to a map (i.e. an object) of short option names (object keys) to long option names (values).
|
|
4
|
+
*
|
|
5
|
+
* @optionsConfig Options config to create the map from.
|
|
6
|
+
* @return Resulting map.
|
|
7
|
+
*/
|
|
8
|
+
function createOptionShortNameMap(optionsConfig) {
|
|
9
|
+
let map = {};
|
|
10
|
+
for (let [optionName, optionConfig] of Object.entries(optionsConfig)) if (typeof optionConfig.shortName === "string") map[optionConfig.shortName] = optionName;
|
|
11
|
+
return map;
|
|
10
12
|
}
|
|
11
|
-
|
|
13
|
+
|
|
14
|
+
//#endregion
|
|
15
|
+
export { createOptionShortNameMap };
|
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
import { isShortOptionsGroup } from "./isShortOptionsGroup.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
2
|
+
|
|
3
|
+
//#region source/internals/expandArguments.ts
|
|
4
|
+
/**
|
|
5
|
+
* Converts short options group in a provided list of arguments into separate short options.
|
|
6
|
+
*
|
|
7
|
+
* @param argv Arguments.
|
|
8
|
+
* @return Arguments that contain no short options group
|
|
9
|
+
*/
|
|
10
|
+
function expandArguments(argv) {
|
|
11
|
+
let expandedArguments = [];
|
|
12
|
+
for (let arg of argv) if (isShortOptionsGroup(arg)) expandedArguments.push(...[...arg].slice(1).map((value) => `-${value}`));
|
|
13
|
+
else expandedArguments.push(arg);
|
|
14
|
+
return expandedArguments;
|
|
13
15
|
}
|
|
14
|
-
|
|
16
|
+
|
|
17
|
+
//#endregion
|
|
18
|
+
export { expandArguments };
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
//#region source/internals/isCommand.ts
|
|
2
|
+
const COMMAND_REGEXP = /^(?:[a-z][-a-z0-9]*[a-z])+(?:\s(?:[a-z][-a-z0-9]*[a-z])+)*$/iu;
|
|
3
|
+
/**
|
|
4
|
+
* A function that checks if a value is an argument that looks like command, i.e. space-separated words consisting only of letters, numbers (but not at the start of the word) or dashes.
|
|
5
|
+
*
|
|
6
|
+
* @param value The value to check.
|
|
7
|
+
* @return `true` if `value` is a command.
|
|
8
|
+
*/
|
|
9
|
+
function isCommand(value) {
|
|
10
|
+
return COMMAND_REGEXP.test(value);
|
|
5
11
|
}
|
|
6
|
-
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { isCommand };
|
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
//#region source/internals/isFalseString.ts
|
|
2
2
|
const FALSE_STRING_REGEXP = /^\s*(?:false|0|no|off)\s*$/iu;
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A function that checks if a string value is a false string. A false string is `'false'`, `'0'`, `'no'`, or `'off'` (case insensitive).
|
|
5
|
+
*
|
|
6
|
+
* @param value The value to be tested.
|
|
7
|
+
* @return `true` if string is a false string. */
|
|
8
|
+
function isFalseString(value) {
|
|
9
|
+
return FALSE_STRING_REGEXP.test(value);
|
|
5
10
|
}
|
|
6
|
-
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { isFalseString };
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
//#region source/internals/isLongOption.ts
|
|
2
2
|
const LONG_OPTION_REGEXP = /^--[a-z][-a-z0-9]+$/iu;
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A function that checks if a value is an argument that looks like a long option, i.e. two dashes and a single word consisting only of letters, numbers (but not at the start of the word) or dashes; e.g. `'--some-option'`.
|
|
5
|
+
*
|
|
6
|
+
* @param value The value to check.
|
|
7
|
+
* @return `true` if `value` is a long option.
|
|
8
|
+
*/
|
|
9
|
+
function isLongOption(arg) {
|
|
10
|
+
return LONG_OPTION_REGEXP.test(arg);
|
|
5
11
|
}
|
|
6
|
-
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { isLongOption };
|
|
@@ -1,6 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
//#region source/internals/isOptionLike.ts
|
|
2
2
|
const OPTION_LIKE = /^-{1,2}[a-z][-a-z0-9]*=?/iu;
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* A function that checks if a value is an argument that looks like a long option, short option, or a short options group i.e. one or two dashes and a single word consisting only of letters, numbers (but not at the start of the word) or dashes, optionally followed by `=`.
|
|
5
|
+
*
|
|
6
|
+
* @param value The value to check.
|
|
7
|
+
* @return `true` if `value` is an option.
|
|
8
|
+
*/
|
|
9
|
+
function isOptionLike(value) {
|
|
10
|
+
return OPTION_LIKE.test(value);
|
|
5
11
|
}
|
|
6
|
-
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { isOptionLike };
|