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