How to use the options function from yargs

Find comprehensive JavaScript yargs.options code examples handpicked from public code repositorys.

yargs.options is a method used in the yargs library that sets up configuration options for command line arguments.

15
16
17
18
19
20
21
22
23
24
.usage("Bentley Scripts Utility\n\n These scripts consist of several standard npm tasks an app may want to make use of.")
.command("test", false, {}, () => { testCommand() })
.command("test-tsnode", false, {}, () => { testCommand() })
.command("docs", "Generate TypeDoc documentation by using the provided parameters to pass to TypeDoc.  Supports generating html TypeScript documentation as well as a json representation of the documentation.",
  function (yargs) {
    return yargs.options({
      "source": {
        describe: "Specify the TypeScript source directory"
      },
      "out": {
fork icon193
star icon513
watch icon0

+ 19 other calls in file

169
170
171
172
173
174
175
176
177
178
        .epilog(serveExamples + info);
})
.command('template <cmd>', 'Execute template related commands.', yargs => {
    yargs
        .command('init [path]', 'Initialize a new Docma template module.', () => {
            // yargs.options();
        })
        .command('doctor [path]', 'Diagnose a Docma template.', yargs => {
            yargs
                .options({
fork icon40
star icon317
watch icon6

How does yargs.options work?

yargs.options() is a method that allows you to define the options that your command-line application can accept, specifying their names, types, default values, descriptions, and other properties. When you run your application, these options can be passed in as command-line arguments, and yargs will parse and validate them for you.

3
4
5
6
7
8
9
10
11
12
const yargs = require('yargs');

const { updateAll } = require('./lib/updateDeps');

async function main() {
    const argv = yargs.options({
        transitive: { type: 'boolean' },
    }).argv;

    await updateAll(!!argv.transitive, [
fork icon18
star icon283
watch icon5

+ 7 other calls in file

2
3
4
5
6
7
8
9
10
11
const yargs = require('yargs')

module.exports = () => {

  yargs.usage('Usage: $0 <command> [options]')
  yargs.options({
    'url': {
      alias: 'u',
      nargs: 1,
      string: true,
fork icon13
star icon30
watch icon4

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const yargs = require("yargs");

// Define options for the command
const options = {
  name: {
    describe: "Name of the user",
    demandOption: true,
    type: "string",
  },
  age: {
    describe: "Age of the user",
    demandOption: true,
    type: "number",
  },
  email: {
    describe: "Email address of the user",
    demandOption: false,
    type: "string",
  },
};

// Register the options with yargs
yargs.options(options);

// Parse command-line arguments
const argv = yargs.argv;

// Access the values passed in via the command line
const { name, age, email } = argv;

// Use the values as needed
console.log(
  `User ${name} is ${age} years old and their email is ${email || "unknown"}`
);

In this example, yargs.options is used to define the options for a command-line utility. The options are specified as an object where the keys are the option names and the values are objects that define the option properties such as describe, demandOption, and type. The yargs.options method is then used to register the options with yargs. Once the options are registered, they can be accessed via the argv object returned by yargs.argv.

48
49
50
51
52
53
54
55
56
57
    file: '../src/data/moonriver/solarbeamLpPools.json',
    masterchef: solarbeam.masterchef,
  },
};

const args = yargs.options({
  network: {
    type: 'string',
    demandOption: true,
    describe: 'blockchain network',
fork icon191
star icon0
watch icon0

35
36
37
38
39
40
41
42
43
44

chaincodeServerCommand.builder(yargs);

expect(yargs.options.calledOnce).to.be.true;

const args = yargs.options.getCall(0).args[0];

expect(args['chaincode-address'].required).to.be.true;
expect(args['chaincode-id'].required).to.be.true;
expect(args['grpc.max_send_message_length'].default).to.deep.equal(-1);
fork icon139
star icon215
watch icon16

+ 14 other calls in file

160
161
162
163
164
165
166
167
168
169
showHelp(level = 'log') {
  this.yargs.showHelp(level);
}

/**
 * shortcut for yargs.options
 * @param  {Object} opt - an object set to `yargs.options`
 */
set options(opt) {
  this.yargs.options(opt);
fork icon23
star icon189
watch icon17

36
37
38
39
40
41
42
43
44
45
  describe: 'Skip the automatic unpublish process',
  alias: 'u',
  type: 'boolean',
  default: false,
});
yargs.options('skip-unpublish-regex', {
  describe: 'Skip the unpublish process for specific regex',
  type: 'string',
});
yargs.options('skip-purge', {
fork icon0
star icon2
watch icon3

+ 3 other calls in file

Other functions in yargs

Sorted by popularity

function icon

yargs.argv is the most popular function in yargs (1012 examples)