@jakubmazanec/args 2.0.4 → 2.0.5-unstable.1be6df9

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.
@@ -1,2 +1,2 @@
1
1
  /** Short option name. */
2
- export type ShortOptionName = 'A' | 'a' | 'B' | 'b' | 'C' | 'c' | 'D' | 'd' | 'E' | 'e' | 'F' | 'f' | 'G' | 'g' | 'H' | 'h' | 'I' | 'i' | 'J' | 'j' | 'K' | 'k' | 'L' | 'l' | 'M' | 'm' | 'N' | 'n' | 'O' | 'o' | 'P' | 'p' | 'Q' | 'q' | 'R' | 'r' | 'S' | 's' | 'T' | 't' | 'U' | 'u' | 'V' | 'v' | 'W' | 'w' | 'X' | 'x' | 'Y' | 'y' | 'Z' | 'z';
2
+ export type ShortOptionName = 'a' | 'A' | 'b' | 'B' | 'c' | 'C' | 'd' | 'D' | 'e' | 'E' | 'f' | 'F' | 'g' | 'G' | 'h' | 'H' | 'i' | 'I' | 'j' | 'J' | 'k' | 'K' | 'l' | 'L' | 'm' | 'M' | 'n' | 'N' | 'o' | 'O' | 'p' | 'P' | 'q' | 'Q' | 'r' | 'R' | 's' | 'S' | 't' | 'T' | 'u' | 'U' | 'v' | 'V' | 'w' | 'W' | 'x' | 'X' | 'y' | 'Y' | 'z' | 'Z';
@@ -4,4 +4,4 @@
4
4
  * @param value The value to be converted.
5
5
  * @param type The type to convert the value into.
6
6
  * */
7
- export declare function castValue<T extends 'boolean' | 'number' | 'string'>(value: string, type?: T): T extends 'boolean' ? boolean : T extends 'number' ? number : string;
7
+ export declare function castValue<T extends 'boolean' | 'number' | 'string'>(value: string, targetType?: T): T extends 'boolean' ? boolean : T extends 'number' ? number : string;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  import { isFalseString } from "./isFalseString.js";
3
- export function castValue(value, type) {
4
- switch (type) {
3
+ export function castValue(value, targetType) {
4
+ switch (targetType) {
5
5
  case "boolean": {
6
6
  let booleanString = String(value).toLowerCase();
7
7
  return !(isFalseString(booleanString) || booleanString === "");
@@ -9,7 +9,14 @@ export function castValue(value, type) {
9
9
  case "number": {
10
10
  return Number.parseFloat(value);
11
11
  }
12
+ case "string": {
13
+ return String(value);
14
+ }
15
+ case void 0: {
16
+ return String(value);
17
+ }
12
18
  default: {
19
+ targetType;
13
20
  return String(value);
14
21
  }
15
22
  }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../source/internals/castValue.ts"],
4
- "sourcesContent": ["import {isFalseString} from './isFalseString.js';\n\n/**\n * A function that casts string value to a specified type.\n *\n * @param value The value to be converted.\n * @param type The type to convert the value into.\n * */\nexport function castValue<T extends 'boolean' | 'number' | 'string'>(\n value: string,\n type?: T,\n): T extends 'boolean' ? boolean\n: T extends 'number' ? number\n: string {\n switch (type) {\n case 'boolean': {\n let booleanString = String(value).toLowerCase();\n\n return !(isFalseString(booleanString) || booleanString === '') as T extends 'boolean' ?\n boolean\n : T extends 'number' ? number\n : string;\n }\n\n case 'number': {\n return Number.parseFloat(value) as T extends 'boolean' ? boolean\n : T extends 'number' ? number\n : string;\n }\n\n default: {\n return String(value) as T extends 'boolean' ? boolean\n : T extends 'number' ? number\n : string;\n }\n }\n}\n"],
5
- "mappings": ";AAAA,SAAQ,qBAAoB;AAQrB,gBAAS,UACd,OACA,MAGO;AACP,UAAQ,MAAM;AAAA,IACZ,KAAK,WAAW;AACd,UAAI,gBAAgB,OAAO,KAAK,EAAE,YAAY;AAE9C,aAAO,EAAE,cAAc,aAAa,KAAK,kBAAkB;AAAA,IAI7D;AAAA,IAEA,KAAK,UAAU;AACb,aAAO,OAAO,WAAW,KAAK;AAAA,IAGhC;AAAA,IAEA,SAAS;AACP,aAAO,OAAO,KAAK;AAAA,IAGrB;AAAA,EACF;AACF;",
4
+ "sourcesContent": ["import {isFalseString} from './isFalseString.js';\n\n/**\n * A function that casts string value to a specified type.\n *\n * @param value The value to be converted.\n * @param type The type to convert the value into.\n * */\nexport function castValue<T extends 'boolean' | 'number' | 'string'>(\n value: string,\n targetType?: T,\n): T extends 'boolean' ? boolean\n: T extends 'number' ? number\n: string {\n switch (targetType) {\n case 'boolean': {\n let booleanString = String(value).toLowerCase();\n\n return !(isFalseString(booleanString) || booleanString === '') as T extends 'boolean' ?\n boolean\n : T extends 'number' ? number\n : string;\n }\n\n case 'number': {\n return Number.parseFloat(value) as T extends 'boolean' ? boolean\n : T extends 'number' ? number\n : string;\n }\n\n case 'string': {\n return String(value) as T extends 'boolean' ? boolean\n : T extends 'number' ? number\n : string;\n }\n\n case undefined: {\n return String(value) as T extends 'boolean' ? boolean\n : T extends 'number' ? number\n : string;\n }\n\n default: {\n targetType satisfies never;\n\n return String(value) as T extends 'boolean' ? boolean\n : T extends 'number' ? number\n : string;\n }\n }\n}\n"],
5
+ "mappings": ";AAAA,SAAQ,qBAAoB;AAQrB,gBAAS,UACd,OACA,YAGO;AACP,UAAQ,YAAY;AAAA,IAClB,KAAK,WAAW;AACd,UAAI,gBAAgB,OAAO,KAAK,EAAE,YAAY;AAE9C,aAAO,EAAE,cAAc,aAAa,KAAK,kBAAkB;AAAA,IAI7D;AAAA,IAEA,KAAK,UAAU;AACb,aAAO,OAAO,WAAW,KAAK;AAAA,IAGhC;AAAA,IAEA,KAAK,UAAU;AACb,aAAO,OAAO,KAAK;AAAA,IAGrB;AAAA,IAEA,KAAK,QAAW;AACd,aAAO,OAAO,KAAK;AAAA,IAGrB;AAAA,IAEA,SAAS;AACP;AAEA,aAAO,OAAO,KAAK;AAAA,IAGrB;AAAA,EACF;AACF;",
6
6
  "names": []
7
7
  }
@@ -11,6 +11,6 @@ export * from './internals/isOptionLike.js';
11
11
  export * from './internals/isShortOption.js';
12
12
  export * from './internals/isShortOptionsGroup.js';
13
13
  export * from './internals/isTrueString.js';
14
- export * from './internals/NormalizedCommandConfig.js';
15
- export * from './internals/NormalizedCommandsConfig.js';
14
+ export type * from './internals/NormalizedCommandConfig.js';
15
+ export type * from './internals/NormalizedCommandsConfig.js';
16
16
  export * from './internals/OptionScope.js';
@@ -12,7 +12,5 @@ export * from "./internals/isOptionLike.js";
12
12
  export * from "./internals/isShortOption.js";
13
13
  export * from "./internals/isShortOptionsGroup.js";
14
14
  export * from "./internals/isTrueString.js";
15
- export * from "./internals/NormalizedCommandConfig.js";
16
- export * from "./internals/NormalizedCommandsConfig.js";
17
15
  export * from "./internals/OptionScope.js";
18
16
  //# sourceMappingURL=internals.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../source/internals.ts"],
4
- "sourcesContent": ["export * from './internals/castValue.js';\nexport * from './internals/Checker.js';\nexport * from './internals/CommandScope.js';\nexport * from './internals/commandToArguments.js';\nexport * from './internals/createOptionShortNameMap.js';\nexport * from './internals/expandArguments.js';\nexport * from './internals/isCommand.js';\nexport * from './internals/isFalseString.js';\nexport * from './internals/isLongOption.js';\nexport * from './internals/isOptionLike.js';\nexport * from './internals/isShortOption.js';\nexport * from './internals/isShortOptionsGroup.js';\nexport * from './internals/isTrueString.js';\nexport * from './internals/NormalizedCommandConfig.js';\nexport * from './internals/NormalizedCommandsConfig.js';\nexport * from './internals/OptionScope.js';\n"],
5
- "mappings": ";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
4
+ "sourcesContent": ["export * from './internals/castValue.js';\nexport * from './internals/Checker.js';\nexport * from './internals/CommandScope.js';\nexport * from './internals/commandToArguments.js';\nexport * from './internals/createOptionShortNameMap.js';\nexport * from './internals/expandArguments.js';\nexport * from './internals/isCommand.js';\nexport * from './internals/isFalseString.js';\nexport * from './internals/isLongOption.js';\nexport * from './internals/isOptionLike.js';\nexport * from './internals/isShortOption.js';\nexport * from './internals/isShortOptionsGroup.js';\nexport * from './internals/isTrueString.js';\nexport type * from './internals/NormalizedCommandConfig.js';\nexport type * from './internals/NormalizedCommandsConfig.js';\nexport * from './internals/OptionScope.js';\n"],
5
+ "mappings": ";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAGd,cAAc;",
6
6
  "names": []
7
7
  }
package/build/main.d.ts CHANGED
@@ -1,18 +1,18 @@
1
- export * from './Arguments.js';
2
- export * from './ArgumentsCommand.js';
3
- export * from './ArgumentsOptions.js';
4
- export * from './ArgumentsParameters.js';
5
- export * from './ArgumentsUnknownOptions.js';
6
- export * from './Argv.js';
7
- export * from './CommandConfig.js';
8
- export * from './CommandsConfig.js';
9
- export * from './LongOptionName.js';
10
- export * from './OptionConfig.js';
11
- export * from './OptionsConfig.js';
12
- export * from './ParameterConfig.js';
13
- export * from './ParametersConfig.js';
1
+ export type * from './Arguments.js';
2
+ export type * from './ArgumentsCommand.js';
3
+ export type * from './ArgumentsOptions.js';
4
+ export type * from './ArgumentsParameters.js';
5
+ export type * from './ArgumentsUnknownOptions.js';
6
+ export type * from './Argv.js';
7
+ export type * from './CommandConfig.js';
8
+ export type * from './CommandsConfig.js';
9
+ export type * from './LongOptionName.js';
10
+ export type * from './OptionConfig.js';
11
+ export type * from './OptionsConfig.js';
12
+ export type * from './ParameterConfig.js';
13
+ export type * from './ParametersConfig.js';
14
14
  export * from './parseArguments.js';
15
- export * from './ParserConfig.js';
15
+ export type * from './ParserConfig.js';
16
16
  export * from './ParsingError.js';
17
- export * from './ShortOptionName.js';
17
+ export type * from './ShortOptionName.js';
18
18
  export * from './ValidationError.js';
package/build/main.js CHANGED
@@ -1,20 +1,5 @@
1
1
  "use strict";
2
- export * from "./Arguments.js";
3
- export * from "./ArgumentsCommand.js";
4
- export * from "./ArgumentsOptions.js";
5
- export * from "./ArgumentsParameters.js";
6
- export * from "./ArgumentsUnknownOptions.js";
7
- export * from "./Argv.js";
8
- export * from "./CommandConfig.js";
9
- export * from "./CommandsConfig.js";
10
- export * from "./LongOptionName.js";
11
- export * from "./OptionConfig.js";
12
- export * from "./OptionsConfig.js";
13
- export * from "./ParameterConfig.js";
14
- export * from "./ParametersConfig.js";
15
2
  export * from "./parseArguments.js";
16
- export * from "./ParserConfig.js";
17
3
  export * from "./ParsingError.js";
18
- export * from "./ShortOptionName.js";
19
4
  export * from "./ValidationError.js";
20
5
  //# sourceMappingURL=main.js.map
package/build/main.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../source/main.ts"],
4
- "sourcesContent": ["export * from './Arguments.js';\nexport * from './ArgumentsCommand.js';\nexport * from './ArgumentsOptions.js';\nexport * from './ArgumentsParameters.js';\nexport * from './ArgumentsUnknownOptions.js';\nexport * from './Argv.js';\nexport * from './CommandConfig.js';\nexport * from './CommandsConfig.js';\nexport * from './LongOptionName.js';\nexport * from './OptionConfig.js';\nexport * from './OptionsConfig.js';\nexport * from './ParameterConfig.js';\nexport * from './ParametersConfig.js';\nexport * from './parseArguments.js';\nexport * from './ParserConfig.js';\nexport * from './ParsingError.js';\nexport * from './ShortOptionName.js';\nexport * from './ValidationError.js';\n"],
5
- "mappings": ";AAAA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;",
4
+ "sourcesContent": ["export type * from './Arguments.js';\nexport type * from './ArgumentsCommand.js';\nexport type * from './ArgumentsOptions.js';\nexport type * from './ArgumentsParameters.js';\nexport type * from './ArgumentsUnknownOptions.js';\nexport type * from './Argv.js';\nexport type * from './CommandConfig.js';\nexport type * from './CommandsConfig.js';\nexport type * from './LongOptionName.js';\nexport type * from './OptionConfig.js';\nexport type * from './OptionsConfig.js';\nexport type * from './ParameterConfig.js';\nexport type * from './ParametersConfig.js';\nexport * from './parseArguments.js';\nexport type * from './ParserConfig.js';\nexport * from './ParsingError.js';\nexport type * from './ShortOptionName.js';\nexport * from './ValidationError.js';\n"],
5
+ "mappings": ";AAaA,cAAc;AAEd,cAAc;AAEd,cAAc;",
6
6
  "names": []
7
7
  }
@@ -69,9 +69,8 @@ export function parseArguments(argv, parserConfig) {
69
69
  );
70
70
  }
71
71
  }
72
+ commandScope = null;
72
73
  }
73
- optionScope = null;
74
- commandScope = null;
75
74
  let optionName = arg;
76
75
  let optionValue;
77
76
  if (optionName.includes("=")) {
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../source/parseArguments.ts"],
4
- "sourcesContent": ["import {type Arguments} from './Arguments.js';\nimport {type ArgumentsCommand} from './ArgumentsCommand.js';\nimport {type ArgumentsOptions} from './ArgumentsOptions.js';\nimport {type ArgumentsParameters} from './ArgumentsParameters.js';\nimport {type ArgumentsUnknownOptions} from './ArgumentsUnknownOptions.js';\nimport {type Argv} from './Argv.js';\nimport {\n castValue,\n Checker,\n CommandScope,\n commandToArguments,\n createOptionShortNameMap,\n expandArguments,\n isFalseString,\n isLongOption,\n isOptionLike,\n isShortOption,\n isTrueString,\n type NormalizedCommandsConfig,\n OptionScope,\n} from './internals.js';\nimport {type ParameterConfig} from './ParameterConfig.js';\nimport {type ParserConfig} from './ParserConfig.js';\nimport {ParsingError} from './ParsingError.js';\n\n/* eslint-disable no-param-reassign -- we need to add properties to the `parsedOption` object */\nfunction commitOptionScope(optionScope: OptionScope, parsedOptions: Record<string, unknown>) {\n let optionValue = optionScope.getValue();\n\n parsedOptions[optionScope.name] = optionValue;\n\n if (optionScope.config.validate) {\n optionScope.config.validate(parsedOptions[optionScope.name] as never); // type assertion needed to make TypeScript happy\n }\n}\n/* eslint-enable no-param-reassign */\n\n/**\n * Parses a list of command line arguments into an arguments object. Based on provided parser config, raw arguments can be parsed as commands, options and parameters.\n *\n * @param argv Raw arguments to be parsed.\n * @param parserConfig Parser configuration.\n * @return Parsed arguments.\n *\n */\n// TODO: refactor\n// eslint-disable-next-line complexity -- TODO\nexport function parseArguments<const O extends ParserConfig>(\n argv: Argv,\n parserConfig: O,\n): Arguments<O> {\n // create separate configs for commands, parameters and options\n let commandsConfig = parserConfig.commands ?? [];\n let parametersConfig = parserConfig.parameters ?? [];\n let optionsConfig = parserConfig.options ?? {};\n\n // check if parser config is valid\n let checker = new Checker();\n\n checker.checkCommandsConfig(commandsConfig);\n checker.checkParametersConfig(parametersConfig);\n checker.checkOptionsConfig(optionsConfig);\n\n // normalize some configs\n let normalizedCommandsConfig: NormalizedCommandsConfig = commandsConfig.map((command) => ({\n command: command.toLowerCase(),\n args: commandToArguments(command.toLowerCase()),\n }));\n let commandFirstArguments = new Set(\n normalizedCommandsConfig.map((commandConfig) => commandConfig.args[0]),\n );\n let optionShortNameMap =\n parserConfig.options ? createOptionShortNameMap(parserConfig.options) : {};\n\n // variables for keeping track of the parsing\n let parsedCommand: string | undefined;\n let parsedParameters: unknown[] = [];\n let parsedOptions: Record<string, unknown> = {};\n let parsedUnknownOptions: Record<string, unknown> = {};\n let restArgs: string[] = [];\n let optionScope: OptionScope | null = null;\n let commandScope: CommandScope | null = null;\n\n //\n let args = expandArguments(argv);\n\n for (let [i, arg] of args.entries()) {\n // extract rest arguments\n if (arg === '--') {\n restArgs.push(...args.slice(i + 1));\n\n break;\n }\n\n try {\n if (isOptionLike(arg)) {\n // first let's handle options\n\n if (optionScope) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n }\n\n // if there is still unhandled `commandScope`, let's handle its args as parameters...\n if (commandScope) {\n let unhandledArgs = commandScope.getArgs();\n\n for (let unhandledArg of unhandledArgs) {\n if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(unhandledArg);\n } else {\n checker.addError(\n new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [unhandledArg]}),\n );\n }\n }\n }\n\n optionScope = null;\n commandScope = null;\n\n let optionName = arg;\n let optionValue;\n\n // Extract option name and value\n if (optionName.includes('=')) {\n let result = optionName.split('=', 2);\n\n if (result[0] && result[1]) {\n [optionName, optionValue] = result;\n }\n }\n\n if (isShortOption(optionName)) {\n let fullOptionName = optionShortNameMap[optionName.slice(1)];\n\n if (typeof fullOptionName === 'string') {\n optionName = fullOptionName;\n }\n } else if (isLongOption(optionName)) {\n optionName = optionName.slice(2);\n }\n\n let newOptionScope = new OptionScope(\n optionName,\n optionsConfig,\n parserConfig.allowUnknownOptions,\n );\n\n if (parsedOptions[newOptionScope.name] !== undefined) {\n if (newOptionScope.config.multiple) {\n newOptionScope.previousParsedValue = parsedOptions[newOptionScope.name];\n } else if (newOptionScope.config.type === 'number' && newOptionScope.config.count) {\n newOptionScope.previousParsedValue = parsedOptions[newOptionScope.name];\n }\n }\n\n if (optionValue !== undefined) {\n newOptionScope.captureArg(optionValue);\n }\n\n if (newOptionScope.isComplete()) {\n commitOptionScope(\n newOptionScope,\n optionsConfig[newOptionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n\n optionScope = null;\n } else {\n optionScope = newOptionScope;\n }\n } else if (\n optionScope &&\n !(optionScope.config.type === 'boolean' && !optionScope.config.multiple)\n ) {\n // let's handle adding more values to previous option, but not if it's signle boolean option\n\n optionScope.captureArg(arg);\n\n if (optionScope.isComplete()) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n\n optionScope = null;\n }\n } else if (\n optionScope &&\n optionScope.config.type === 'boolean' &&\n !optionScope.config.multiple &&\n (isFalseString(arg) || isTrueString(arg))\n ) {\n // we're adding value to a single boolean option only if the value is \"false\" or \"true\" string\n optionScope.captureArg(arg);\n\n if (optionScope.isComplete()) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n\n optionScope = null;\n }\n } else if (commandScope) {\n // let's handle rest of the command\n\n commandScope.captureArg(arg);\n\n if (commandScope.isComplete()) {\n parsedCommand = commandScope.getValue();\n commandScope = null;\n } else if (commandScope.isDone()) {\n let unhandledArgs = commandScope.getArgs();\n\n for (let unhandledArg of unhandledArgs) {\n if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(unhandledArg);\n } else {\n checker.addError(\n new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [unhandledArg]}),\n );\n }\n }\n\n commandScope = null;\n }\n } else if (parsedCommand === undefined && commandFirstArguments.has(arg.toLowerCase())) {\n // let's handle command\n\n let newCommandScope = new CommandScope(arg, normalizedCommandsConfig);\n\n if (newCommandScope.isComplete()) {\n parsedCommand = newCommandScope.getValue();\n commandScope = null;\n } else {\n commandScope = newCommandScope;\n }\n } else if (parametersConfig[parsedParameters.length]) {\n // let's handle configured parameter\n let parameterConfig = parametersConfig[parsedParameters.length] as ParameterConfig;\n let parameterValue = castValue(arg, parameterConfig.type);\n\n if (\n parameterConfig.type !== 'boolean' &&\n parameterConfig.choices !== undefined &&\n !parameterConfig.choices.includes(parameterValue as never) // during runtime `parameterValue` has correct type\n ) {\n throw new ParsingError('INVALID_PARAMETER', {\n messageParameters: [\n parameterValue,\n parameterConfig.label ?? parsedParameters.length,\n parameterConfig.choices.map((choice) => `\"${choice}\"`).join(', '),\n ],\n });\n }\n\n parsedParameters.push(parameterValue);\n\n if (parameterConfig.validate) {\n parameterConfig.validate(parameterValue as never); // during runtime `parameterValue` has correct type\n }\n } else if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(arg);\n } else {\n throw new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [arg]});\n }\n } catch (error: unknown) {\n optionScope = null;\n commandScope = null;\n\n checker.addError(error as Error);\n }\n }\n\n try {\n if (optionScope) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n }\n\n // if there is still unhandled `commandScope`, let's handle its args as parameters...\n if (commandScope) {\n let unhandledArgs = commandScope.getArgs();\n\n for (let unhandledArg of unhandledArgs) {\n if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(unhandledArg);\n } else {\n checker.addError(\n new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [unhandledArg]}),\n );\n }\n }\n }\n\n // if there are missing parsed parameters, add undefined\n for (let [i] of parametersConfig.entries()) {\n if (parsedParameters[i] === undefined) {\n parsedParameters[i] = undefined;\n }\n }\n\n // handle default option values...\n for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {\n if (parsedOptions[optionName] === undefined && optionConfig.defaultValue !== undefined) {\n parsedOptions[optionName] = optionConfig.defaultValue;\n } else if (parsedOptions[optionName] === undefined) {\n parsedOptions[optionName] = undefined;\n }\n }\n\n // handle default parameters values...\n for (let [i, parameterConfig] of parametersConfig.entries()) {\n if (parsedParameters[i] === undefined && parameterConfig.defaultValue !== undefined) {\n parsedParameters[i] = parameterConfig.defaultValue;\n }\n }\n } catch (error: unknown) {\n checker.addError(error as Error);\n }\n\n checker.checkParsedParameters(parametersConfig, parsedParameters);\n checker.checkParsedOptions(optionsConfig, parsedOptions);\n\n return {\n command: (normalizedCommandsConfig.length ? parsedCommand : null) as ArgumentsCommand<O>,\n parameters: (parametersConfig.length || parserConfig.allowUnknownParameters ?\n parsedParameters\n : null) as ArgumentsParameters<O>,\n options: (Object.keys(optionsConfig).length ? parsedOptions : null) as ArgumentsOptions<O>,\n unknownOptions: (parserConfig.allowUnknownOptions ? parsedUnknownOptions : (\n null\n )) as ArgumentsUnknownOptions<O>,\n rest: restArgs,\n errors: checker.errors,\n };\n}\n"],
5
- "mappings": ";AAMA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,OAC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AGA,aAAS,CAAC,CAAC,KAAK,iBAAiB,QAAQ,GAAG;AAC1C,UAAI,iBAAiB,CAAC,MAAM,QAAW;AACrC,yBAAiB,CAAC,IAAI;AAAA,MACxB;AAAA,IACF;AAGA,aAAS,CAAC,YAAY,YAAY,KAAK,OAAO,QAAQ,aAAa,GAAG;AACpE,UAAI,cAAc,UAAU,MAAM,UAAa,aAAa,iBAAiB,QAAW;AACtF,sBAAc,UAAU,IAAI,aAAa;AAAA,MAC3C,WAAW,cAAc,UAAU,MAAM,QAAW;AAClD,sBAAc,UAAU,IAAI;AAAA,MAC9B;AAAA,IACF;AAGA,aAAS,CAAC,GAAG,eAAe,KAAK,iBAAiB,QAAQ,GAAG;AAC3D,UAAI,iBAAiB,CAAC,MAAM,UAAa,gBAAgB,iBAAiB,QAAW;AACnF,yBAAiB,CAAC,IAAI,gBAAgB;AAAA,MACxC;AAAA,IACF;AAAA,EACF,SAAS,OAAgB;AACvB,YAAQ,SAAS,KAAc;AAAA,EACjC;AAEA,UAAQ,sBAAsB,kBAAkB,gBAAgB;AAChE,UAAQ,mBAAmB,eAAe,aAAa;AAEvD,SAAO;AAAA,IACL,SAAU,yBAAyB,SAAS,gBAAgB;AAAA,IAC5D,YAAa,iBAAiB,UAAU,aAAa,yBACnD,mBACA;AAAA,IACF,SAAU,OAAO,KAAK,aAAa,EAAE,SAAS,gBAAgB;AAAA,IAC9D,gBAAiB,aAAa,sBAAsB,uBAClD;AAAA,IAEF,MAAM;AAAA,IACN,QAAQ,QAAQ;AAAA,EAClB;AACF;",
4
+ "sourcesContent": ["import {type Arguments} from './Arguments.js';\nimport {type ArgumentsCommand} from './ArgumentsCommand.js';\nimport {type ArgumentsOptions} from './ArgumentsOptions.js';\nimport {type ArgumentsParameters} from './ArgumentsParameters.js';\nimport {type ArgumentsUnknownOptions} from './ArgumentsUnknownOptions.js';\nimport {type Argv} from './Argv.js';\nimport {\n castValue,\n Checker,\n CommandScope,\n commandToArguments,\n createOptionShortNameMap,\n expandArguments,\n isFalseString,\n isLongOption,\n isOptionLike,\n isShortOption,\n isTrueString,\n type NormalizedCommandsConfig,\n OptionScope,\n} from './internals.js';\nimport {type ParameterConfig} from './ParameterConfig.js';\nimport {type ParserConfig} from './ParserConfig.js';\nimport {ParsingError} from './ParsingError.js';\n\n/* eslint-disable no-param-reassign -- we need to add properties to the `parsedOption` object */\nfunction commitOptionScope(optionScope: OptionScope, parsedOptions: Record<string, unknown>) {\n let optionValue = optionScope.getValue();\n\n parsedOptions[optionScope.name] = optionValue;\n\n if (optionScope.config.validate) {\n optionScope.config.validate(parsedOptions[optionScope.name] as never); // type assertion needed to make TypeScript happy\n }\n}\n/* eslint-enable no-param-reassign */\n\n/**\n * Parses a list of command line arguments into an arguments object. Based on provided parser config, raw arguments can be parsed as commands, options and parameters.\n *\n * @param argv Raw arguments to be parsed.\n * @param parserConfig Parser configuration.\n * @return Parsed arguments.\n *\n */\nexport function parseArguments<const O extends ParserConfig>(\n argv: Argv,\n parserConfig: O,\n): Arguments<O> {\n // create separate configs for commands, parameters and options\n let commandsConfig = parserConfig.commands ?? [];\n let parametersConfig = parserConfig.parameters ?? [];\n let optionsConfig = parserConfig.options ?? {};\n\n // check if parser config is valid\n let checker = new Checker();\n\n checker.checkCommandsConfig(commandsConfig);\n checker.checkParametersConfig(parametersConfig);\n checker.checkOptionsConfig(optionsConfig);\n\n // normalize some configs\n let normalizedCommandsConfig: NormalizedCommandsConfig = commandsConfig.map((command) => ({\n command: command.toLowerCase(),\n args: commandToArguments(command.toLowerCase()),\n }));\n let commandFirstArguments = new Set(\n normalizedCommandsConfig.map((commandConfig) => commandConfig.args[0]),\n );\n let optionShortNameMap =\n parserConfig.options ? createOptionShortNameMap(parserConfig.options) : {};\n\n // variables for keeping track of the parsing\n let parsedCommand: string | undefined;\n let parsedParameters: unknown[] = [];\n let parsedOptions: Record<string, unknown> = {};\n let parsedUnknownOptions: Record<string, unknown> = {};\n let restArgs: string[] = [];\n let optionScope: OptionScope | null = null;\n let commandScope: CommandScope | null = null;\n\n //\n let args = expandArguments(argv);\n\n for (let [i, arg] of args.entries()) {\n // extract rest arguments\n if (arg === '--') {\n restArgs.push(...args.slice(i + 1));\n\n break;\n }\n\n try {\n if (isOptionLike(arg)) {\n // first let's handle options\n\n if (optionScope) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n }\n\n // if there is still unhandled `commandScope`, let's handle its args as parameters...\n if (commandScope) {\n let unhandledArgs = commandScope.getArgs();\n\n for (let unhandledArg of unhandledArgs) {\n if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(unhandledArg);\n } else {\n checker.addError(\n new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [unhandledArg]}),\n );\n }\n }\n\n commandScope = null;\n }\n\n let optionName = arg;\n let optionValue;\n\n // Extract option name and value\n if (optionName.includes('=')) {\n let result = optionName.split('=', 2);\n\n if (result[0] && result[1]) {\n [optionName, optionValue] = result;\n }\n }\n\n if (isShortOption(optionName)) {\n let fullOptionName = optionShortNameMap[optionName.slice(1)];\n\n if (typeof fullOptionName === 'string') {\n optionName = fullOptionName;\n }\n } else if (isLongOption(optionName)) {\n optionName = optionName.slice(2);\n }\n\n let newOptionScope = new OptionScope(\n optionName,\n optionsConfig,\n parserConfig.allowUnknownOptions,\n );\n\n if (parsedOptions[newOptionScope.name] !== undefined) {\n if (newOptionScope.config.multiple) {\n newOptionScope.previousParsedValue = parsedOptions[newOptionScope.name];\n } else if (newOptionScope.config.type === 'number' && newOptionScope.config.count) {\n newOptionScope.previousParsedValue = parsedOptions[newOptionScope.name];\n }\n }\n\n if (optionValue !== undefined) {\n newOptionScope.captureArg(optionValue);\n }\n\n if (newOptionScope.isComplete()) {\n commitOptionScope(\n newOptionScope,\n optionsConfig[newOptionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n\n optionScope = null;\n } else {\n optionScope = newOptionScope;\n }\n } else if (\n optionScope &&\n !(optionScope.config.type === 'boolean' && !optionScope.config.multiple)\n ) {\n // let's handle adding more values to previous option, but not if it's signle boolean option\n\n optionScope.captureArg(arg);\n\n if (optionScope.isComplete()) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n\n optionScope = null;\n }\n } else if (\n optionScope &&\n optionScope.config.type === 'boolean' &&\n !optionScope.config.multiple &&\n (isFalseString(arg) || isTrueString(arg))\n ) {\n // we're adding value to a single boolean option only if the value is \"false\" or \"true\" string\n optionScope.captureArg(arg);\n\n if (optionScope.isComplete()) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n\n optionScope = null;\n }\n } else if (commandScope) {\n // let's handle rest of the command\n\n commandScope.captureArg(arg);\n\n if (commandScope.isComplete()) {\n parsedCommand = commandScope.getValue();\n commandScope = null;\n } else if (commandScope.isDone()) {\n let unhandledArgs = commandScope.getArgs();\n\n for (let unhandledArg of unhandledArgs) {\n if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(unhandledArg);\n } else {\n checker.addError(\n new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [unhandledArg]}),\n );\n }\n }\n\n commandScope = null;\n }\n } else if (parsedCommand === undefined && commandFirstArguments.has(arg.toLowerCase())) {\n // let's handle command\n\n let newCommandScope = new CommandScope(arg, normalizedCommandsConfig);\n\n if (newCommandScope.isComplete()) {\n parsedCommand = newCommandScope.getValue();\n commandScope = null;\n } else {\n commandScope = newCommandScope;\n }\n } else if (parametersConfig[parsedParameters.length]) {\n // let's handle configured parameter\n let parameterConfig = parametersConfig[parsedParameters.length] as ParameterConfig;\n let parameterValue = castValue(arg, parameterConfig.type);\n\n if (\n parameterConfig.type !== 'boolean' &&\n parameterConfig.choices !== undefined &&\n !parameterConfig.choices.includes(parameterValue as never) // during runtime `parameterValue` has correct type\n ) {\n throw new ParsingError('INVALID_PARAMETER', {\n messageParameters: [\n parameterValue,\n parameterConfig.label ?? parsedParameters.length,\n parameterConfig.choices.map((choice) => `\"${choice}\"`).join(', '),\n ],\n });\n }\n\n parsedParameters.push(parameterValue);\n\n if (parameterConfig.validate) {\n parameterConfig.validate(parameterValue as never); // during runtime `parameterValue` has correct type\n }\n } else if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(arg);\n } else {\n throw new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [arg]});\n }\n } catch (error: unknown) {\n optionScope = null;\n commandScope = null;\n\n checker.addError(error as Error);\n }\n }\n\n try {\n if (optionScope) {\n commitOptionScope(\n optionScope,\n optionsConfig[optionScope.name] ? parsedOptions : parsedUnknownOptions,\n );\n }\n\n // if there is still unhandled `commandScope`, let's handle its args as parameters...\n if (commandScope) {\n let unhandledArgs = commandScope.getArgs();\n\n for (let unhandledArg of unhandledArgs) {\n if (parserConfig.allowUnknownParameters) {\n // let's handle other parameter\n parsedParameters.push(unhandledArg);\n } else {\n checker.addError(\n new ParsingError('UNKNOWN_PARAMETER', {messageParameters: [unhandledArg]}),\n );\n }\n }\n }\n\n // if there are missing parsed parameters, add undefined\n for (let [i] of parametersConfig.entries()) {\n if (parsedParameters[i] === undefined) {\n parsedParameters[i] = undefined;\n }\n }\n\n // handle default option values...\n for (let [optionName, optionConfig] of Object.entries(optionsConfig)) {\n if (parsedOptions[optionName] === undefined && optionConfig.defaultValue !== undefined) {\n parsedOptions[optionName] = optionConfig.defaultValue;\n } else if (parsedOptions[optionName] === undefined) {\n parsedOptions[optionName] = undefined;\n }\n }\n\n // handle default parameters values...\n for (let [i, parameterConfig] of parametersConfig.entries()) {\n if (parsedParameters[i] === undefined && parameterConfig.defaultValue !== undefined) {\n parsedParameters[i] = parameterConfig.defaultValue;\n }\n }\n } catch (error: unknown) {\n checker.addError(error as Error);\n }\n\n checker.checkParsedParameters(parametersConfig, parsedParameters);\n checker.checkParsedOptions(optionsConfig, parsedOptions);\n\n return {\n command: (normalizedCommandsConfig.length ? parsedCommand : null) as ArgumentsCommand<O>,\n parameters: (parametersConfig.length || parserConfig.allowUnknownParameters ?\n parsedParameters\n : null) as ArgumentsParameters<O>,\n options: (Object.keys(optionsConfig).length ? parsedOptions : null) as ArgumentsOptions<O>,\n unknownOptions: (parserConfig.allowUnknownOptions ? parsedUnknownOptions : (\n null\n )) as ArgumentsUnknownOptions<O>,\n rest: restArgs,\n errors: checker.errors,\n };\n}\n"],
5
+ "mappings": ";AAMA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AAGP,SAAQ,oBAAmB;AAG3B,SAAS,kBAAkB,aAA0B,eAAwC;AAC3F,MAAI,cAAc,YAAY,SAAS;AAEvC,gBAAc,YAAY,IAAI,IAAI;AAElC,MAAI,YAAY,OAAO,UAAU;AAC/B,gBAAY,OAAO,SAAS,cAAc,YAAY,IAAI,CAAU;AAAA,EACtE;AACF;AAWO,gBAAS,eACd,MACA,cACc;AAEd,MAAI,iBAAiB,aAAa,YAAY,CAAC;AAC/C,MAAI,mBAAmB,aAAa,cAAc,CAAC;AACnD,MAAI,gBAAgB,aAAa,WAAW,CAAC;AAG7C,MAAI,UAAU,IAAI,QAAQ;AAE1B,UAAQ,oBAAoB,cAAc;AAC1C,UAAQ,sBAAsB,gBAAgB;AAC9C,UAAQ,mBAAmB,aAAa;AAGxC,MAAI,2BAAqD,eAAe,IAAI,CAAC,aAAa;AAAA,IACxF,SAAS,QAAQ,YAAY;AAAA,IAC7B,MAAM,mBAAmB,QAAQ,YAAY,CAAC;AAAA,EAChD,EAAE;AACF,MAAI,wBAAwB,IAAI;AAAA,IAC9B,yBAAyB,IAAI,CAAC,kBAAkB,cAAc,KAAK,CAAC,CAAC;AAAA,EACvE;AACA,MAAI,qBACF,aAAa,UAAU,yBAAyB,aAAa,OAAO,IAAI,CAAC;AAG3E,MAAI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AAO,KAAK,aAAa,EAAE,SAAS,gBAAgB;AAAA,IAC9D,gBAAiB,aAAa,sBAAsB,uBAClD;AAAA,IAEF,MAAM;AAAA,IACN,QAAQ,QAAQ;AAAA,EAClB;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jakubmazanec/args",
3
- "version": "2.0.4",
3
+ "version": "2.0.5-unstable.1be6df9",
4
4
  "description": "TypeScript-first library for parsing command line arguments.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -27,27 +27,27 @@
27
27
  "typecheck": "tsc --project tsconfig.typecheck.json"
28
28
  },
29
29
  "dependencies": {
30
- "@jakubmazanec/error": "^3.0.0",
31
- "@jakubmazanec/ts-utils": "^3.0.0",
30
+ "@jakubmazanec/error": "3.0.5-unstable.1be6df9",
31
+ "@jakubmazanec/ts-utils": "3.0.5-unstable.1be6df9",
32
32
  "lodash.camelcase": "^4.3.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@jakubmazanec/eslint-config": "^3.2.0",
35
+ "@jakubmazanec/eslint-config": "4.0.0-unstable.1be6df9",
36
36
  "@types/lodash": "^4.17.13",
37
37
  "@types/lodash.camelcase": "^4.3.9",
38
38
  "@types/node": "^22.9.0",
39
- "@vitest/coverage-v8": "^2.1.4",
39
+ "@vitest/coverage-v8": "^2.1.5",
40
40
  "concurrently": "^9.1.0",
41
41
  "del-cli": "^6.0.0",
42
42
  "esbuild": "^0.24.0",
43
- "eslint": "^8.56.0",
43
+ "eslint": "^9.15.0",
44
44
  "lodash": "^4.17.21",
45
45
  "prettier": "^3.3.3",
46
46
  "prettier-plugin-packagejson": "^2.5.3",
47
47
  "typedoc": "^0.26.11",
48
48
  "typedoc-plugin-markdown": "^4.2.10",
49
49
  "typescript": "^5.6.3",
50
- "vitest": "^2.1.4"
50
+ "vitest": "^2.1.5"
51
51
  },
52
52
  "packageManager": "npm@10.9.0",
53
53
  "engines": {
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/sort-type-constituents -- sorting hurts readability in this case */
1
+ /* eslint-disable perfectionist/sort-union-types -- sorting hurts readability in this case */
2
2
 
3
3
  import {type ShortOptionName} from './ShortOptionName.js';
4
4
 
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/sort-type-constituents -- sorting hurts readability in this case */
1
+ /* eslint-disable perfectionist/sort-union-types -- sorting hurts readability in this case */
2
2
 
3
3
  export type BooleanParameterConfig =
4
4
  | {
@@ -1,54 +1,54 @@
1
1
  /** Short option name. */
2
2
  export type ShortOptionName =
3
- | 'A'
4
3
  | 'a'
5
- | 'B'
4
+ | 'A'
6
5
  | 'b'
7
- | 'C'
6
+ | 'B'
8
7
  | 'c'
9
- | 'D'
8
+ | 'C'
10
9
  | 'd'
11
- | 'E'
10
+ | 'D'
12
11
  | 'e'
13
- | 'F'
12
+ | 'E'
14
13
  | 'f'
15
- | 'G'
14
+ | 'F'
16
15
  | 'g'
17
- | 'H'
16
+ | 'G'
18
17
  | 'h'
19
- | 'I'
18
+ | 'H'
20
19
  | 'i'
21
- | 'J'
20
+ | 'I'
22
21
  | 'j'
23
- | 'K'
22
+ | 'J'
24
23
  | 'k'
25
- | 'L'
24
+ | 'K'
26
25
  | 'l'
27
- | 'M'
26
+ | 'L'
28
27
  | 'm'
29
- | 'N'
28
+ | 'M'
30
29
  | 'n'
31
- | 'O'
30
+ | 'N'
32
31
  | 'o'
33
- | 'P'
32
+ | 'O'
34
33
  | 'p'
35
- | 'Q'
34
+ | 'P'
36
35
  | 'q'
37
- | 'R'
36
+ | 'Q'
38
37
  | 'r'
39
- | 'S'
38
+ | 'R'
40
39
  | 's'
41
- | 'T'
40
+ | 'S'
42
41
  | 't'
43
- | 'U'
42
+ | 'T'
44
43
  | 'u'
45
- | 'V'
44
+ | 'U'
46
45
  | 'v'
47
- | 'W'
46
+ | 'V'
48
47
  | 'w'
49
- | 'X'
48
+ | 'W'
50
49
  | 'x'
51
- | 'Y'
50
+ | 'X'
52
51
  | 'y'
53
- | 'Z'
54
- | 'z';
52
+ | 'Y'
53
+ | 'z'
54
+ | 'Z';
@@ -8,11 +8,11 @@ import {isFalseString} from './isFalseString.js';
8
8
  * */
9
9
  export function castValue<T extends 'boolean' | 'number' | 'string'>(
10
10
  value: string,
11
- type?: T,
11
+ targetType?: T,
12
12
  ): T extends 'boolean' ? boolean
13
13
  : T extends 'number' ? number
14
14
  : string {
15
- switch (type) {
15
+ switch (targetType) {
16
16
  case 'boolean': {
17
17
  let booleanString = String(value).toLowerCase();
18
18
 
@@ -28,7 +28,21 @@ export function castValue<T extends 'boolean' | 'number' | 'string'>(
28
28
  : string;
29
29
  }
30
30
 
31
+ case 'string': {
32
+ return String(value) as T extends 'boolean' ? boolean
33
+ : T extends 'number' ? number
34
+ : string;
35
+ }
36
+
37
+ case undefined: {
38
+ return String(value) as T extends 'boolean' ? boolean
39
+ : T extends 'number' ? number
40
+ : string;
41
+ }
42
+
31
43
  default: {
44
+ targetType satisfies never;
45
+
32
46
  return String(value) as T extends 'boolean' ? boolean
33
47
  : T extends 'number' ? number
34
48
  : string;
@@ -11,6 +11,6 @@ export * from './internals/isOptionLike.js';
11
11
  export * from './internals/isShortOption.js';
12
12
  export * from './internals/isShortOptionsGroup.js';
13
13
  export * from './internals/isTrueString.js';
14
- export * from './internals/NormalizedCommandConfig.js';
15
- export * from './internals/NormalizedCommandsConfig.js';
14
+ export type * from './internals/NormalizedCommandConfig.js';
15
+ export type * from './internals/NormalizedCommandsConfig.js';
16
16
  export * from './internals/OptionScope.js';
package/source/main.ts CHANGED
@@ -1,18 +1,18 @@
1
- export * from './Arguments.js';
2
- export * from './ArgumentsCommand.js';
3
- export * from './ArgumentsOptions.js';
4
- export * from './ArgumentsParameters.js';
5
- export * from './ArgumentsUnknownOptions.js';
6
- export * from './Argv.js';
7
- export * from './CommandConfig.js';
8
- export * from './CommandsConfig.js';
9
- export * from './LongOptionName.js';
10
- export * from './OptionConfig.js';
11
- export * from './OptionsConfig.js';
12
- export * from './ParameterConfig.js';
13
- export * from './ParametersConfig.js';
1
+ export type * from './Arguments.js';
2
+ export type * from './ArgumentsCommand.js';
3
+ export type * from './ArgumentsOptions.js';
4
+ export type * from './ArgumentsParameters.js';
5
+ export type * from './ArgumentsUnknownOptions.js';
6
+ export type * from './Argv.js';
7
+ export type * from './CommandConfig.js';
8
+ export type * from './CommandsConfig.js';
9
+ export type * from './LongOptionName.js';
10
+ export type * from './OptionConfig.js';
11
+ export type * from './OptionsConfig.js';
12
+ export type * from './ParameterConfig.js';
13
+ export type * from './ParametersConfig.js';
14
14
  export * from './parseArguments.js';
15
- export * from './ParserConfig.js';
15
+ export type * from './ParserConfig.js';
16
16
  export * from './ParsingError.js';
17
- export * from './ShortOptionName.js';
17
+ export type * from './ShortOptionName.js';
18
18
  export * from './ValidationError.js';
@@ -43,8 +43,6 @@ function commitOptionScope(optionScope: OptionScope, parsedOptions: Record<strin
43
43
  * @return Parsed arguments.
44
44
  *
45
45
  */
46
- // TODO: refactor
47
- // eslint-disable-next-line complexity -- TODO
48
46
  export function parseArguments<const O extends ParserConfig>(
49
47
  argv: Argv,
50
48
  parserConfig: O,
@@ -117,10 +115,9 @@ export function parseArguments<const O extends ParserConfig>(
117
115
  );
118
116
  }
119
117
  }
120
- }
121
118
 
122
- optionScope = null;
123
- commandScope = null;
119
+ commandScope = null;
120
+ }
124
121
 
125
122
  let optionName = arg;
126
123
  let optionValue;