@trenskow/arguments-parser 0.3.122 → 0.3.124
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 +36 -0
- package/index.js +4 -9
- package/lib/autocomplete/bash.js +64 -0
- package/lib/autocomplete/index.js +7 -5
- package/lib/autocomplete/zsh.js +1 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -80,6 +80,42 @@ Options:
|
|
|
80
80
|
|
|
81
81
|
> The validation schema is described in package [isvalid](https://github.com/trenskow/isvalid).
|
|
82
82
|
|
|
83
|
+
## Autocomplete
|
|
84
|
+
|
|
85
|
+
This supports Bash and ZSH autocomplete of the box.
|
|
86
|
+
|
|
87
|
+
> In both examples below replace `my_script` with the actual name of the script.
|
|
88
|
+
|
|
89
|
+
### Bash
|
|
90
|
+
|
|
91
|
+
The completion script for Bash should be installed as below.
|
|
92
|
+
|
|
93
|
+
````bash
|
|
94
|
+
_my_script() {
|
|
95
|
+
eval "$(AUTOCOMPLETE=bash my_script "${COMP_WORDS[@]}")"
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
complete -F _my_script my_script
|
|
99
|
+
````
|
|
100
|
+
|
|
101
|
+
#### ZSH
|
|
102
|
+
|
|
103
|
+
The completion script for ZSH should be installed as below.
|
|
104
|
+
|
|
105
|
+
````zsh
|
|
106
|
+
#compdef my_script
|
|
107
|
+
|
|
108
|
+
_my_script() {
|
|
109
|
+
|
|
110
|
+
local -a completions
|
|
111
|
+
|
|
112
|
+
eval "$(AUTOCOMPLETE=zsh my_script ${words[@]})"
|
|
113
|
+
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_my_script
|
|
117
|
+
````
|
|
118
|
+
|
|
83
119
|
# License
|
|
84
120
|
|
|
85
121
|
See license in LICENSE.
|
package/index.js
CHANGED
|
@@ -51,20 +51,15 @@ plugins.use('argumentsParser.autocomplete', () => ({
|
|
|
51
51
|
phase: 'pre',
|
|
52
52
|
supportsType: () => true,
|
|
53
53
|
validatorsForType: () => ({
|
|
54
|
-
autocomplete: ['array', '
|
|
54
|
+
autocomplete: ['array', 'function']
|
|
55
55
|
}),
|
|
56
56
|
validate: (data) => data,
|
|
57
|
-
formalize: (data
|
|
58
|
-
|
|
59
|
-
if (!Array.isArray(data)) data = [data];
|
|
57
|
+
formalize: (data) => {
|
|
60
58
|
|
|
61
|
-
if (
|
|
62
|
-
|
|
63
|
-
delete schema.errors.autocomplete;
|
|
59
|
+
if (!(Array.isArray(data) && data.every((data) => typeof data === 'string')) && typeof data !== 'function') {
|
|
60
|
+
throw new Error('Must be a string or a function.');
|
|
64
61
|
}
|
|
65
62
|
|
|
66
|
-
if (data.some((data) => typeof data !== 'string')) throw new Error('Must be a string.');
|
|
67
|
-
|
|
68
63
|
return data;
|
|
69
64
|
|
|
70
65
|
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//
|
|
2
|
+
// bash.js
|
|
3
|
+
// @trenskow/arguments-parser
|
|
4
|
+
//
|
|
5
|
+
// Created by Kristian Trenskow on 2026/08/03
|
|
6
|
+
// See license in LICENSE.
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
export default (items) => {
|
|
10
|
+
|
|
11
|
+
const keys = Object.keys(items);
|
|
12
|
+
|
|
13
|
+
if (keys.length === 0) {
|
|
14
|
+
process.exit(0);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let options = [];
|
|
18
|
+
let files = false;
|
|
19
|
+
|
|
20
|
+
keys
|
|
21
|
+
.forEach((key) => {
|
|
22
|
+
|
|
23
|
+
switch (key) {
|
|
24
|
+
case 'command':
|
|
25
|
+
// fall through
|
|
26
|
+
case 'option':
|
|
27
|
+
|
|
28
|
+
options = options.concat(items[key]
|
|
29
|
+
.map((item) => item.argument));
|
|
30
|
+
|
|
31
|
+
break;
|
|
32
|
+
case 'files':
|
|
33
|
+
files = true;
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (options.length > 0 || files) {
|
|
40
|
+
process.stdout.write('COMPREPLY=()\n');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (options.length > 0) {
|
|
44
|
+
|
|
45
|
+
process.stdout.write('COMPREPLY+=(');
|
|
46
|
+
|
|
47
|
+
options
|
|
48
|
+
.forEach((option) => {
|
|
49
|
+
process.stdout.write(` "${['"', '\\', '$', '`']
|
|
50
|
+
.reduce((option, char) => option.replaceAll(char, `\\${char}`), option)
|
|
51
|
+
}" `);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
process.stdout.write(')\n');
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (files) {
|
|
59
|
+
process.stdout.write('COMPREPLY+=($(compgen -f -- "${COMP_WORDS[COMP_CWORD]}"))\n');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
process.exit(0);
|
|
63
|
+
|
|
64
|
+
};
|
|
@@ -17,9 +17,11 @@ import {
|
|
|
17
17
|
|
|
18
18
|
import caseit from '@trenskow/caseit';
|
|
19
19
|
|
|
20
|
+
import bash from './bash.js';
|
|
20
21
|
import zsh from './zsh.js';
|
|
21
22
|
|
|
22
23
|
const supported = {
|
|
24
|
+
bash,
|
|
23
25
|
zsh
|
|
24
26
|
};
|
|
25
27
|
|
|
@@ -48,7 +50,7 @@ const argumentsParser = (shell) => {
|
|
|
48
50
|
|
|
49
51
|
});
|
|
50
52
|
|
|
51
|
-
const filter = (items, current) => {
|
|
53
|
+
const filter = (items, current = '') => {
|
|
52
54
|
return normalize(items)
|
|
53
55
|
.filter((item) => {
|
|
54
56
|
switch (item.type) {
|
|
@@ -83,7 +85,7 @@ const argumentsParser = (shell) => {
|
|
|
83
85
|
|
|
84
86
|
const argumentsParser = (
|
|
85
87
|
{
|
|
86
|
-
args = process.argv.slice(shell.argsOffset ||
|
|
88
|
+
args = process.argv.slice(shell.argsOffset || 3),
|
|
87
89
|
argvLevel = 0,
|
|
88
90
|
placeholder = '<>',
|
|
89
91
|
command,
|
|
@@ -151,7 +153,7 @@ const argumentsParser = (shell) => {
|
|
|
151
153
|
autocomplete
|
|
152
154
|
}, command) => {
|
|
153
155
|
|
|
154
|
-
const available = filter(await autocomplete?.(args[0] || '') || []);
|
|
156
|
+
const available = filter(await autocomplete?.(args[0] || '') || [], args[0]);
|
|
155
157
|
|
|
156
158
|
if (available.length) {
|
|
157
159
|
return output(available);
|
|
@@ -236,7 +238,7 @@ const argumentsParser = (shell) => {
|
|
|
236
238
|
}];
|
|
237
239
|
}
|
|
238
240
|
|
|
239
|
-
return filter(await schemas[idx].autocomplete?.(current) || []);
|
|
241
|
+
return filter(await schemas[idx].autocomplete?.(current) || [], current);
|
|
240
242
|
|
|
241
243
|
} else {
|
|
242
244
|
|
|
@@ -247,7 +249,7 @@ const argumentsParser = (shell) => {
|
|
|
247
249
|
if (!current.startsWith('-')) {
|
|
248
250
|
if (variadic === 'allow') {
|
|
249
251
|
items = items.concat(
|
|
250
|
-
filter(await autocomplete?.(current) || []));
|
|
252
|
+
filter(await autocomplete?.(current) || [], current));
|
|
251
253
|
}
|
|
252
254
|
}
|
|
253
255
|
|
package/lib/autocomplete/zsh.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// See license in LICENSE.
|
|
7
7
|
//
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
export default (items) => {
|
|
10
10
|
|
|
11
11
|
const keys = Object.keys(items);
|
|
12
12
|
|
|
@@ -49,7 +49,3 @@ const zsh = (items) => {
|
|
|
49
49
|
process.exit(0);
|
|
50
50
|
|
|
51
51
|
};
|
|
52
|
-
|
|
53
|
-
zsh.argsOffset = 3;
|
|
54
|
-
|
|
55
|
-
export default zsh;
|