Node App: Note命令行应用程序

2023-05-16

此程序需安装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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Node App: Note命令行应用程序 的相关文章

  • c++ GUI轻量工具包FLTK介绍 (1)

    c 43 43 有许多gui开发工具 xff0c 比如MFC xff0c QT xff0c 而FLTK Fast Light ToolKit 读音fulltik xff0c 则是一个轻量级的 xff0c 简洁的gui开发库 fltk是跨平台
  • 一道题谈回溯

    CSDN话题挑战赛第1期 活动详情地址 xff1a https marketing csdn net p bb5081d88a77db8d6ef45bb7b6ef3d7f 参赛话题 xff1a Leetcode刷题指南 话题描述 xff1a
  • 戴尔服务器安装Debian11过程

    目录 物理戴尔服务器Debian11安装过程 以下皆为作者实操 转载注明出处 制作Debian ISO 镜像 U盘启动服务器进入启动项设置注意 在开机之前 请一定拔掉服务器网线 否则Debian安装会卡在 39 安装软件 39 这一步安装过
  • (转)Linux文件系统只读Read-only file system的快速解决方法

    xff08 原创地址 xff09 http www ha97 com 5428 html 问题描述 xff1a 上周公司的私有云 xff08 底层架构是Openstack 43 KVM xff0c 目前稳定性还不够好 xff0c 开发团队在
  • C语言中头文件写法

    在实现C语言模块化编程时 xff0c 通常会用到 h式的头文件的编写 xff0c 在此记录下头文件的基本写法 通常我们写C程序时 xff0c 都会直接使用这样的语句 include lt stdlib h gt 这便是我们通常说的头文件 头
  • 测试人员应该知道的Redis知识(六) Set

    一 概述 Redis 的 Set 是 String 类型的无序集合 集合成员是唯一的 xff0c 这就意味着集合中不能出现重复的数据 Redis 中集合是通过哈希表实现的 xff0c 所以添加 xff0c 删除 xff0c 查找的复杂度都是
  • OpenPCDet初级教程【自定义模型、loss】

    最近在研究点云物体检测 xff0c 基于OpenPCDet框架进行算法开发可以节约大量的重复性工作 xff0c 专心集中在核心算法的设计上 xff0c 大量节约时间 同时 xff0c 因为框架由大公司专业团队进行维护 xff0c 代码质量稳
  • NFS

    NFS Server apt install nfs kernel server nfs common y vim etc exports etc exports the access control list for filesystem
  • (深入理解计算机系统) bss段,data段、text段、堆(heap)和栈(stack)

    文章目录 bssdatatextheapstack总结例子 bss bss段 xff08 bss segment xff09 通常是指用来存放程序中未初始化的全局变量的一块内存区域 bss是英文Block Started by Symbol
  • linux学习43-HTTP服务和APACHE2

    HTTP服务和APACHE2 知识点 请求报文响应报文错误码请求重定向编译安装实现httpscurl工具 1 http协议 http协议版本 http 0 9 http 1 0 http 1 1 xff08 较多 xff09 http 2
  • Ubuntu20.4安装QT6

    前言 xff1a 本教程基于Ubuntu20 4 xff0c 在Ubuntu22 4上也测试过 Ubuntu18 04由于GCC版本太低 xff0c 无法正常工作 1 下载QT安装程序 xff1a Open Source Developme
  • sublime安装教程并配置C++环境

    sublime安装教程并配置C 43 43 环境 sublime安装教程并配置C 43 43 环境前言下载sublime配置C 43 43 环境 sublime安装教程并配置C 43 43 环境 前言 最近将电脑重新安装 xff0c 配置s
  • PHP call_user_func_array回调函数 call_user_func_array函数详解

    call user func array PHP官方 call user func array讲解 call user func array PHP 4 gt 61 4 0 4 PHP 5 PHP 7 call user func arra
  • MicroPython 链接WiFi ESP32连WiFi

    这里 gt gt gt gt MicroPython 教程写的非常好强烈推荐 import network import socket import time SSID 61 34 abc 34 修改为你的WiFi名称 PASSWORD 6
  • ESP32 arduino 天气显示 后台可控制 定时消息提示 图片提示

    ESP32 arduino 天气显示 后台可控制 定时消息提示 图片提示 后台操作界面 添加图片显示 添加文字轮播 用到的 H库 include lt WiFi h gt include lt ESPmDNS h gt include lt
  • python subprocess子进程

    import subprocess cmd lx 61 subprocess Popen cmd rtmp encoding 61 34 utf 8 34 shell 61 True sg SystemTray notify 39 开播成功
  • python 获取可用视频列表 和麦克风列表

    import re import subprocess cmd 61 39 ffmpeg 39 39 list devices 39 39 true 39 39 f 39 39 dshow 39 39 i 39 39 dummy 39 de
  • python ffmpeg直播客户端

    import PySimpleGUI as sg import sys import json import os import requests import subprocess import pygame camera import
  • 动物类的继承

    动物类的继承 要求 xff1a 1 在一个名为Test java的文件中定义四个类 xff1a 动物类Animal xff0c 狗类Dog和猫类Cat继承Animal xff0c 测试类Test xff0c 要求编写代码的同时编写简单注释
  • ffmpeg命令操作 合并视频 取图片帧数 获取音频

    ffmpeg安装 点击这里跳转 官方 wins安装的话要添加 环境变量转载点击这里 ffmpeg命令操作 合并视频 取图片帧数 1 获取视频内的图片 ffmpeg i input mp4 r 15 q v 2 f image2 img 04

随机推荐

  • JS 使用 lz-string存储 数据压缩

    浏览器localStorage存储为 5M 然而并不能满足我们的要求 我们可以压缩的是您可以存储的更多数据 好在JS 有lz string 库 引入库 lt script src 61 34 https cdn bootcss com lz
  • USB开发者模式 安卓 adb操作 + 安装

    USB开发者模式 安卓 adb操作 43 安装 这是adb 安装 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 不是gdb 安装 adb 下载
  • python控制 鼠标移动 pyautogui || PyMouse 自动化

    python控制 鼠标移动 pyautogui PyMouse 自动化 方法1 pyautogui 安装 pip install pyautogui文档基本操作指令 gui PAUSE 61 0 5 每次函数调用后暂停0 5秒 gui FA
  • PHP 图片去除水印 去除logo

    使用插件 ffmpeg 点这里 不使用插件 也可以 lt php function CLEAR ICO filename savename Clear W Clear H Clear X Clear Y filename 61 读取图片名
  • Apache Options指令详解

    Options指令是Apache配置文件中一个比较常见也比较重要的指令 xff0c Options指令可以在Apache服务器核心配置 server config 虚拟主机 配置 virtual host 特定目录配置 directory
  • 基于VC开发epoll/linux 程序指南

    1 概述 3 2 背景 3 3 总体思路 3 4 功能特点 4 5 开发工具和操作系统要求 4 5 1 Windows VC 4 5 2 linux g 43 43 4 6 开发人员要求 5 7 Linux针对epoll移植 5 8 VC工
  • Qt Creator闪退解决办法

    Qt Creator闪退解决办法 Qt 今天突然出现所有插件无法打开的问题 xff0c 找了很久的资料也没解决 只能重装QtCreator 结果一点开QtCreator过不了一分钟 xff0c 就闪退 看到windows记录的问题原因是Qt
  • QScrollArea qss样式设置失效问题

    QScrollArea通过样式改变背景色 遇到设置QScrollArea qss无法生效问题 xff0c 网上没找到合适解决办法 xff0c 查看QScrollArea qt4源码后发现 xff0c 增加如下代码之后 xff0c qss正常
  • Android下打印调试堆栈方法

    打印堆栈是调试的常用方法 xff0c 一般在系统异常时 xff0c 我们可以将异常情况下的堆栈打印出来 xff0c 这样十分方便错误查找 实际上还有另外一个非常有用的功能 xff1a 分析代码的行为 android代码太过庞大复杂了 xff
  • 家里的垃圾移动光猫和tp路由器设置

    第一步 xff1a 将TP初始化 第二步 xff1a 将网线一端插在tpwan口 xff0c 就是与众不同的 第三步 xff1a 将TP的LAN口IP改为192 168 2 1 第四步 xff1a 将TP的WAN口设置为 xff1a 这里先
  • 蓝桥杯单片机闪烁灯控制逻辑分析

    以前的闪烁灯上来就是一片代码 xff0c 总感觉不是很靠谱 xff0c 现在决定写一个比较细致的逻辑分析 首先 xff0c 我们先来贴上开发板的电路图 我们首先先看一下上面的流水灯电路图 xff0c 74HC573锁存器进行流水灯的控制 x
  • idea取消vim模式

    在安装idea时选择了vim编辑模式 xff0c 但是用习惯了eclipse xff0c 总是要拷贝粘贴 xff0c 在idea中一直按ctrl 43 c和ctrl 43 v不起总用 于是想把vim模式关闭掉 方法 xff1a 菜单栏 to
  • atcoder beginner contest 142 Disjoint Set of Common Divisors(质因数分解)

    题目大意 xff1a 找出A B中的所有互质的因数 解题思路 xff1a 首先 xff0c 我们必须找出因数 我们知道对gcd a b 进行因数分解就能得到a xff0c b的所有因数 但是这里需要互质的因数 xff0c 所以我们这里需要对
  • spring管理事务控制的问题

    在使用ssm框架开发项目时 xff0c 事务控制交由spring来管理 xff0c 然而在查看日志时发现一个问题 xff1a org mybatis spring SqlSessionUtils closeSqlSession SqlSes
  • 关于 Debian 系统

    Debian是什么 xff1f Debian 计划是一个致力于创建一个自由操作系统的合作组织 我们所创建的这个操作系统名为Debian 操作系统是使计算机运行的基本程序和工具的集合 xff0c 其中最主要的部分称为内核 xff08 kern
  • btrfs的子卷与快照功能--Linux下备份系统

    关于Btrfs 什么是Btrfs 以及它的性能 可以在许多地方查阅得到 这里要提到的是 Btrfs文件系统本身就是由子卷 Subvolumes 构成的 它的top level是由B tree结构的含有目录 文件和子卷等东西构成的子卷 而在t
  • c# 通过dotPeek调试.net源码

    1 安装dotPeek 2 设置dotPeek xff0c 开启pdb服务 3 设置VS xff0c 添加服务来源 取消仅我的代码 4 测试代码 xff0c 在Console WriteLine 打断点 xff0c 并按F11进入 注意 x
  • 超棒的离线文档阅读器:Zeal

    前言 xff1a 大家写代码的时候总会有些方法或者属性不太清楚 xff0c 这时候我们就会打开浏览器 xff0c 然后找官方api或者直接搜索引擎找对应问题 xff0c 无疑花费了大量的时间 所以 xff0c 你需要一个桌面应用Zeal x
  • UITabBarController标签控制器相关设置

    1 根据下标索引以及控制器索引显示需要显示的控制器 self selectedIndex 61 sender tag 100 self selectedViewController 61 VC 2 设置标签控制器下面的文字 这里是设置系统自
  • Node App: Note命令行应用程序

    此程序需安装npm 第三方库yargs 解析命令行参数 xff0c chalk 输出特定样式的文本 安装版本如下 xff1a chalk 4 1 1 yargs 17 0 1 Note 应用程序支持 4个命令 xff1a add 添加一个n