@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.
Files changed (73) hide show
  1. package/build/ParsingError.js +13 -7
  2. package/build/ValidationError.js +18 -12
  3. package/build/internals/Checker.js +49 -95
  4. package/build/internals/CommandScope.js +42 -44
  5. package/build/internals/OptionScope.js +50 -68
  6. package/build/internals/castValue.js +22 -22
  7. package/build/internals/commandToArguments.js +16 -13
  8. package/build/internals/createOptionShortNameMap.js +14 -10
  9. package/build/internals/expandArguments.js +16 -12
  10. package/build/internals/isCommand.js +13 -5
  11. package/build/internals/isFalseString.js +11 -4
  12. package/build/internals/isLongOption.js +12 -4
  13. package/build/internals/isOptionLike.js +12 -4
  14. package/build/internals/isShortOption.js +12 -4
  15. package/build/internals/isShortOptionsGroup.js +13 -5
  16. package/build/internals/isTrueString.js +11 -4
  17. package/build/main.js +5 -5
  18. package/build/parseArguments.js +155 -231
  19. package/package.json +7 -7
  20. package/build/Arguments.js +0 -2
  21. package/build/Arguments.js.map +0 -7
  22. package/build/ArgumentsCommand.js +0 -2
  23. package/build/ArgumentsCommand.js.map +0 -7
  24. package/build/ArgumentsOptions.js +0 -2
  25. package/build/ArgumentsOptions.js.map +0 -7
  26. package/build/ArgumentsParameters.js +0 -2
  27. package/build/ArgumentsParameters.js.map +0 -7
  28. package/build/ArgumentsUnknownOptions.js +0 -2
  29. package/build/ArgumentsUnknownOptions.js.map +0 -7
  30. package/build/Argv.js +0 -2
  31. package/build/Argv.js.map +0 -7
  32. package/build/CommandConfig.js +0 -2
  33. package/build/CommandConfig.js.map +0 -7
  34. package/build/CommandsConfig.js +0 -2
  35. package/build/CommandsConfig.js.map +0 -7
  36. package/build/LongOptionName.js +0 -2
  37. package/build/LongOptionName.js.map +0 -7
  38. package/build/OptionConfig.js +0 -2
  39. package/build/OptionConfig.js.map +0 -7
  40. package/build/OptionsConfig.js +0 -2
  41. package/build/OptionsConfig.js.map +0 -7
  42. package/build/ParameterConfig.js +0 -2
  43. package/build/ParameterConfig.js.map +0 -7
  44. package/build/ParametersConfig.js +0 -2
  45. package/build/ParametersConfig.js.map +0 -7
  46. package/build/ParserConfig.js +0 -2
  47. package/build/ParserConfig.js.map +0 -7
  48. package/build/ParsingError.js.map +0 -7
  49. package/build/ShortOptionName.js +0 -2
  50. package/build/ShortOptionName.js.map +0 -7
  51. package/build/ValidationError.js.map +0 -7
  52. package/build/internals/Checker.js.map +0 -7
  53. package/build/internals/CommandScope.js.map +0 -7
  54. package/build/internals/NormalizedCommandConfig.js +0 -2
  55. package/build/internals/NormalizedCommandConfig.js.map +0 -7
  56. package/build/internals/NormalizedCommandsConfig.js +0 -2
  57. package/build/internals/NormalizedCommandsConfig.js.map +0 -7
  58. package/build/internals/OptionScope.js.map +0 -7
  59. package/build/internals/castValue.js.map +0 -7
  60. package/build/internals/commandToArguments.js.map +0 -7
  61. package/build/internals/createOptionShortNameMap.js.map +0 -7
  62. package/build/internals/expandArguments.js.map +0 -7
  63. package/build/internals/isCommand.js.map +0 -7
  64. package/build/internals/isFalseString.js.map +0 -7
  65. package/build/internals/isLongOption.js.map +0 -7
  66. package/build/internals/isOptionLike.js.map +0 -7
  67. package/build/internals/isShortOption.js.map +0 -7
  68. package/build/internals/isShortOptionsGroup.js.map +0 -7
  69. package/build/internals/isTrueString.js.map +0 -7
  70. package/build/internals.js +0 -16
  71. package/build/internals.js.map +0 -7
  72. package/build/main.js.map +0 -7
  73. package/build/parseArguments.js.map +0 -7
@@ -1,9 +1,15 @@
1
- "use strict";
2
1
  import { createCustomError } from "@jakubmazanec/error";
3
- export const ParsingError = createCustomError("ParsingError", {
4
- INVALID_PARAMETER: 'Invalid parameter value "{0}" found. Allowed values for parameter "{1}" are: {2}.',
5
- UNKNOWN_PARAMETER: 'Unknown parameter "{0}" found. Unknown parameters are not allowed.',
6
- INVALID_OPTION: 'Invalid option value "{0}" found. Allowed values for option "{1}" are: {2}.',
7
- UNKNOWN_OPTION: 'Unknown option "{0}" found. Unknown options are not allowed.'
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
- //# sourceMappingURL=ParsingError.js.map
13
+
14
+ //#endregion
15
+ export { ParsingError };
@@ -1,14 +1,20 @@
1
- "use strict";
2
1
  import { createCustomError } from "@jakubmazanec/error";
3
- export const ValidationError = createCustomError("ValidationError", {
4
- INVALID_COMMAND: 'Command "{0}" has invalid format. Only words consisting of letters, numbers or dashes separated by single space are allowed.',
5
- MISSING_PARAMETER: 'Parameter "{0}" is required but its value is undefined.',
6
- INVALID_PARAMETER_DEFAULT_AND_CHOICES: 'Parameter "{0}" has invalid default value "{1}". Default value must be one of the `choices` elements.',
7
- INVALID_PARAMETER_ORDER: 'Parameter "{0}" has invalid order. All parameters before must have a default value or be required too',
8
- MISSING_OPTION: 'Option "{0}" is required but its value is undefined.',
9
- INVALID_OPTION_DEFAULT_AND_CHOICES: 'Option "{0}" has invalid default value "{1}". Default value must be one of the `choices` elements.',
10
- INVALID_OPTION_NAME: 'Option "{0}" has invalid name. Only names consisting of letters, numbers or dashes are allowed.',
11
- REPEATED_OPTION_NAME: 'Option "{0}" has invalid short name "{1}". Short option names must be unique.',
12
- INVALID_OPTION_ARITY: 'Option "{0}" has invalid number of values. Found "{1}", require "{2}".'
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
- //# sourceMappingURL=ValidationError.js.map
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
- export class Checker {
6
- errors = [];
7
- checkCommandsConfig(commandsConfig) {
8
- for (let commandConfig of commandsConfig) {
9
- if (!isCommand(commandConfig)) {
10
- this.addError(new ValidationError("INVALID_COMMAND", { messageParameters: [commandConfig] }));
11
- }
12
- }
13
- }
14
- checkParametersConfig(parametersConfig) {
15
- for (let [i, parameterConfig] of parametersConfig.entries()) {
16
- if (parameterConfig.type !== "boolean" && parameterConfig.choices && // default values and chocies do not contradict each other
17
- parameterConfig.defaultValue !== void 0 && !parameterConfig.choices.includes(parameterConfig.defaultValue)) {
18
- this.addError(
19
- new ValidationError("INVALID_PARAMETER_DEFAULT_AND_CHOICES", {
20
- messageParameters: [parameterConfig.label ?? i, parameterConfig.defaultValue]
21
- })
22
- );
23
- }
24
- if (i >= 1 && parameterConfig.required === true && !parametersConfig.slice(0, i).every(
25
- (previousParameterConfig) => previousParameterConfig.required === true || previousParameterConfig.defaultValue !== void 0
26
- )) {
27
- this.addError(
28
- new ValidationError("INVALID_PARAMETER_ORDER", {
29
- messageParameters: [parameterConfig.label ?? i]
30
- })
31
- );
32
- }
33
- }
34
- }
35
- checkOptionsConfig(optionsConfig) {
36
- let usedShortOptionNames = /* @__PURE__ */ new Set([]);
37
- for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {
38
- if (!isLongOption(`--${optionName}`)) {
39
- this.addError(
40
- new ValidationError("INVALID_OPTION_NAME", {
41
- messageParameters: [optionName]
42
- })
43
- );
44
- }
45
- if (optionConfig.shortName && usedShortOptionNames.has(optionConfig.shortName)) {
46
- this.addError(
47
- new ValidationError("REPEATED_OPTION_NAME", {
48
- messageParameters: [optionName, optionConfig.shortName]
49
- })
50
- );
51
- } else if (optionConfig.shortName) {
52
- usedShortOptionNames.add(optionConfig.shortName);
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
- "use strict";
2
- export class CommandScope {
3
- args;
4
- config;
5
- constructor(arg, normalizedCommandsConfig) {
6
- let normalizedArgument = arg.toLowerCase();
7
- let commandConfig = normalizedCommandsConfig.find(
8
- (commandConfig2) => commandConfig2.args[0] === normalizedArgument
9
- );
10
- this.config = commandConfig ?? [];
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()) {
20
- if (this.args[i] !== arg) {
21
- isComplete = false;
22
- break;
23
- }
24
- }
25
- return isComplete;
26
- }
27
- isDone() {
28
- let isDone = false;
29
- for (let [i, arg] of this.config.args.entries()) {
30
- if (this.args[i] !== arg) {
31
- isDone = true;
32
- break;
33
- }
34
- }
35
- return isDone;
36
- }
37
- getValue() {
38
- return this.config.command;
39
- }
40
- getArgs() {
41
- return [...this.args];
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
- export class OptionScope {
6
- name;
7
- values = [];
8
- previousParsedValue;
9
- config;
10
- constructor(optionName, optionsConfig, allowUnknownOptions = false) {
11
- this.name = optionName;
12
- if (this.name.includes("-")) {
13
- this.name = _.camelCase(this.name);
14
- }
15
- let optionConfig = optionsConfig[this.name];
16
- if (!optionConfig) {
17
- if (!allowUnknownOptions) {
18
- throw new ParsingError("UNKNOWN_OPTION", { messageParameters: [this.name] });
19
- }
20
- optionConfig = {
21
- type: "string"
22
- };
23
- }
24
- this.config = optionConfig;
25
- }
26
- captureArg(arg) {
27
- let value = castValue(arg, this.config.type);
28
- if ((this.config.type === "number" && !this.config.count || this.config.type === "string") && this.config.choices !== void 0 && !this.config.choices.includes(value)) {
29
- throw new ParsingError("INVALID_OPTION", {
30
- messageParameters: [
31
- value,
32
- this.name,
33
- this.config.choices.map((choice) => `"${choice}"`).join(", ")
34
- ]
35
- });
36
- }
37
- if (this.config.multiple) {
38
- this.values.push(value);
39
- } else {
40
- this.values = [value];
41
- }
42
- }
43
- isComplete() {
44
- if (!this.config.multiple && this.values.length === 1) {
45
- return true;
46
- }
47
- if (this.config.multiple && typeof this.config.arity === "number" && this.values.length + (this.previousParsedValue ?? []).length === this.config.arity) {
48
- return true;
49
- }
50
- return false;
51
- }
52
- getValue() {
53
- let { config, previousParsedValue } = this;
54
- let { values } = this;
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
- export function castValue(value, targetType) {
4
- switch (targetType) {
5
- case "boolean": {
6
- let booleanString = String(value).toLowerCase();
7
- return !(isFalseString(booleanString) || booleanString === "");
8
- }
9
- case "number": {
10
- return Number.parseFloat(value);
11
- }
12
- case "string": {
13
- return String(value);
14
- }
15
- case void 0: {
16
- return String(value);
17
- }
18
- default: {
19
- targetType;
20
- return String(value);
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
- //# sourceMappingURL=castValue.js.map
22
+
23
+ //#endregion
24
+ export { castValue };
@@ -1,14 +1,17 @@
1
- "use strict";
2
- export function commandToArguments(value) {
3
- let rawArgs = value.split(" ");
4
- let args = [];
5
- for (let [, rawValue] of rawArgs.entries()) {
6
- if (rawValue === "" && args.at(-1)) {
7
- args[args.length - 1] += " ";
8
- } else {
9
- args.push(rawValue);
10
- }
11
- }
12
- return args;
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
- //# sourceMappingURL=commandToArguments.js.map
15
+
16
+ //#endregion
17
+ export { commandToArguments };
@@ -1,11 +1,15 @@
1
- "use strict";
2
- export function createOptionShortNameMap(optionsConfig) {
3
- let map = {};
4
- for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {
5
- if (typeof optionConfig.shortName === "string") {
6
- map[optionConfig.shortName] = optionName;
7
- }
8
- }
9
- return map;
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
- //# sourceMappingURL=createOptionShortNameMap.js.map
13
+
14
+ //#endregion
15
+ export { createOptionShortNameMap };
@@ -1,14 +1,18 @@
1
- "use strict";
2
1
  import { isShortOptionsGroup } from "./isShortOptionsGroup.js";
3
- export function expandArguments(argv) {
4
- let expandedArguments = [];
5
- for (let arg of argv) {
6
- if (isShortOptionsGroup(arg)) {
7
- expandedArguments.push(...[...arg].slice(1).map((value) => `-${value}`));
8
- } else {
9
- expandedArguments.push(arg);
10
- }
11
- }
12
- return expandedArguments;
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
- //# sourceMappingURL=expandArguments.js.map
16
+
17
+ //#endregion
18
+ export { expandArguments };
@@ -1,6 +1,14 @@
1
- "use strict";
2
- export const COMMAND_REGEXP = /^(?:[a-z][-a-z0-9]*[a-z])+(?:\s(?:[a-z][-a-z0-9]*[a-z])+)*$/iu;
3
- export function isCommand(value) {
4
- return COMMAND_REGEXP.test(value);
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
- //# sourceMappingURL=isCommand.js.map
12
+
13
+ //#endregion
14
+ export { isCommand };
@@ -1,6 +1,13 @@
1
- "use strict";
1
+ //#region source/internals/isFalseString.ts
2
2
  const FALSE_STRING_REGEXP = /^\s*(?:false|0|no|off)\s*$/iu;
3
- export function isFalseString(value) {
4
- return FALSE_STRING_REGEXP.test(value);
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
- //# sourceMappingURL=isFalseString.js.map
11
+
12
+ //#endregion
13
+ export { isFalseString };
@@ -1,6 +1,14 @@
1
- "use strict";
1
+ //#region source/internals/isLongOption.ts
2
2
  const LONG_OPTION_REGEXP = /^--[a-z][-a-z0-9]+$/iu;
3
- export function isLongOption(arg) {
4
- return LONG_OPTION_REGEXP.test(arg);
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
- //# sourceMappingURL=isLongOption.js.map
12
+
13
+ //#endregion
14
+ export { isLongOption };
@@ -1,6 +1,14 @@
1
- "use strict";
1
+ //#region source/internals/isOptionLike.ts
2
2
  const OPTION_LIKE = /^-{1,2}[a-z][-a-z0-9]*=?/iu;
3
- export function isOptionLike(value) {
4
- return OPTION_LIKE.test(value);
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
- //# sourceMappingURL=isOptionLike.js.map
12
+
13
+ //#endregion
14
+ export { isOptionLike };