官术网_书友最值得收藏!

Fetching command

The last thing I want to discuss before we wrap up this section is—how we fetch command.

As we know, command is available in the _ property as the first and only item. This means that in the app.js, var command statement, we can set command equal to argv, then ._, and then we'll use [] to grab the first item in the array, as shown in the following code:

console.log('Starting app.js');

const fs = require('fs');
const _ = require('lodash');
const yargs = require('yargs');

const notes = require('./notes.js');

const argv = yargs.argv;
var command = argv._[0];
console.log('Command:', command);
console.log('Yargs', argv);

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');
}

With this in place, we now have the same functionality, but we'll use yargs everywhere. If I rerun the last command, we can test that the functionality still works. And it does! As shown in the following command output, we can see that Command: remove shows up:

Next, we'll look into filling out the individual functions. We'll take a look first at how we can use JSON to store our notes inside our file system.

主站蜘蛛池模板: 桃源县| 即墨市| 九龙城区| 湾仔区| 迭部县| 西华县| 苍梧县| 紫阳县| 巴南区| 平度市| 阿拉尔市| 天全县| 武隆县| 桓台县| 桃园县| 桓台县| 山丹县| 高青县| 雅江县| 吕梁市| 石家庄市| 平昌县| 苗栗市| 阿鲁科尔沁旗| 西乌珠穆沁旗| 珠海市| 西和县| 鹿泉市| 将乐县| 莱阳市| 吉首市| 桂东县| 洛隆县| 云南省| 金秀| 宜州市| 达日县| 西安市| 柞水县| 云安县| 河池市|