OSDN Git Service

Regular updates
[twpd/master.git] / minimist.md
1 ---
2 title: minimist
3 category: JavaScript libraries
4 layout: 2017/sheet
5 ---
6
7 ### Usage
8
9 ```js
10 var minimist = require('minimist')
11 ```
12 {: .-setup}
13
14 ```js
15 var args = minimist(process.argv.slice(2), {
16   string: 'lang',           // --lang xml
17   boolean: ['version'],     // --version
18   alias: { v: 'version' }
19 })
20 ```
21
22 ```js
23 console.log(args)
24 ```
25
26 All options are optional, but it's recommended you set `string` and `boolean` at least.
27
28 ### All options
29
30 ```js
31 var args = minimist(process.argv.slice(2), {
32   string: [ 'lang' ],
33   boolean: [ 'pager' ],
34   alias: { h: 'help', v: 'version' },
35   default: { lang: 'en' },
36   '--': true,
37   stopEarly: true, /* populate _ with first non-option */
38   unknown: function () { ... } /* invoked on unknown param */
39 })
40 ```
41
42 All options are optional.
43
44 ### Result
45
46 With `--lang xml --no-pager -h index.js package.json`, you get:
47 {: .-setup}
48
49 ```
50 args == {
51   lang: 'xml',
52   version: false,
53   h: true,
54   help: true,
55   _: [ 'index.js', 'package.json' ]
56 }
57 ```
58
59 ## Meow
60
61 ### Help and version
62
63 Use [meow](https://www.npmjs.com/package/meow) to automatically add support for `--help`, `--version` and more.
64 {: .-setup}
65
66 ```js
67 meow(`
68     Usage:
69         $0 FILES [options]
70
71     Options:
72         -h, --help         print usage information
73         -v, --version      show version info and exit
74 `, {
75   alias: { h: 'help', v: 'version' }
76   /* minimist options */
77 })
78 ```
79
80 ### Reference
81
82  * <https://www.npmjs.org/package/minimist>
83  * <https://github.com/substack/minimist>