16.1.0 • Published 7 days ago

yargs v16.1.0

Weekly downloads
49,375,412
License
MIT
Repository
github
Last release
7 days ago

ci NPM version js-standard-style Coverage Conventional Commits Slack

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:
mocha [spec..]

Run tests with Mocha

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

Rules & Behavior
  --allow-uncaught           Allow uncaught errors to propagate        [boolean]
  --async-only, -A           Require all tests to use a callback (async) or
                             return a Promise                          [boolean]
  • bash-completion shortcuts for commands and options.
  • and tons more.

Installation

Stable version:

npm i yargs

Bleeding edge version with the most recent features:

npm i yargs@next

Usage

Simple Example

#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')
const argv = yargs(hideBin(process.argv)).argv

if (argv.ships > 3 && argv.distance < 53.5) {
  console.log('Plunder more riffiwobbles!')
} else {
  console.log('Retreat from the xupptumblers!')
}
$ ./plunder.js --ships=4 --distance=22
Plunder more riffiwobbles!

$ ./plunder.js --ships 12 --distance 98.7
Retreat from the xupptumblers!

Complex Example

#!/usr/bin/env node
const yargs = require('yargs/yargs')
const { hideBin } = require('yargs/helpers')

yargs(hideBin(process.argv))
  .command('serve [port]', 'start the server', (yargs) => {
    yargs
      .positional('port', {
        describe: 'port to bind on',
        default: 5000
      })
  }, (argv) => {
    if (argv.verbose) console.info(`start server on :${argv.port}`)
    serve(argv.port)
  })
  .option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging'
  })
  .argv

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

Supported Platforms

TypeScript

yargs has type definitions at @types/yargs.

npm i @types/yargs --save-dev

See usage examples in docs.

Deno

As of v16, yargs supports Deno:

import yargs from 'https://deno.land/x/yargs/deno.ts'
import { Arguments, YargsType } from 'https://deno.land/x/yargs/types.ts'

yargs(Deno.args)
  .command('download <files...>', 'download a list of files', (yargs: YargsType) => {
    return yargs.positional('files', {
      describe: 'a list of files to do something with'
    })
  }, (argv: Arguments) => {
    console.info(argv)
  })
  .strictCommands()
  .demandCommand(1)
  .argv

ESM

As of v16,yargs supports ESM imports:

import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'

yargs(hideBin(process.argv))
  .command('curl <url>', 'fetch the contents of the URL', () => {}, (argv) => {
    console.info(argv)
  })
  .demandCommand(1)
  .argv

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

Supported Node.js Versions

Libraries in this ecosystem make a best effort to track Node.js' release schedule. Here's a post on why we think this is important.

jest-clijest-runtimewebpack-dev-servermochasass-graph@grpc/proto-loaderkarmagulp-clinycprotractor@angular/compiler-clisaucelabscli-highlightconcurrently@bkit/tspackudt-labelme-converterpsc-panepronorm@beautywe/cliluxafor-for-slackonline-mantra-node-cli-exprefactoring@nomis51/vue-tools@nomercy235/weather-app@nonpolynomial/cuid-cli@nonpolynomial/lambda-server@pisa-research/utils@boris47/electron-webpackwelsee-tool@iharob/graphql-mesh-cli@kaishuu0123/oas-resolver@ymatuhin/uranium@kaishuu0123/swagger2openapi@vchangal/devkitarno-front-clireact-native-bluetooth2killi8n-react-native-fast-imagemodularize-componentsb-filmothequesb-collection-filesqldumpnodewebx-buildeditorconfig-lint-jsgenlicenselicensar@augustojosedev/univerteocw-to-hugonest-aws-serverless-toolswsx-cli@leading-works/create-trans-unit@nia3208/foreversuremdm@httptoolkit/win-detect-browsersgrav-plugin-webpackergulpproject-v1transcendhetatestkoa-templateynab-file-importercovid-by-countrywelsee-cliwebpack-modify-nginx-plugin-test@aeppic/install-image-servernode-red-contrib-auth-motebus@open-template-hub/open-template-hub-server-generatorschriftlage-cliplaytwo-core-cms-sectionsmongo-backup-minionodejs-intership-wh1tevsz3r-runner@tradeling/gen-sdk@corratech/pwa-buildpack@yelloan/redoc-clietherless-cli@enlibe/clipuppeteer-clientoapi2ts3asenkronvehttprequestgraphql-schema-processgraphql-monsterelectron-zero@binaryshrub/arbor-cli@byte-ferry/cli@kroonprins/pipectl-coreflowmms@yeeti/automatypeorm-model-generator-relations-republishjuit-api-schemaspring-generatorxen-hostctl-builder@tuxmachine/html2json@lucat1/linaria@puzzleio-devops/env-file-generator@puzzleio-devops/env-token-replacer@canvanauts/check-a-block@bigpopakap/architectsongrunyu112@w00zla/npm-recursersm-flex-nssm-flex-ns-00
16.1.0

7 days ago

16.0.2

1 month ago

16.0.1

1 month ago

16.0.3

1 month ago

16.0.0

1 month ago

16.0.0-beta.3

1 month ago

16.0.0-beta.2

1 month ago

16.0.0-beta.1

2 months ago

16.0.0-beta.0

2 months ago

16.0.0-alpha.3

2 months ago

16.0.0-alpha.0

2 months ago

16.0.0-alpha.1

2 months ago

16.0.0-alpha.2

2 months ago

15.4.1

3 months ago

15.4.0

4 months ago

15.4.0-beta.1

4 months ago

7.1.1

5 months ago

15.4.0-beta.0

5 months ago

15.3.2-beta.0

6 months ago

15.3.1

7 months ago

13.3.2

7 months ago

14.2.3

7 months ago

15.3.1-beta.0

7 months ago

15.3.0

8 months ago

15.3.0-beta.1

8 months ago

15.3.0-beta.0

8 months ago

15.2.0-beta.2

8 months ago

15.2.0

8 months ago

15.2.0-beta.1

8 months ago

15.2.0-beta.0

8 months ago

15.1.0

10 months ago

14.2.2

11 months ago

15.0.2

11 months ago

15.0.1

11 months ago

15.0.0

12 months ago

14.2.1

12 months ago

11.1.1

1 year ago

14.2.0

1 year ago

14.1.0

1 year ago

14.0.0

1 year ago

13.3.0

1 year ago

13.2.4

1 year ago

13.2.2

2 years ago

13.2.1

2 years ago

13.2.0

2 years ago

13.1.0

2 years ago

12.0.5

2 years ago

12.0.4

2 years ago

12.0.2

2 years ago

12.0.1

2 years ago

12.0.0

2 years ago

11.1.0

3 years ago

11.0.0

3 years ago

10.1.2

3 years ago

10.1.1

3 years ago

10.1.0

3 years ago

10.0.3

3 years ago

10.0.2

3 years ago

10.0.1

3 years ago

10.0.0

3 years ago

10.0.0-alpha.4

3 years ago

10.0.0-alpha.3

3 years ago

10.0.0-alpha.2

3 years ago

10.0.0-alpha.1

3 years ago

10.0.0-alpha.0

3 years ago

9.0.1

3 years ago

9.0.0

3 years ago

8.0.2

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

7.1.0

4 years ago

7.0.2

4 years ago

7.0.1

4 years ago

7.0.0

4 years ago

7.0.0-alpha.3

4 years ago

7.0.0-alpha.2

4 years ago

7.0.0-alpha.1

4 years ago

6.6.0

4 years ago

6.5.0

4 years ago

6.4.0

4 years ago

6.3.0

4 years ago

6.3.0-candidate

4 years ago

6.2.0

4 years ago

6.2.0-candidate

4 years ago

6.1.1

4 years ago

6.1.1-candidate

4 years ago

6.1.0-candidate

4 years ago

6.0.0

4 years ago

6.0.0-alpha.1

4 years ago

5.0.0

4 years ago

5.0.0-candidate

4 years ago

4.8.1

4 years ago

4.8.1-candidate

4 years ago

4.8.0

4 years ago

4.8.0-candidate3

4 years ago

4.8.0-candidate2

4 years ago

4.8.0-candidate

4 years ago

4.7.1

4 years ago

4.7.0

4 years ago

4.7.0-pre

4 years ago

4.6.0

5 years ago

4.6.0-candidate

5 years ago

4.5.0

5 years ago

4.4.0

5 years ago

4.3.2

5 years ago

4.3.1

5 years ago

4.3.0

5 years ago

4.2.0

5 years ago

4.1.0

5 years ago

4.0.0

5 years ago

4.0.0-alpha5

5 years ago

4.0.0-alpha4

5 years ago

4.0.0-alpha3

5 years ago

4.0.0-alpha2

5 years ago

4.0.0-alpha1

5 years ago

3.32.0

5 years ago

3.31.0

5 years ago

3.30.0

5 years ago

3.29.0

5 years ago

3.28.0

5 years ago

3.27.0

5 years ago

3.26.0

5 years ago

3.25.0

5 years ago

3.24.0

5 years ago

3.23.0

5 years ago

3.22.2

5 years ago

3.22.1

5 years ago

3.22.0

5 years ago

3.21.1

5 years ago

3.21.0

5 years ago

3.20.0

5 years ago

3.19.0

5 years ago

3.18.1

5 years ago

3.18.0

5 years ago

3.17.1

5 years ago

3.16.1

5 years ago

3.16.0

5 years ago

3.15.0

5 years ago

3.14.0

5 years ago

3.13.0

5 years ago

3.12.0

5 years ago

3.11.0

5 years ago

3.10.0

5 years ago

3.9.1

5 years ago

3.9.0

5 years ago

3.8.0

6 years ago

3.7.2

6 years ago

3.7.1

6 years ago

3.7.0

6 years ago

3.6.0

6 years ago

3.5.4

6 years ago

3.5.3

6 years ago

3.5.2

6 years ago

3.5.1

6 years ago

3.5.0

6 years ago

3.4.5

6 years ago

3.4.4

6 years ago

3.4.0

6 years ago

3.3.1

6 years ago

3.2.1

6 years ago

3.1.0

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.3.0

6 years ago

2.1.1

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.6

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.15

7 years ago

1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago