@trenskow/arguments-parser 0.3.122 → 0.3.123

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
@@ -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.
@@ -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
 
@@ -83,7 +85,7 @@ const argumentsParser = (shell) => {
83
85
 
84
86
  const argumentsParser = (
85
87
  {
86
- args = process.argv.slice(shell.argsOffset || 2),
88
+ args = process.argv.slice(shell.argsOffset || 3),
87
89
  argvLevel = 0,
88
90
  placeholder = '<>',
89
91
  command,
@@ -6,7 +6,7 @@
6
6
  // See license in LICENSE.
7
7
  //
8
8
 
9
- const zsh = (items) => {
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/arguments-parser",
3
- "version": "0.3.122",
3
+ "version": "0.3.123",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",