2017-08-14 05:01:11 +02:00
|
|
|
# Yargs
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
[![Build Status][travis-image]][travis-url]
|
|
|
|
[![Coverage Status][coveralls-image]][coveralls-url]
|
|
|
|
[![NPM version][npm-image]][npm-url]
|
|
|
|
[![Windows Tests][windows-image]][windows-url]
|
|
|
|
[![js-standard-style][standard-image]][standard-url]
|
2017-08-14 05:01:11 +02:00
|
|
|
[![Conventional Commits][conventional-commits-image]][conventional-commits-url]
|
2017-05-03 15:35:00 +02:00
|
|
|
[![Gitter][gitter-image]][gitter-url]
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
> Yargs be a node.js library fer hearties tryin' ter parse optstrings.
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
<img width="250" src="/yargs-logo.png">
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface. It gives you:
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
* commands and (grouped) options (`my-program.js serve --port=5000`).
|
|
|
|
* a dynamically generated help menu based on your arguments.
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
> <img width="400" src="/screen.png">
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
* bash-completion shortcuts for commands and options.
|
|
|
|
* and [tons more](/docs/api.md).
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
## Installation
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
```bash
|
|
|
|
npm i yargs --save
|
|
|
|
```
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
## Simple Example
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
````javascript
|
|
|
|
#!/usr/bin/env node
|
2017-08-14 05:01:11 +02:00
|
|
|
const argv = require('yargs').argv
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
if (argv.ships > 3 && argv.distance < 53.5) {
|
|
|
|
console.log('Plunder more riffiwobbles!')
|
|
|
|
} else {
|
|
|
|
console.log('Retreat from the xupptumblers!')
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
````
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
```bash
|
|
|
|
$ ./plunder.js --ships=4 --distance=22
|
|
|
|
Plunder more riffiwobbles!
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
$ ./plunder.js --ships 12 --distance 98.7
|
|
|
|
Retreat from the xupptumblers!
|
2017-05-03 15:35:00 +02:00
|
|
|
```
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
## Complex Example
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
#!/usr/bin/env node
|
2017-08-14 05:01:11 +02:00
|
|
|
const yargs = require('yargs') // eslint-disable-line
|
|
|
|
.command('serve', 'start the server', (yargs) => {
|
|
|
|
yargs.option('port', {
|
|
|
|
describe: 'port to bind on',
|
|
|
|
default: 5000
|
|
|
|
})
|
|
|
|
}, (argv) => {
|
|
|
|
if (argv.verbose) console.info(`start server on :${argv.port}`)
|
|
|
|
serve(argv.port)
|
2017-05-03 15:35:00 +02:00
|
|
|
})
|
2017-08-14 05:01:11 +02:00
|
|
|
.option('verbose', {
|
|
|
|
alias: 'v',
|
|
|
|
default: false
|
2017-05-03 15:35:00 +02:00
|
|
|
})
|
|
|
|
.help()
|
|
|
|
.argv
|
|
|
|
```
|
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
## Table of Contents
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
* [Yargs' API](/docs/api.md)
|
|
|
|
* [Examples](/docs/examples.md)
|
|
|
|
* [Parsing Tricks](/docs/tricks.md)
|
|
|
|
* [Stop the Parser](/docs/tricks.md#stop)
|
|
|
|
* [Negating Boolean Arguments](/docs/tricks.md#negate)
|
|
|
|
* [Numbers](/docs/tricks.md#numbers)
|
|
|
|
* [Arrays](/docs/tricks.md#arrays)
|
|
|
|
* [Objects](/docs/tricks.md#objects)
|
|
|
|
* [Advanced Topics](/docs/advanced.md)
|
|
|
|
* [Composing Your App Using Commands](/docs/advanced.md#commands)
|
|
|
|
* [Building Configurable CLI Apps](/docs/advanced.md#configuration)
|
|
|
|
* [Customizing Yargs' Parser](/docs/advanced.md#customizing)
|
|
|
|
* [Contributing](/contributing.md)
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
[travis-url]: https://travis-ci.org/yargs/yargs
|
|
|
|
[travis-image]: https://img.shields.io/travis/yargs/yargs/master.svg
|
|
|
|
[coveralls-url]: https://coveralls.io/github/yargs/yargs
|
|
|
|
[coveralls-image]: https://img.shields.io/coveralls/yargs/yargs.svg
|
|
|
|
[npm-url]: https://www.npmjs.com/package/yargs
|
|
|
|
[npm-image]: https://img.shields.io/npm/v/yargs.svg
|
|
|
|
[windows-url]: https://ci.appveyor.com/project/bcoe/yargs-ljwvf
|
|
|
|
[windows-image]: https://img.shields.io/appveyor/ci/bcoe/yargs-ljwvf/master.svg?label=Windows%20Tests
|
|
|
|
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
|
|
|
|
[standard-url]: http://standardjs.com/
|
2017-08-14 05:01:11 +02:00
|
|
|
[conventional-commits-image]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg
|
|
|
|
[conventional-commits-url]: https://conventionalcommits.org/
|
2017-05-03 15:35:00 +02:00
|
|
|
[gitter-image]: https://img.shields.io/gitter/room/nwjs/nw.js.svg?maxAge=2592000
|
|
|
|
[gitter-url]: https://gitter.im/yargs/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link
|