此程序需安装npm 第三方库yargs 解析命令行参数,chalk 输出特定样式的文本。安装版本如下:
“chalk”: “^4.1.1”,
“yargs”: “^17.0.1”
Note 应用程序支持 4个命令:
主要有两个文件:
app.js
const chalk = require("chalk");
const getNotes = require("./notes.js");
const yargs = require("yargs");
const { addNote, removeNote, listNotes, readNote } = require("./notes.js");
// Create a add command
yargs.command({
command: "add",
describe: "Add a new note",
builder: {
title: {
describe: "Note title",
demandOption: true,
type: "string",
},
body: {
describe: "Note body",
demandOption: true,
type: "string",
},
},
handler(argv) {
addNote(argv.title, argv.body);
},
});
// Create a remove command
yargs.command({
command: "remove",
describe: "Remove a note",
builder: {
title: {
describe: "This is the title arg",
demandOption: true,
type: "string",
},
},
handler(argv) {
removeNote(argv.title);
},
});
// Create a list command
yargs.command({
command: "list",
describe: "List all notes",
builder: {
title: {
describe: "This is the title arg",
type: "string",
},
},
handler(argv) {
listNotes();
},
});
// Create a read command
yargs.command({
command: "read",
describe: "Read a note",
builder: {
title: {
describe: "This is the title arg",
demandOption: true,
type: "string",
},
},
handler(argv) {
readNote(argv.title);
},
});
//console.log(yargs.argv);
yargs.parse();
note.js
const fs = require("fs");
const chalk = require("chalk");
// add a note
const addNote = function (title, body) {
// load all notes from file
const notes = loadNote();
//const duplicateNotes = notes.filter((note) => note.title === title);
const duplicateNote = notes.find((note) => note.title === title);
if (duplicateNote) {
console.log(chalk.red.inverse("Title already taken!"));
} else {
const note = { title: title, body: body };
notes.push(note);
saveNote(notes);
console.log(chalk.green.inverse("New note added!"));
}
};
// remove a note
const removeNote = function (title) {
// load all notes from file
const notes = loadNote();
const notesToKeep = notes.filter((note) => note.title !== title);
if (notes.length > notesToKeep.length) {
saveNote(notesToKeep);
console.log(chalk.green.inverse("Note removed!"));
} else {
console.log(chalk.red.inverse("Note not found."));
}
};
// list notes
const listNotes = function () {
// load all notes from file
const notes = loadNote();
console.log(chalk.inverse("Your notes"));
notes.forEach((note) => console.log(note.title));
};
// read a note
const readNote = function (title) {
// load all notes from file
const notes = loadNote();
const note = notes.find((note) => note.title === title);
if (!note) {
console.log(chalk.red.inverse("Note not found"));
} else {
console.log(chalk.magentaBright(note.title), note.body);
}
};
const loadNote = function () {
try {
const dataBuffer = fs.readFileSync("notes.json");
const data = dataBuffer.toString(); //json string
return JSON.parse(data);
} catch (err) {
return [];
}
};
const saveNote = function (notes) {
const notesJSON = JSON.stringify(notes);
fs.writeFileSync("notes.json", notesJSON);
};
module.exports = {
addNote: addNote,
removeNote: removeNote,
listNotes: listNotes,
readNote: readNote,
};
运行结果:
> node app.js add --title="title1" --body="body1"
new note added!
> node app.js list
Your notes
title1
最基本的处理命令行的方法:process.argv
获取用户输入,打印出来就知道该怎么截取了。但是,复杂的命令就用yargs。