1
- # Arg [ ![ CircleCI ] ( https://circleci.com/gh/vercel/arg.svg?style=svg )] ( https://circleci.com/gh/vercel/arg )
1
+ # Arg
2
2
3
3
` arg ` is an unopinionated, no-frills CLI argument parser.
4
4
5
5
## Installation
6
6
7
- Use Yarn or NPM to install.
8
-
9
- ``` console
10
- $ yarn add arg
11
- ```
12
-
13
- or
14
-
15
- ``` console
16
- $ npm install arg
7
+ ``` bash
8
+ npm install arg
17
9
```
18
10
19
11
## Usage
@@ -35,7 +27,10 @@ in which case an empty array is returned).
35
27
const arg = require (' arg' );
36
28
37
29
// `options` is an optional parameter
38
- const args = arg (spec, options = {permissive: false , argv: process .argv .slice (2 )});
30
+ const args = arg (
31
+ spec,
32
+ (options = { permissive: false , argv: process .argv .slice (2 ) })
33
+ );
39
34
```
40
35
41
36
For example:
@@ -50,18 +45,18 @@ const arg = require('arg');
50
45
51
46
const args = arg ({
52
47
// Types
53
- ' --help' : Boolean ,
48
+ ' --help' : Boolean ,
54
49
' --version' : Boolean ,
55
- ' --verbose' : arg .COUNT , // Counts the number of times --verbose is passed
56
- ' --port' : Number , // --port <number> or --port=<number>
57
- ' --name' : String , // --name <string> or --name=<string>
58
- ' --tag' : [String ], // --tag <string> or --tag=<string>
50
+ ' --verbose' : arg .COUNT , // Counts the number of times --verbose is passed
51
+ ' --port' : Number , // --port <number> or --port=<number>
52
+ ' --name' : String , // --name <string> or --name=<string>
53
+ ' --tag' : [String ], // --tag <string> or --tag=<string>
59
54
60
55
// Aliases
61
- ' -v' : ' --verbose' ,
62
- ' -n' : ' --name' , // -n <string>; result is stored in --name
63
- ' --label' : ' --name' // --label <string> or --label=<string>;
64
- // result is stored in --name
56
+ ' -v' : ' --verbose' ,
57
+ ' -n' : ' --name' , // -n <string>; result is stored in --name
58
+ ' --label' : ' --name' // --label <string> or --label=<string>;
59
+ // result is stored in --name
65
60
});
66
61
67
62
console .log (args);
@@ -104,19 +99,32 @@ For custom handlers that wish to behave as flags, you may pass the function thro
104
99
``` javascript
105
100
const arg = require (' arg' );
106
101
107
- const argv = [' --foo' , ' bar' , ' -ff' , ' baz' , ' --foo' , ' --foo' , ' qux' , ' -fff' , ' qix' ];
102
+ const argv = [
103
+ ' --foo' ,
104
+ ' bar' ,
105
+ ' -ff' ,
106
+ ' baz' ,
107
+ ' --foo' ,
108
+ ' --foo' ,
109
+ ' qux' ,
110
+ ' -fff' ,
111
+ ' qix'
112
+ ];
108
113
109
114
function myHandler (value , argName , previousValue ) {
110
115
/* `value` is always `true` */
111
116
return ' na ' + (previousValue || ' batman!' );
112
117
}
113
118
114
- const args = arg ({
115
- ' --foo' : arg .flag (myHandler),
116
- ' -f' : ' --foo'
117
- }, {
118
- argv
119
- });
119
+ const args = arg (
120
+ {
121
+ ' --foo' : arg .flag (myHandler),
122
+ ' -f' : ' --foo'
123
+ },
124
+ {
125
+ argv
126
+ }
127
+ );
120
128
121
129
console .log (args);
122
130
/*
@@ -136,12 +144,15 @@ const arg = require('arg');
136
144
137
145
const argv = [' -AAAA' , ' -BBBB' ];
138
146
139
- const args = arg ({
140
- ' -A' : arg .COUNT ,
141
- ' -B' : [Boolean ]
142
- }, {
143
- argv
144
- });
147
+ const args = arg (
148
+ {
149
+ ' -A' : arg .COUNT ,
150
+ ' -B' : [Boolean ]
151
+ },
152
+ {
153
+ argv
154
+ }
155
+ );
145
156
146
157
console .log (args);
147
158
/*
@@ -168,7 +179,8 @@ For example:
168
179
const args = arg (
169
180
{
170
181
' --foo' : String
171
- }, {
182
+ },
183
+ {
172
184
argv: [' hello' , ' --foo' , ' world' ]
173
185
}
174
186
);
@@ -194,13 +206,22 @@ For example:
194
206
``` javascript
195
207
const arg = require (' arg' );
196
208
197
- const argv = [' --foo' , ' hello' , ' --qux' , ' qix' , ' --bar' , ' 12345' , ' hello again' ];
209
+ const argv = [
210
+ ' --foo' ,
211
+ ' hello' ,
212
+ ' --qux' ,
213
+ ' qix' ,
214
+ ' --bar' ,
215
+ ' 12345' ,
216
+ ' hello again'
217
+ ];
198
218
199
219
const args = arg (
200
220
{
201
221
' --foo' : String ,
202
222
' --bar' : Number
203
- }, {
223
+ },
224
+ {
204
225
argv,
205
226
permissive: true
206
227
}
@@ -211,10 +232,10 @@ results in:
211
232
212
233
``` javascript
213
234
const args = {
214
- _: [' --qux' , ' qix' , ' hello again' ],
215
- ' --foo' : ' hello' ,
216
- ' --bar' : 12345
217
- }
235
+ _: [' --qux' , ' qix' , ' hello again' ],
236
+ ' --foo' : ' hello' ,
237
+ ' --bar' : 12345
238
+ };
218
239
```
219
240
220
241
#### ` stopAtPositional `
@@ -233,7 +254,8 @@ const args = arg(
233
254
{
234
255
' --foo' : Boolean ,
235
256
' --bar' : Boolean
236
- }, {
257
+ },
258
+ {
237
259
argv,
238
260
stopAtPositional: true
239
261
}
@@ -257,16 +279,17 @@ differentiate between user error and developer error (bug).
257
279
##### ARG_UNKNOWN_OPTION
258
280
259
281
If an unknown option (not defined in the spec object) is passed, an error with code ` ARG_UNKNOWN_OPTION ` will be thrown:
282
+
260
283
``` js
261
284
// cli.js
262
285
try {
263
- require (' arg' )({ ' --hi' : String });
286
+ require (' arg' )({ ' --hi' : String });
264
287
} catch (err) {
265
- if (err .code === ' ARG_UNKNOWN_OPTION' ) {
266
- console .log (err .message );
267
- } else {
268
- throw err;
269
- }
288
+ if (err .code === ' ARG_UNKNOWN_OPTION' ) {
289
+ console .log (err .message );
290
+ } else {
291
+ throw err;
292
+ }
270
293
}
271
294
```
272
295
@@ -291,7 +314,4 @@ if (!args['--name']) throw new Error('missing required argument: --name');
291
314
292
315
# License
293
316
294
- Copyright © ; 2017-2019 by ZEIT, Inc.
295
- Copyright © ; 2020 by Vercel, Inc.
296
-
297
317
Released under the [ MIT License] ( LICENSE.md ) .
0 commit comments