@trenskow/arguments-parser 0.2.5 → 0.2.7

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 (2) hide show
  1. package/lib/index.js +47 -19
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -23,7 +23,7 @@ plugins.use(() => ({
23
23
  formalize: (schema) => schema
24
24
  }));
25
25
 
26
- const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placeholder = '<>', command } = {}) => {
26
+ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placeholder = '<>', command, strings = {} } = {}) => {
27
27
 
28
28
  const [ opening, closing ] = placeholder.split('');
29
29
 
@@ -51,9 +51,11 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
51
51
  const printHelp = (error) => {
52
52
 
53
53
  print();
54
- print(`Usage: ${base} ${opening}command${closing}`);
54
+ print((strings?.commands?.help?.usage || 'Usage: <base> <command>')
55
+ .replace('<base>', base)
56
+ .replace('<command>', `${opening}command${closing}`));
55
57
  print();
56
- print('Available commands:');
58
+ print(strings?.commands?.help?.available || 'Available commands:');
57
59
  print();
58
60
 
59
61
  const tools = Object.keys(commands).map((key) => ({
@@ -64,12 +66,13 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
64
66
  tools.sort((a, b) => a.name > b.name ? 1 : -1);
65
67
 
66
68
  list(Object.fromEntries(tools
67
- .map((tool) => [tool.name, tool.description || 'No description'])));
69
+ .map((tool) => [tool.name, tool.description || (strings?.commands?.help?.noDescription || 'No description')])));
68
70
 
69
71
  if (error) {
70
72
  print();
71
73
  print();
72
- print(`Error: ${error.message}`);
74
+ print((strings?.commands?.help?.error || 'Error: <message>')
75
+ .replace('<message>', error.message));
73
76
  }
74
77
 
75
78
  print();
@@ -82,7 +85,12 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
82
85
 
83
86
  const tool = caseit(args[0]);
84
87
 
85
- if (!commands[tool]) printHelp(new Error(`${args[0]}: Command not found.`));
88
+ if (!commands[tool]) {
89
+ printHelp(
90
+ new Error(
91
+ (strings?.commands?.help?.errors?.notFound || '<command>: Command not found')
92
+ .replace('<command>', args[0])));
93
+ }
86
94
 
87
95
  try {
88
96
 
@@ -92,7 +100,8 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
92
100
  args: args.slice(1),
93
101
  argvLevel: argvLevel + 1,
94
102
  placeholder,
95
- command: commands[tool]
103
+ command: commands[tool],
104
+ strings
96
105
  })
97
106
  });
98
107
 
@@ -128,7 +137,10 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
128
137
  }
129
138
 
130
139
  print();
131
- print.nn(`Usage: ${base} ${opening}options${closing}`);
140
+ print.nn(
141
+ (strings?.options?.help?.usage || 'Usage: <base> <options>')
142
+ .replace('<base>', base)
143
+ .replace('<options>', `${opening}options${closing}`));
132
144
 
133
145
  if (options.help?.postfix) print.nn(options.help.postfix);
134
146
 
@@ -137,7 +149,7 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
137
149
  if (allKeyPaths.length) {
138
150
 
139
151
  print();
140
- print('Options:');
152
+ print(strings?.options?.help?.options || 'Options:');
141
153
 
142
154
  list(Object.fromEntries(allKeyPaths
143
155
  .map((keyPath) => {
@@ -150,19 +162,29 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
150
162
  description += ` (${Object.keys(keyPathSchema.enum).map((value) => `\`${value}\``).join(', ')})`;
151
163
  }
152
164
 
165
+ let addition;
166
+
153
167
  if (keyPathSchema.type === Array) {
154
- description += ' (allows multiple)';
168
+ addition = ` ${strings?.options?.help?.allowsMultple || '(allows multiple)'}`;
155
169
  } else if (keyPathSchema.required === true) {
156
- description += ' (required)';
170
+ addition = ` ${strings?.options?.help?.required || '(required)'}`;
157
171
  } else if (typeof keyPathSchema.default !== 'undefined') {
158
- description += ' (default: ';
172
+
173
+ let defaultDescription;
174
+
159
175
  if (keyPathSchema.type === Boolean) {
160
- description += keyPathSchema.default === true ? 'enabled' : 'disabled';
176
+ defaultDescription = keyPathSchema.default === true ? 'enabled' : 'disabled';
161
177
  } else {
162
- description += `\`${keyPathSchema.defaultDescription ?? keyPathSchema.default}\``;
178
+ defaultDescription = `\`${keyPathSchema.defaultDescription ?? keyPathSchema.default}\``;
163
179
  }
164
- description += ')';
180
+
181
+ addition = ` ${(strings?.options?.help?.default || '(default: <default>)')
182
+ .replace('<default>', defaultDescription)}`;
183
+
165
184
  }
185
+
186
+ if (addition) description += addition;
187
+
166
188
  description += '.';
167
189
 
168
190
  return [`--${caseit(keyPath, 'kebab')}`, description];
@@ -175,8 +197,6 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
175
197
 
176
198
  if (error) {
177
199
 
178
- print();
179
-
180
200
  (error.errors || [error])
181
201
  .forEach((error) => {
182
202
  if (error.keyPath) {
@@ -212,7 +232,12 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
212
232
 
213
233
  if (key === 'help') printHelp();
214
234
 
215
- if (!keyPaths(schema).all().includes(key)) printHelp(new Error(`Unknown option: ${args[idx]}.`));
235
+ if (!keyPaths(schema).all().includes(key)) {
236
+ printHelp(
237
+ new Error(
238
+ (strings?.options?.help?.errors?.unknownOption || 'Unknown option: <option>.')
239
+ .replace('<option>', args[idx])));
240
+ }
216
241
 
217
242
  let value;
218
243
 
@@ -220,7 +245,10 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
220
245
  value = true;
221
246
  } else {
222
247
  if (args.length <= idx + 1 || args[idx + 1].slice(0, 2) === '--') {
223
- printHelp(new Error(`Argument missing for option: ${args[idx]}.`));
248
+ printHelp(
249
+ new Error(
250
+ (strings?.options?.help?.errors?.missingArgument || 'Argument missing for option: <option>.')
251
+ .replace('<option>', args[idx])));
224
252
  }
225
253
  value = args[++idx];
226
254
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/arguments-parser",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",