1 | # args-parser
|
2 | Straight-forward node.js arguments parser.
|
3 |
|
4 | ## Get the module
|
5 |
|
6 | ```bash
|
7 | $ npm install args-parser
|
8 | ```
|
9 |
|
10 | ## How to use it?
|
11 |
|
12 | ### args(arguments)
|
13 |
|
14 | Simply call the module passing it an `arguments` array such as `process.argv`:
|
15 |
|
16 | ```javascript
|
17 | const args = require("args-parser")(process.argv)
|
18 |
|
19 | console.info(args)
|
20 | ```
|
21 |
|
22 | The returned value is an `Object` having a key for each argument given, and eventually a value if it's found an `=` sign.
|
23 |
|
24 | Considering that simple command:
|
25 |
|
26 | ```bash
|
27 | $ node ./script.js careful -dangerous --tomatoes=3 --tonight
|
28 | ```
|
29 |
|
30 | Will return:
|
31 |
|
32 | ```json
|
33 | {
|
34 | "careful": true,
|
35 | "dangerous": true,
|
36 | "tomatoes": 3,
|
37 | "tonight": true
|
38 | }
|
39 | ```
|
40 |
|
41 | So then you can easily check what you need:
|
42 |
|
43 | ```javascript
|
44 | if (args.careful) {
|
45 | // Do something
|
46 | }
|
47 | ```
|