args-tokens 0.4.1 → 0.6.0

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/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  ## 🐱 Motivation
10
10
 
11
- - Although Node.js's [`parseArgs`](https://nodejs.org/api/util.html#utilparseargsconfig) can return tokens, tokens that the short options are not in the format I expect. Of course, I recoginize the background of [this issue](https://github.com/pkgjs/parseargs/issues/78).
11
+ - Although Node.js [`parseArgs`](https://nodejs.org/api/util.html#utilparseargsconfig) can return tokens, that the short options are not in the format I expect. Of course, I recoginize the background of [this issue](https://github.com/pkgjs/parseargs/issues/78).
12
12
  - `parseArgs` gives the command line args parser a useful util, so the resolution of the options values and the parsing of the tokens are tightly coupled. As a result, Performance is sacrificed. Of course, I recoginize that's the trade-off.
13
13
 
14
14
  ## ⏱️ Benchmark
@@ -167,7 +167,7 @@ const tokens = parseArgs(['--foo', 'bar', '-x', '--bar=baz'])
167
167
  console.log('tokens:', tokens)
168
168
  ```
169
169
 
170
- ## Resolve args value with tokens and arg option schema
170
+ ## Resolve args values with tokens and arg option schema
171
171
 
172
172
  `resolveArgs` is a useful function when you want to resolve values from the tokens obtained by `parseArgs`.
173
173
 
@@ -281,7 +281,7 @@ If you are interested in contributing to `args-tokens`, I highly recommend check
281
281
 
282
282
  This project is inspired by:
283
283
 
284
- - [`utils.parseArgs`](https://nodejs.org/api/util.html#utilparseargsconfig), created by Node.js contributors and [OpenJS Foundation](https://openjsf.org/)
284
+ - [`util.parseArgs`](https://nodejs.org/api/util.html#utilparseargsconfig), created by Node.js contributors and [OpenJS Foundation](https://openjsf.org/)
285
285
  - [`pkgjs/parseargs`](https://github.com/pkgjs/parseargs), created by Node.js CLI package maintainers and Node.js community.
286
286
 
287
287
  ## ©️ License
@@ -3,4 +3,4 @@ export { parseArgs } from './parser.js';
3
3
  export { resolveArgs } from './resolver.js';
4
4
  export type { ParsedArgs, ParseOptions } from './parse';
5
5
  export type { ArgToken, ParserOptions } from './parser';
6
- export type { ArgOptionSchema } from './resolver';
6
+ export type { ArgOptions, ArgOptionSchema } from './resolver';
package/lib/cjs/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  "use strict";
2
+ // SPDX-License-Identifier: MIT
3
+ // Modifier: kazuya kawaguchi (a.k.a. kazupon)
2
4
  Object.defineProperty(exports, "__esModule", { value: true });
3
5
  exports.resolveArgs = exports.parseArgs = exports.parse = void 0;
4
6
  var parse_js_1 = require("./parse.js");
package/lib/cjs/parse.js CHANGED
@@ -1,4 +1,6 @@
1
1
  "use strict";
2
+ // SPDX-License-Identifier: MIT
3
+ // Modifier: kazuya kawaguchi (a.k.a. kazupon)
2
4
  Object.defineProperty(exports, "__esModule", { value: true });
3
5
  exports.parse = parse;
4
6
  const parser_js_1 = require("./parser.js");
@@ -9,9 +9,10 @@ function resolveArgs(options, tokens) {
9
9
  const positionals = [];
10
10
  const longOptionTokens = [];
11
11
  const shortOptionTokens = [];
12
+ let currentLongOption;
12
13
  let currentShortOption;
13
14
  const expandableShortOptions = [];
14
- function toValue() {
15
+ function toShortValue() {
15
16
  if (expandableShortOptions.length === 0) {
16
17
  return undefined;
17
18
  }
@@ -21,28 +22,44 @@ function resolveArgs(options, tokens) {
21
22
  return value;
22
23
  }
23
24
  }
24
- function applyShortOptionValue() {
25
+ function applyLongOptionValue(value = undefined) {
26
+ if (currentLongOption) {
27
+ currentLongOption.value = value;
28
+ longOptionTokens.push({ ...currentLongOption });
29
+ currentLongOption = undefined;
30
+ }
31
+ }
32
+ function applyShortOptionValue(value = undefined) {
25
33
  if (currentShortOption) {
26
- currentShortOption.value = toValue();
34
+ currentShortOption.value = value || toShortValue();
27
35
  shortOptionTokens.push({ ...currentShortOption });
28
36
  currentShortOption = undefined;
29
37
  }
30
38
  }
31
39
  /**
32
- * separate tokens into positionals, long options, and short options, after that resolve values
40
+ * analyze phase to resolve value
41
+ * separate tokens into positionals, long and short options, after that resolve values
33
42
  */
34
43
  // eslint-disable-next-line unicorn/no-for-loop
35
44
  for (let i = 0; i < tokens.length; i++) {
36
45
  const token = tokens[i];
37
46
  if (token.kind === 'positional') {
38
47
  positionals.push(token.value);
39
- applyShortOptionValue(); // check if previous short option is not resolved
48
+ // check if previous option is not resolved
49
+ applyLongOptionValue(token.value);
50
+ applyShortOptionValue(token.value);
40
51
  }
41
52
  else if (token.kind === 'option') {
42
53
  if (token.rawName) {
43
54
  if ((0, parser_js_1.hasLongOptionPrefix)(token.rawName)) {
44
- longOptionTokens.push({ ...token });
45
- applyShortOptionValue(); // check if previous short option is not resolved
55
+ if (token.inlineValue) {
56
+ longOptionTokens.push({ ...token });
57
+ }
58
+ else {
59
+ currentLongOption = { ...token };
60
+ }
61
+ // check if previous short option is not resolved
62
+ applyShortOptionValue();
46
63
  }
47
64
  else if ((0, parser_js_1.isShortOption)(token.rawName)) {
48
65
  if (currentShortOption) {
@@ -50,13 +67,17 @@ function resolveArgs(options, tokens) {
50
67
  expandableShortOptions.push({ ...token });
51
68
  }
52
69
  else {
53
- currentShortOption.value = toValue();
70
+ currentShortOption.value = toShortValue();
54
71
  shortOptionTokens.push({ ...currentShortOption });
55
72
  currentShortOption = { ...token };
56
73
  }
74
+ // check if previous long option is not resolved
75
+ applyLongOptionValue();
57
76
  }
58
77
  else {
59
78
  currentShortOption = { ...token };
79
+ // check if previous long option is not resolved
80
+ applyLongOptionValue();
60
81
  }
61
82
  }
62
83
  }
@@ -67,15 +88,20 @@ function resolveArgs(options, tokens) {
67
88
  shortOptionTokens.push({ ...currentShortOption });
68
89
  currentShortOption = undefined;
69
90
  }
91
+ // check if previous long option is not resolved
92
+ applyLongOptionValue();
70
93
  }
71
94
  }
72
95
  else {
73
- applyShortOptionValue(); // check if previous short option is not resolved
96
+ // check if previous option is not resolved
97
+ applyLongOptionValue();
98
+ applyShortOptionValue();
74
99
  }
75
100
  }
76
101
  /**
77
- * check if the last short option is not resolved
102
+ * check if the last long or short option is not resolved
78
103
  */
104
+ applyLongOptionValue();
79
105
  applyShortOptionValue();
80
106
  /**
81
107
  * resolve values
@@ -90,7 +116,11 @@ function resolveArgs(options, tokens) {
90
116
  // eslint-disable-next-line unicorn/no-null
91
117
  if (option === token.name && token.rawName != null && (0, parser_js_1.hasLongOptionPrefix)(token.rawName)) {
92
118
  validateRequire(token, option, schema);
93
- if (schema.type !== 'boolean') {
119
+ if (schema.type === 'boolean') {
120
+ // NOTE: re-set value to undefined, because long boolean type option is set on analyze phase
121
+ token.value = undefined;
122
+ }
123
+ else {
94
124
  validateValue(token, option, schema);
95
125
  }
96
126
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
@@ -105,7 +135,11 @@ function resolveArgs(options, tokens) {
105
135
  // eslint-disable-next-line unicorn/no-null
106
136
  if (schema.short === token.name && token.rawName != null && (0, parser_js_1.isShortOption)(token.rawName)) {
107
137
  validateRequire(token, option, schema);
108
- if (schema.type !== 'boolean') {
138
+ if (schema.type === 'boolean') {
139
+ // NOTE: re-set value to undefined, because short boolean type option is set on analyze phase
140
+ token.value = undefined;
141
+ }
142
+ else {
109
143
  validateValue(token, option, schema);
110
144
  }
111
145
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
@@ -3,4 +3,4 @@ export { parseArgs } from './parser.js';
3
3
  export { resolveArgs } from './resolver.js';
4
4
  export type { ParsedArgs, ParseOptions } from './parse';
5
5
  export type { ArgToken, ParserOptions } from './parser';
6
- export type { ArgOptionSchema } from './resolver';
6
+ export type { ArgOptions, ArgOptionSchema } from './resolver';
package/lib/esm/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Modifier: kazuya kawaguchi (a.k.a. kazupon)
1
3
  export { parse } from './parse.js';
2
4
  export { parseArgs } from './parser.js';
3
5
  export { resolveArgs } from './resolver.js';
package/lib/esm/parse.js CHANGED
@@ -1,3 +1,5 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // Modifier: kazuya kawaguchi (a.k.a. kazupon)
1
3
  import { parseArgs } from './parser.js';
2
4
  import { resolveArgs } from './resolver.js';
3
5
  const DEFAULT_OPTIONS = {
@@ -6,9 +6,10 @@ export function resolveArgs(options, tokens) {
6
6
  const positionals = [];
7
7
  const longOptionTokens = [];
8
8
  const shortOptionTokens = [];
9
+ let currentLongOption;
9
10
  let currentShortOption;
10
11
  const expandableShortOptions = [];
11
- function toValue() {
12
+ function toShortValue() {
12
13
  if (expandableShortOptions.length === 0) {
13
14
  return undefined;
14
15
  }
@@ -18,28 +19,44 @@ export function resolveArgs(options, tokens) {
18
19
  return value;
19
20
  }
20
21
  }
21
- function applyShortOptionValue() {
22
+ function applyLongOptionValue(value = undefined) {
23
+ if (currentLongOption) {
24
+ currentLongOption.value = value;
25
+ longOptionTokens.push({ ...currentLongOption });
26
+ currentLongOption = undefined;
27
+ }
28
+ }
29
+ function applyShortOptionValue(value = undefined) {
22
30
  if (currentShortOption) {
23
- currentShortOption.value = toValue();
31
+ currentShortOption.value = value || toShortValue();
24
32
  shortOptionTokens.push({ ...currentShortOption });
25
33
  currentShortOption = undefined;
26
34
  }
27
35
  }
28
36
  /**
29
- * separate tokens into positionals, long options, and short options, after that resolve values
37
+ * analyze phase to resolve value
38
+ * separate tokens into positionals, long and short options, after that resolve values
30
39
  */
31
40
  // eslint-disable-next-line unicorn/no-for-loop
32
41
  for (let i = 0; i < tokens.length; i++) {
33
42
  const token = tokens[i];
34
43
  if (token.kind === 'positional') {
35
44
  positionals.push(token.value);
36
- applyShortOptionValue(); // check if previous short option is not resolved
45
+ // check if previous option is not resolved
46
+ applyLongOptionValue(token.value);
47
+ applyShortOptionValue(token.value);
37
48
  }
38
49
  else if (token.kind === 'option') {
39
50
  if (token.rawName) {
40
51
  if (hasLongOptionPrefix(token.rawName)) {
41
- longOptionTokens.push({ ...token });
42
- applyShortOptionValue(); // check if previous short option is not resolved
52
+ if (token.inlineValue) {
53
+ longOptionTokens.push({ ...token });
54
+ }
55
+ else {
56
+ currentLongOption = { ...token };
57
+ }
58
+ // check if previous short option is not resolved
59
+ applyShortOptionValue();
43
60
  }
44
61
  else if (isShortOption(token.rawName)) {
45
62
  if (currentShortOption) {
@@ -47,13 +64,17 @@ export function resolveArgs(options, tokens) {
47
64
  expandableShortOptions.push({ ...token });
48
65
  }
49
66
  else {
50
- currentShortOption.value = toValue();
67
+ currentShortOption.value = toShortValue();
51
68
  shortOptionTokens.push({ ...currentShortOption });
52
69
  currentShortOption = { ...token };
53
70
  }
71
+ // check if previous long option is not resolved
72
+ applyLongOptionValue();
54
73
  }
55
74
  else {
56
75
  currentShortOption = { ...token };
76
+ // check if previous long option is not resolved
77
+ applyLongOptionValue();
57
78
  }
58
79
  }
59
80
  }
@@ -64,15 +85,20 @@ export function resolveArgs(options, tokens) {
64
85
  shortOptionTokens.push({ ...currentShortOption });
65
86
  currentShortOption = undefined;
66
87
  }
88
+ // check if previous long option is not resolved
89
+ applyLongOptionValue();
67
90
  }
68
91
  }
69
92
  else {
70
- applyShortOptionValue(); // check if previous short option is not resolved
93
+ // check if previous option is not resolved
94
+ applyLongOptionValue();
95
+ applyShortOptionValue();
71
96
  }
72
97
  }
73
98
  /**
74
- * check if the last short option is not resolved
99
+ * check if the last long or short option is not resolved
75
100
  */
101
+ applyLongOptionValue();
76
102
  applyShortOptionValue();
77
103
  /**
78
104
  * resolve values
@@ -87,7 +113,11 @@ export function resolveArgs(options, tokens) {
87
113
  // eslint-disable-next-line unicorn/no-null
88
114
  if (option === token.name && token.rawName != null && hasLongOptionPrefix(token.rawName)) {
89
115
  validateRequire(token, option, schema);
90
- if (schema.type !== 'boolean') {
116
+ if (schema.type === 'boolean') {
117
+ // NOTE: re-set value to undefined, because long boolean type option is set on analyze phase
118
+ token.value = undefined;
119
+ }
120
+ else {
91
121
  validateValue(token, option, schema);
92
122
  }
93
123
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
@@ -102,7 +132,11 @@ export function resolveArgs(options, tokens) {
102
132
  // eslint-disable-next-line unicorn/no-null
103
133
  if (schema.short === token.name && token.rawName != null && isShortOption(token.rawName)) {
104
134
  validateRequire(token, option, schema);
105
- if (schema.type !== 'boolean') {
135
+ if (schema.type === 'boolean') {
136
+ // NOTE: re-set value to undefined, because short boolean type option is set on analyze phase
137
+ token.value = undefined;
138
+ }
139
+ else {
106
140
  validateValue(token, option, schema);
107
141
  }
108
142
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "args-tokens",
3
3
  "description": "parseArgs tokens compatibility and more high-performance parser",
4
- "version": "0.4.1",
4
+ "version": "0.6.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -70,10 +70,10 @@
70
70
  "@eslint/markdown": "^6.2.2",
71
71
  "@kazupon/eslint-config": "^0.19.0",
72
72
  "@kazupon/prettier-config": "^0.1.1",
73
- "@types/node": "^22.13.1",
74
- "@vitest/eslint-plugin": "^1.1.28",
75
- "bumpp": "^10.0.2",
76
- "eslint": "^9.20.0",
73
+ "@types/node": "^22.13.4",
74
+ "@vitest/eslint-plugin": "^1.1.31",
75
+ "bumpp": "^10.0.3",
76
+ "eslint": "^9.20.1",
77
77
  "eslint-config-prettier": "^10.0.1",
78
78
  "eslint-plugin-jsonc": "^2.19.1",
79
79
  "eslint-plugin-promise": "^7.2.1",
@@ -82,11 +82,11 @@
82
82
  "eslint-plugin-yml": "^1.16.0",
83
83
  "gh-changelogen": "^0.2.8",
84
84
  "jsr": "^0.13.3",
85
- "knip": "^5.44.0",
85
+ "knip": "^5.44.1",
86
86
  "lint-staged": "^15.4.3",
87
87
  "mitata": "^1.0.34",
88
88
  "pkg-pr-new": "^0.0.39",
89
- "prettier": "^3.5.0",
89
+ "prettier": "^3.5.1",
90
90
  "typescript": "^5.7.3",
91
91
  "typescript-eslint": "^8.24.0",
92
92
  "vitest": "^3.0.5"
@@ -1 +0,0 @@
1
- export {};
@@ -1,6 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const vitest_1 = require("vitest");
4
- (0, vitest_1.test)('ArgValues', () => {
5
- (0, vitest_1.expectTypeOf)().toEqualTypeOf();
6
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- import { expectTypeOf, test } from 'vitest';
2
- test('ArgValues', () => {
3
- expectTypeOf().toEqualTypeOf();
4
- });