Where can i find documentation for builder in yargs npm?

Viewed 1847

Does anyone know npm yargs very well? I am using this package for command line argument thing.. BUT I cannot find explanation for "builder". Where can i get this?

yargs.command({
    command: 'test',
    describe: 'testing a note',
    builder:{
        sample:{
            describe: 'Note content',
            demandOption: true,
            type: 'string'
        },
    },
    handler(argv){
        notes.addNote(argv.title, argv.content);
    }
})
2 Answers

The builder pattern can help with giving more context with documentation help messages on using multiple commands that can be used.

We would normally use the lambda function version rather than the object version.

For an example, one application can support a number of "commands". These commands would have different bits of functionality associated with the same application.

Below is a simple get/post example that the user can do:

$ node src/index.js get
$ node src/index.js post "" 5001

The get and post after index.js is what yargs considers commands.

One way you could implement this in yargs is:

require('yargs') // eslint-disable-line
  .command('post data [port]', 'post some data', yargs => {
    yargs.positional('data', {
      describe: 'post string',
      require: true
    })
    .positional('port', {
      require: false,
      describe: 'port to bind on',
      default: 8080
    })
  })
  .command('get [port]', 'get some data', yargs => {
    yargs.positional('port', {
      require: false,
      describe: 'port to bind on',
      default: 8080
    })
  })
  .option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging'
  })
  .argv

So when requesting help information, you can do the following:

$ node src/index.js --help
index.js [command]

Commands:
  index.js serve [port]      start the server
  index.js post data [port]  post some data

Options:
      --help     Show help                                             [boolean]
      --version  Show version number                                   [boolean]
  -v, --verbose  Run with verbose logging                              [boolean]

You can then drill into the help information further:

$ node src/index.js post --help
index.js post data [port]

post some data

Positionals:
  data  post string                                                   [required]
  port  port to bind on                                          [default: 8080]

This shows the specific help for the post command.

Hence, the command builder pattern allows us to pass any number of commands to an application and provide argument documentation thereof.

It's quite useful when you have to support a number of disparate commands that need their own arguments.

So a long way round to answer the question on where to get more documentation is that if you use the lambda version, the yargs main object is passed to you and you can use all the usual yargs parameters to describe your parameters per command.

Hence, in the example above, the usage of yargs like:

yargs.positional('port', {
      require: false,
      describe: 'port to bind on',
      default: 8080
    })

is documented at: https://github.com/yargs/yargs/blob/master/docs/api.md

I found the above answer a bit complicated and to help anyone who may land on this page in the future, I would recommend reading this GeeksForGeeks article:

https://www.geeksforgeeks.org/node-js-yargs-module/

The example with .command(cmd, desc, [builder], [handler]) is this:

//Add note
yargs
  .command({
    command: "add",
    describe: "Add a note",
    builder: {
      title: {
        describe: "Title of the note",
        demandOption: true,
        type: "string",
      },
    },
    handler: (argv) => {
      console.log("Added note with title:", argv.title);
    },
  })
  .parse();

In the terminal, you have to type:

node app.js add --title="The Complete Node.js Developer Course (3rd Edition)"  
Related