3.7 Simplified Input with Yargs

We are going to use a third party module called Yargs from http://yargs.js.org.

Yargs is a npm module that is professional at dealing with arguments, in particular, those passed in from terminal.

Install Yargs

npm install yargs --save

We could specify the version number by

npm install yargs @4.7.1 --save

--save tells npm to enlist yargs in package.json file.

Use Module

const yargs = require('yargs');
const argv = yargs.argv;

Compare Between yargs.argv and process.argv

const argv = yargs.argv;
console.log('Process', process.argv);
console.log('Yargs', argv);

Normal Argument Passing

jude@jude-ThinkPad-S5:~/Desktop/n-3-6-getting-input$ node app.js remove
Starting app.js
Starting notes.js
Command: remove
Process [ '/usr/bin/node',
'/home/jude/Desktop/n-3-6-getting-input/app.js',
'remove' ]
Yargs { _: [ 'remove' ], '$0': 'app.js' }
Removing note

Argument Passing with Key-value Pair

Here is where Yargs comes to its real strength!

jude@jude-ThinkPad-S5:~/Desktop/n-3-6-getting-input$ node app.js add --title="haha" --body="ok"
Starting app.js
Starting notes.js
Command: add
Process [ '/usr/bin/node',
'/home/jude/Desktop/n-3-6-getting-input/app.js',
'add',
'--title=haha',
'--body=ok' ]
Yargs { _: [ 'add' ], title: 'haha', body: 'ok', '$0': 'app.js' }
Adding new note

We saw that our argument was parsed to an argument project. This is really useful later when we need the fields title and body

Use Yargs Key-Value Pair Argument

We update nodes.js to

console.log('Starting notes.js');
var addNote = (title, body) => {
console.log('adding note', title, body);
};
module.exports = {
addNote // same as addNote: addNote
}

We update the add branch in app.js to

if (command === 'add') {
notes.addNote(argv.title, argv.body);

If we run our script with add --title="abc" --body="this is a body", we would be able to see the following

jude@jude-ThinkPad-S5:~/Desktop/n-3-6-getting-input$ node app.js add --title="abc" --body="this is a body"
Starting app.js
Starting notes.js
Command: add
Process [ '/usr/bin/node',
'/home/jude/Desktop/n-3-6-getting-input/app.js',
'add',
'--title=abc',
'--body=this is a body' ]
Yargs { _: [ 'add' ],
title: 'abc',
body: 'this is a body',
'$0': 'app.js' }
adding note abc this is a body

If the above truly happened, you did it correctly!

Updating Everything

Updates in notes.js

console.log('Starting notes.js');
var addNote = (title, body) => {
console.log('adding note', title, body);
};
var getAll = () => {
console.log('getting all notes');
}
var getNote = (title) => {
console.log('getting note', title);
}
var removeNote = (title) => {
console.log('removing note', title);
}
module.exports = {
addNote,
getAll,
getNote,
removeNote
}

Updates in app.js

if (command === 'add') {
notes.addNote(argv.title, argv.body);
} else if (command === 'list') {
notes.getAll();
} else if (command === 'read') {
notes.getNote(argv.title);
} else if (command === 'remove') {
notes.removeNote(argv.title);
} else {
console.log('Command not recognized');
}

Update in var command

var command = argv._[0];

Now command has exactly the same functionality as Yargs.