NodeJS - yargs utility for command line help


About

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.

Example:

ScreenShot

Installation
npm install yargs --save

Usage

Importing yargs
const yargs = require('yargs');

Example terminal commands, to accept, are as follows, for a terminal-based-notes application.

$node app.js list # which will list all the notes
$node app.js read --title='My Secret Note'
$node app.js add -t="Note Title" -b="Note body section"
$node app.js add --title="My Personal Note title" --body="Custom note body"
Setting up yargs
const titleOptions = {
  describe: 'Title of note',
  demand: true,
  alias: 't'
};
const bodyOptions = {
  describe: 'Body of note',
  demand: true,
  alias: 'b'
};
const argv = yargs
  .command('add', 'Add a new note', {
    title: titleOptions,
    body: bodyOptions
  })
  .command('list', 'List all notes')
  .command('read', 'Read a note', {
    title: titleOptions,
  })
  .command('remove', 'Remove a note', {
    title: titleOptions
  })
  .help()
  .argv;
var command = argv._[0];
Using yargs variable
if (command === 'add') {
  // do something to add a note
  // argv.title 
  // argv.body
} else if (command === 'list') {
  // do something to list all notes
} else if (command === 'read') {
  // do something to read a specific note
  // argv.title = requested note title
} else if (command === 'remove') {
  // do something to remove a specific note
  // argv.title = requested note title
}
Another Example.

If you want user to provide address, you can configure yargs as follows.

const argv = yargs
    .options({
        a: { // name of parameter
            demand: true, // requires true
            alias: 'address', // alias - another name
            describe: 'Address to fetch weather for', // description of command. It will be displayed in help
            string: true // is it string? 
        }
    })
    .help() // generates help
    .alias('help', 'h') // alias of help command
    .argv; // argument values

Need more examples? Click here.