yargs
DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/yargs package

18.0.0 • Public • Published

Yargs

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


ci NPM version js-standard-style Coverage Conventional Commits

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]
  • generate completion scripts for Bash and Zsh for your command
  • 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
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
const argv = yargs(hideBin(process.argv)).parse()

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!

Note: hideBin is a shorthand for process.argv.slice(2). It has the benefit that it takes into account variations in some environments, e.g., Electron.

Complex Example

#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';

yargs(hideBin(process.argv))
  .command('serve [port]', 'start the server', (yargs) => {
    return 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'
  })
  .parse()

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@v17.7.2-deno/deno.ts'
import { Arguments } from 'https://deno.land/x/yargs@v17.7.2-deno/deno-types.ts'

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

Note: If you use version tags in url then you also have to add -deno flag on the end, like @17.7.2-deno

Usage in Browser

See examples of using yargs in the browser in docs.

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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
18.0.016,802,128latest
13.3.25,168,258latest-13
14.2.31,375,357latest-14
7.1.21,205,111latest-7
11.1.1516,623latest-11
14.2.1392next-14
18.0.0-candidate.7329next
17.7.3-candidate.0221next-17
15.5.0-candidate.066next-15
18.0.0-browser.235browser

Version History

VersionDownloads (Last 7 Days)Published
17.7.3-candidate.0221
18.0.016,802,128
18.0.0-candidate.7329
18.0.0-candidate.627
18.0.0-candidate.525
18.0.0-candidate.4286
18.0.0-candidate.313
18.0.0-browser.235
18.0.0-browser.120
18.0.0-browser.013
18.0.0-candidate.211
18.0.0-candidate.114
17.7.296,650,691
17.7.12,013,212
17.7.041,016
17.6.21,129,640
17.6.15,012
17.6.0207,068
17.5.1825,891
17.5.08,995
17.4.1107,495
17.4.062,255
17.3.1788,207
17.3.034,259
17.2.1122,196
17.2.03,115
17.1.1691,429
17.1.1-candidate.057
17.1.033,299
17.1.0-candidate.018
17.0.2-candidate.114
17.0.2-candidate16
17.0.11,895,012
17.0.01,574
17.0.0-candidate.1312
7.1.21,205,111
17.0.0-candidate.1217
17.0.0-candidate.1114
17.0.0-candidate.1016
17.0.0-candidate.914
17.0.0-candidate.817
17.0.0-candidate.7181
17.0.0-candidate.645
17.0.0-candidate.511
17.0.0-candidate.414
17.0.0-candidate.311
15.5.0-candidate.066
17.0.0-candidate.2245
17.0.0-candidate.112
17.0.0-candidate.022
16.2.033,219,569
16.1.133,126
16.1.064,506
16.0.4-candidate.023
16.0.367,810
16.0.2133
16.0.145
16.0.01,880
16.0.0-beta.316
16.0.0-beta.212
16.0.0-beta.115
16.0.0-beta.016
16.0.0-alpha.369
16.0.0-alpha.214
16.0.0-alpha.115
16.0.0-alpha.015
15.4.133,515,528
15.4.024,143
15.4.0-beta.118
7.1.1112,130
15.4.0-beta.020
15.3.2-beta.020
15.3.1658,649
13.3.25,168,258
14.2.31,375,357
15.3.1-beta.023
15.3.044,358
15.3.0-beta.117
15.3.0-beta.014
15.2.0474
15.2.0-beta.218
15.2.0-beta.117
15.2.0-beta.019
15.1.054,398
14.2.237,558
15.0.228,334
15.0.1409
15.0.0133
14.2.1392
11.1.1516,623
14.2.077,123
14.1.0475
14.0.050,589
13.3.0343,162
13.2.4284,548
13.2.298,932
13.2.12,552
13.2.02,304
13.1.076,278
13.0.0-candidate.01,196
12.0.51,441,144
12.0.41,006
12.0.3-candidate.031
12.0.2264,925
12.0.134,113
12.0.096
12.0.0-candidate.034
11.1.0250,600
11.0.0113,257
10.1.2270,766
10.1.113,317
10.1.0224
10.0.337,860
10.0.226
10.0.138
10.0.041
10.0.0-alpha.418
10.0.0-alpha.316
10.0.0-alpha.217
10.0.0-alpha.116
10.0.0-alpha.016
9.0.1266,635
9.0.02,460
8.0.2665,674
8.0.18,125
8.0.047
8.0.0-candidate.118
8.0.0-candidate.017
7.1.0470,765
7.0.28,927
7.0.2-candidate.116
7.0.1336
7.0.027
7.0.0-alpha.315
7.0.0-alpha.216
7.0.0-alpha.114
6.6.1-candidate.420
6.6.1-candidate.314
6.6.1-candidate.215
6.6.1-candidate.113
6.6.1-candidate.016
6.6.0615,592
6.6.0-candidate.046
6.5.08,104
6.4.082,205
6.3.01,117
6.3.0-candidate16
6.2.061
6.2.0-candidate15
6.1.160
6.1.1-candidate15
6.1.0-candidate14
6.0.02,954
6.0.0-alpha.117
5.0.0125,923
5.0.0-candidate15
4.8.1330,324
4.8.1-candidate64
4.8.0192
4.8.0-candidate319
4.8.0-candidate222
4.8.0-candidate19
4.7.116,442
4.7.0149
4.7.0-pre16
4.6.036,947
4.6.0-candidate22
4.5.037
4.4.01,944
4.3.2993
4.3.1246
4.3.027
4.2.0757
4.1.0881
4.0.037
4.0.0-alpha518
4.0.0-alpha418
4.0.0-alpha321
4.0.0-alpha218
4.0.0-alpha115
3.32.0548,322
3.31.03,584
3.30.01,854
3.29.038,317
3.28.020
3.27.0175,383
3.26.0183
3.25.0280
3.24.0112
3.23.0221
3.22.215
3.22.118
3.22.018
3.21.134
3.21.023
3.20.029
3.19.046
3.18.1206
3.18.029
3.17.1508
3.16.122
3.16.019
3.15.06,591
3.14.044
3.13.027
3.12.0145
3.11.0272
3.10.02,193,664
3.9.1916
3.9.056
3.8.082
3.7.21,140
3.7.121
3.7.020
3.6.0151
3.5.4249,356
3.5.319
3.5.219
3.5.120
3.5.016
3.4.530,019
3.4.418
3.4.018
3.3.122
3.2.117
3.1.024
3.0.454
3.0.319
3.0.219
3.0.119
3.0.021
2.3.012,445
2.1.144
1.3.3126,050
1.3.2338
1.3.179
1.3.017
1.2.665,940
1.2.524
1.2.421
1.2.318
1.2.248
1.2.123
1.2.018
1.1.352
1.1.216
1.1.116
1.1.020
1.0.1523
1.0.1318
1.0.1224
1.0.1125
1.0.1023
1.0.918
1.0.822
1.0.721
1.0.618
1.0.520
1.0.422
1.0.319
1.0.125
1.0.030