Node App: Note命令行应用程序

"该博客介绍了如何使用npm库yargs来解析命令行参数,实现一个简单的笔记应用。应用包括添加、删除、列出和读取笔记功能,并通过chalk库输出不同样式的提示信息。用户可以通过命令行交互,如`node app.js add --title="title1" --body="body1"`来添加笔记。程序从notes.json文件中读取和保存笔记数据。"
摘要由CSDN通过智能技术生成

此程序需安装npm 第三方库yargs 解析命令行参数,chalk 输出特定样式的文本。安装版本如下:
“chalk”: “^4.1.1”,
“yargs”: “^17.0.1”

Note 应用程序支持 4个命令:

  1. add 添加一个note,两个要求的option: title 和 body
  2. remove 删除一个note, 一个要求的option: title
  3. list 列举所有note,没有option
  4. read读取一个note,必要option: title

主要有两个文件:
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值