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

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.

主站蜘蛛池模板: 平南县| 龙口市| 休宁县| 利津县| 庄河市| 嘉善县| 焦作市| 屯留县| 中牟县| 南木林县| 德兴市| 疏勒县| 察哈| 温宿县| 林甸县| 安阳县| 泾阳县| 门头沟区| 无极县| 大丰市| 柳林县| 双柏县| 沙洋县| 清丰县| 渝中区| 称多县| 县级市| 凤城市| 鄱阳县| 吴堡县| 沅陵县| 绍兴市| 河源市| 大渡口区| 娱乐| 弋阳县| 石门县| 九寨沟县| 和田市| 巴中市| 永平县|