yargs

yargs the modern, pirate-themed successor to optimist.

README

Yargs

Yargs be a node.js library fer hearties tryin' ter parse optstrings


ci [![NPM version][npm-image]][npm-url] [![js-standard-style][standard-image]][standard-url] [![Coverage][coverage-image]][coverage-url] [![Conventional Commits][conventional-commits-image]][conventional-commits-url] [![Slack][slack-image]][slack-url]

Description

Yargs helps you build interactive command line tools, by parsing arguments and generating an elegant user interface.

It gives you:

commands and (grouped) options (my-program.js serve --port=5000).
a dynamically generated help menu based on your arguments:

  1. ```
  2. mocha [spec..]

  3. Run tests with Mocha

  4. Commands
  5.   mocha inspect [spec..]  Run tests with Mocha                         [default]
  6.   mocha init <path>       create a client-side Mocha setup at <path>

  7. Rules & Behavior
  8.   --allow-uncaught           Allow uncaught errors to propagate        [boolean]
  9.   --async-only, -A           Require all tests to use a callback (async) or
  10.                              return a Promise                          [boolean]
  11. ```

bash-completion shortcuts for commands and options.

Installation


Stable version:
  1. ``` sh
  2. npm i yargs
  3. ```

Bleeding edge version with the most recent features:
  1. ``` sh
  2. npm i yargs@next
  3. ```

Usage


Simple Example


  1. ``` js
  2. #!/usr/bin/env node
  3. const yargs = require('yargs/yargs')
  4. const { hideBin } = require('yargs/helpers')
  5. const argv = yargs(hideBin(process.argv)).argv

  6. if (argv.ships > 3 && argv.distance < 53.5) {
  7.   console.log('Plunder more riffiwobbles!')
  8. } else {
  9.   console.log('Retreat from the xupptumblers!')
  10. }
  11. ```

  1. ``` sh
  2. $ ./plunder.js --ships=4 --distance=22
  3. Plunder more riffiwobbles!

  4. $ ./plunder.js --ships 12 --distance 98.7
  5. Retreat from the xupptumblers!
  6. ```

Note: hideBin is a shorthand for [process.argv.slice(2)](https://nodejs.org/en/knowledge/command-line/how-to-parse-command-line-arguments/). It has the benefit that it takes into account variations in some environments, e.g., Electron.


Complex Example


  1. ``` js
  2. #!/usr/bin/env node
  3. const yargs = require('yargs/yargs')
  4. const { hideBin } = require('yargs/helpers')

  5. yargs(hideBin(process.argv))
  6.   .command('serve [port]', 'start the server', (yargs) => {
  7.     return yargs
  8.       .positional('port', {
  9.         describe: 'port to bind on',
  10.         default: 5000
  11.       })
  12.   }, (argv) => {
  13.     if (argv.verbose) console.info(`start server on :${argv.port}`)
  14.     serve(argv.port)
  15.   })
  16.   .option('verbose', {
  17.     alias: 'v',
  18.     type: 'boolean',
  19.     description: 'Run with verbose logging'
  20.   })
  21.   .parse()
  22. ```

Run the example above with --help to see the help for the application.

Supported Platforms


TypeScript


yargs has type definitions at [@types/yargs][type-definitions].

  1. ```
  2. npm i @types/yargs --save-dev
  3. ```

See usage examples in docs.

Deno


As of v16, yargs supports Deno:

  1. ```typescript
  2. import yargs from 'https://deno.land/x/yargs/deno.ts'
  3. import { Arguments } from 'https://deno.land/x/yargs/deno-types.ts'

  4. yargs(Deno.args)
  5.   .command('download <files...>', 'download a list of files', (yargs: any) => {
  6.     return yargs.positional('files', {
  7.       describe: 'a list of files to do something with'
  8.     })
  9.   }, (argv: Arguments) => {
  10.     console.info(argv)
  11.   })
  12.   .strictCommands()
  13.   .demandCommand(1)
  14.   .parse()
  15. ```

ESM


As of v16,yargs supports ESM imports:

  1. ``` js
  2. import yargs from 'yargs'
  3. import { hideBin } from 'yargs/helpers'

  4. yargs(hideBin(process.argv))
  5.   .command('curl <url>', 'fetch the contents of the URL', () => {}, (argv) => {
  6.     console.info(argv)
  7.   })
  8.   .demandCommand(1)
  9.   .parse()
  10. ```

Usage in Browser


See examples of using yargs in the browser in docs.

Community


Having problems? want to contribute? join our community slack.

Documentation


Table of Contents


  Arrays
  Quotes

Supported Node.js Versions


Libraries in this ecosystem make a best effort to track
post on why we think this is important](https://medium.com/the-node-js-collection/maintainers-should-consider-following-node-js-release-schedule-ab08ed4de71a).

[npm-url]: https://www.npmjs.com/package/yargs
[npm-image]: https://img.shields.io/npm/v/yargs.svg
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[standard-url]: http://standardjs.com/
[conventional-commits-image]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg
[conventional-commits-url]: https://conventionalcommits.org/
[slack-image]: http://devtoolscommunity.herokuapp.com/badge.svg
[slack-url]: http://devtoolscommunity.herokuapp.com
[type-definitions]: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/yargs
[coverage-image]: https://img.shields.io/nycrc/yargs/yargs
[coverage-url]: https://github.com/yargs/yargs/blob/main/.nycrc