yargs / yargs

yargs the modern, pirate-themed successor to optimist.
https://yargs.js.org/
MIT License
11.09k stars 992 forks source link

How to provide example for a specific command in a command module ? #1047

Open amarzavery opened 6 years ago

amarzavery commented 6 years ago

As mentioned in the example for specifying individual commands in a specific file over here.

// my-module.js
exports.command = 'get <source> [proxy]'

exports.describe = 'make a get HTTP request'

exports.builder = {
  banana: {
    default: 'cool'
  },
  batman: {
    default: 'sad'
  }
}

exports.handler = function (argv) {
  // do something with argv.
}

How can one provide an example for this specific command?

I tried to provide an example based on a sample provided for yargs over here with .example()

exports.example = ['$0 get', 'https://google.com'];

where the first item is the command and the second item in the array is the example description.

However that did not work. Any idea as to how can one provide examples for commands defined in an individual file?

ownadi commented 6 years ago

Use function form of builder

// my-module.js
exports.command = 'get <source> [proxy]'

exports.describe = 'make a get HTTP request'

exports.builder = (yargs) => {
  yargs
    .option('banana', { default: 'cool' })
    .option('batman', { default: 'sad' })
    .example('$0 get https://google.com')
}

exports.handler = function (argv) {
  // do something with argv.
}