@trenskow/arguments-parser 0.2.6 → 0.2.8
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/lib/index.js +47 -17
- package/package.json +2 -2
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(
|
|
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(
|
|
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])
|
|
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(
|
|
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
|
-
|
|
168
|
+
addition = ` ${strings?.options?.help?.allowsMultple || '(allows multiple)'}`;
|
|
155
169
|
} else if (keyPathSchema.required === true) {
|
|
156
|
-
|
|
170
|
+
addition = ` ${strings?.options?.help?.required || '(required)'}`;
|
|
157
171
|
} else if (typeof keyPathSchema.default !== 'undefined') {
|
|
158
|
-
|
|
172
|
+
|
|
173
|
+
let defaultDescription;
|
|
174
|
+
|
|
159
175
|
if (keyPathSchema.type === Boolean) {
|
|
160
|
-
|
|
176
|
+
defaultDescription = keyPathSchema.default === true ? 'enabled' : 'disabled';
|
|
161
177
|
} else {
|
|
162
|
-
|
|
178
|
+
defaultDescription = `\`${keyPathSchema.defaultDescription ?? keyPathSchema.default}\``;
|
|
163
179
|
}
|
|
164
|
-
|
|
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];
|
|
@@ -210,7 +232,12 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
|
|
|
210
232
|
|
|
211
233
|
if (key === 'help') printHelp();
|
|
212
234
|
|
|
213
|
-
if (!keyPaths(schema).all().includes(key))
|
|
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
|
+
}
|
|
214
241
|
|
|
215
242
|
let value;
|
|
216
243
|
|
|
@@ -218,7 +245,10 @@ const argumentsParser = ({ args = process.argv.slice(2), argvLevel = 0, placehol
|
|
|
218
245
|
value = true;
|
|
219
246
|
} else {
|
|
220
247
|
if (args.length <= idx + 1 || args[idx + 1].slice(0, 2) === '--') {
|
|
221
|
-
printHelp(
|
|
248
|
+
printHelp(
|
|
249
|
+
new Error(
|
|
250
|
+
(strings?.options?.help?.errors?.missingArgument || 'Argument missing for option: <option>.')
|
|
251
|
+
.replace('<option>', args[idx])));
|
|
222
252
|
}
|
|
223
253
|
value = args[++idx];
|
|
224
254
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trenskow/arguments-parser",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "Yet another arguments parser.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"homepage": "https://github.com/trenskow/arguments-parser#readme",
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@trenskow/caseit": "^1.4.6",
|
|
28
|
-
"@trenskow/print": "^0.1.
|
|
28
|
+
"@trenskow/print": "^0.1.4",
|
|
29
29
|
"isvalid": "^4.1.27"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|