1 | # Why a wrong args parser?
|
2 |
|
3 | > tl-dr; I didn't wrote a parser.
|
4 |
|
5 | [![Build Status](https://travis-ci.org/pateketrueke/wargs.png)](https://travis-ci.org/pateketrueke/wargs)
|
6 | [![NPM version](https://badge.fury.io/js/wargs.png)](http://badge.fury.io/js/wargs)
|
7 | [![Coverage Status](https://codecov.io/github/pateketrueke/wargs/coverage.svg)](https://codecov.io/github/pateketrueke/wargs)
|
8 |
|
9 | Instead, I used regular expressions for extracting values, flags and other kind of parameters from a string or from an argv-like array.
|
10 |
|
11 | I've tried commander, minimist, yargs, etc. but no one fulfilled my exact requirements, e.g.
|
12 |
|
13 | ```js
|
14 | const str = '/ _csrf=`token` --json accept:"text/plain; charset=utf8"';
|
15 | const argv = ['/', '_csrf=`token`', '--json', 'accept:text/plain; charset=utf8'];
|
16 | ```
|
17 |
|
18 | Both values are representing the same input, the former can be taken from any source while the latter is usually provided by `process.argv.slice(2)`, etc.
|
19 |
|
20 | Most importantly: these modules will won't work with a string as input.
|
21 |
|
22 | **wargs** will do and return: `_`, `data`, `flags` and `params`.
|
23 |
|
24 | ```js
|
25 | {
|
26 | _: ['/'],
|
27 | data: { _csrf: '`token`' },
|
28 | flags: { json: true },
|
29 | params: { accept: 'text/plain; charset=utf8' },
|
30 | }
|
31 | ```
|
32 |
|
33 | Hint: It suits _-and feels-_ very well on a repl for making http requests. ;-)
|
34 |
|
35 | ## How it works
|
36 |
|
37 | **wargs** _understand_ regular flags, `-short` or `--long`, `key:value` and `key=value` params, and `everything` else will be collected as an array of values, e.g.
|
38 |
|
39 | ```js
|
40 | wargs('-x').flags.x; // true
|
41 | wargs('--x').flags.x; // true
|
42 | wargs('x:y').params.x; // y
|
43 | wargs('x=y').data; // { x: 'y' }
|
44 | wargs('x y')._ // ['x', 'y']
|
45 | wargs('--x-y', { camelCase: true }).flags; // { xY: true }
|
46 | wargs('-x y', { format: v => v.toUpperCase() }).flags; // { x: 'Y' }
|
47 | ```
|
48 |
|
49 | ### Options
|
50 |
|
51 | - `format` — function decorator for all values
|
52 | - `camelCase` — normalize keys from `--camel-case` to `camelCase`
|
53 |
|
54 | ### Fun facts
|
55 |
|
56 | - When I was looking for a name for this module I found that `xargs`, `yargs` and `zargs` already existed
|
57 | - I'm totally wrong on calling this module a "parser" for command line arguments, don't be rude
|
58 | - Finally, I discovered that [wargs](http://gameofthrones.wikia.com/wiki/Warg) are a thing from GoT
|
59 |
|
60 | ![OrellWarg](http://vignette2.wikia.nocookie.net/gameofthrones/images/f/fc/OrellWarg.jpg/revision/latest)
|