args-tokens 0.4.1 → 0.5.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.
@@ -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
@@ -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.5.0",
5
5
  "author": {
6
6
  "name": "kazuya kawaguchi",
7
7
  "email": "kawakazu80@gmail.com"
@@ -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
- });