cheatsheets/yargs.md

81 lines
1.2 KiB
Markdown
Raw Permalink Normal View History

2015-04-05 10:54:58 +00:00
---
title: Yargs
2015-11-24 05:09:17 +00:00
category: JavaScript libraries
2015-04-05 10:54:58 +00:00
---
2015-04-06 10:37:34 +00:00
### Basic usage
2015-04-05 10:54:58 +00:00
```js
var argv = require('yargs').argv;
argv._ // [ ... ]
argv.$0 // "node bin/mybin"
argv.verbose // --verbose
```
2015-04-06 10:37:34 +00:00
### Help and version
```js
var argv = require('yargs')
// version
.alias('v', 'version')
.version(function() { return require('../package').version; })
.describe('v', 'show version information')
// help text
2015-05-29 18:11:28 +00:00
.alias('h', 'help')
.help('help')
2015-04-06 10:37:34 +00:00
.usage('Usage: $0 -x [num]')
.showHelpOnFail(false, "Specify --help for available options")
```
### Options
```js
.option('f', {
alias : 'file',
describe: 'x marks the spot',
type: 'string', /* array | boolean | string */
nargs: 1,
demand: true,
demand: 'file is required',
default: '/etc/passwd'
// also: count:true, requiresArg:true
})
.options({
f: { ... }
})
```
2017-05-20 15:15:44 +00:00
### Examples and more help stuff
2015-04-06 10:37:34 +00:00
```js
// more help
.example('...')
.epilog('copyright 2015')
2015-05-29 18:11:28 +00:00
.command('start', 'start a server')
```
2015-04-06 10:37:34 +00:00
### Stacking
```js
.count('verbose')
argv.verbose // -vvv => 3
```
### Reject non explicits
```js
.strict()
```
### Methods
```
yargs.showHelp()
yargs.help() //=>string
```