OSDN Git Service

Regular updates
[twpd/master.git] / yargs.md
1 ---
2 title: Yargs
3 category: JavaScript libraries
4 ---
5
6 ### Basic usage
7
8 ```js
9 var argv = require('yargs').argv;
10
11 argv._         // [ ... ]
12 argv.$0        // "node bin/mybin"
13 argv.verbose   // --verbose
14 ```
15
16 ### Help and version
17
18 ```js
19 var argv = require('yargs')
20
21   // version
22   .alias('v', 'version')
23   .version(function() { return require('../package').version; })
24   .describe('v', 'show version information')
25
26   // help text
27   .alias('h', 'help')
28   .help('help')
29   .usage('Usage: $0 -x [num]')
30   .showHelpOnFail(false, "Specify --help for available options")
31 ```
32
33 ### Options
34
35 ```js
36   .option('f', {
37       alias : 'file',
38       describe: 'x marks the spot',
39       type: 'string', /* array | boolean | string */
40       nargs: 1,
41       demand: true,
42       demand: 'file is required',
43       default: '/etc/passwd'
44       // also: count:true, requiresArg:true
45   })
46
47   .options({
48     f: { ... }
49   })
50 ```
51
52 ### Examples and more help stuff
53
54 ```js
55   // more help
56   .example('...')
57   .epilog('copyright 2015')
58   .command('start', 'start a server')
59 ```
60
61 ### Stacking
62
63 ```js
64   .count('verbose')
65
66 argv.verbose // -vvv => 3
67 ```
68
69 ### Reject non explicits
70
71 ```js
72   .strict()
73 ```
74
75 ### Methods
76
77 ```
78 yargs.showHelp()
79 yargs.help() //=>string
80 ```