<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community:   Mohamed Reda Abdelaziz </title>
    <description>The latest articles on DEV Community by   Mohamed Reda Abdelaziz  (@3azizo).</description>
    <link>https://dev.to/3azizo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1073845%2F800096c4-f5d1-4ebe-9c6d-828d41233331.jpeg</url>
      <title>DEV Community:   Mohamed Reda Abdelaziz </title>
      <link>https://dev.to/3azizo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/3azizo"/>
    <language>en</language>
    <item>
      <title>How make Commead line app using Node.js</title>
      <dc:creator>  Mohamed Reda Abdelaziz </dc:creator>
      <pubDate>Mon, 31 Jul 2023 15:43:39 +0000</pubDate>
      <link>https://dev.to/3azizo/how-make-commead-line-app-using-nodejs-15pl</link>
      <guid>https://dev.to/3azizo/how-make-commead-line-app-using-nodejs-15pl</guid>
      <description>&lt;p&gt;Creating a command-line interface app using Node.js is a great way to build utility that users can run from their terminal.&lt;/p&gt;

&lt;p&gt;below,I'll guide you through the steps to create a simple cli tool using Node.js&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;set up the project:
First, create a new directory for your cli tool and initialize a new node.js project using npm
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir my-cli-tool
cd my-cli-tool
npm init -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2.Install necessary dependencies:&lt;br&gt;
In this example, we'll use the 'yargs' package to handle command line input and 'chalk' package to add color to the output .install these dependencies:&lt;br&gt;
&lt;code&gt;npm install yargs chalk&lt;/code&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;setup yargs pakage:
import yargs and notes file we'll create. below,
add all command to handle input and use notes script to 
handel every command.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const yargs = require("yargs");
const notes = require("./notes.js");

//creat 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) {
    notes.addNote(argv.title, argv.body);
  },
});
// remove command
yargs.command({
  command: "remove",
  describe: "remove a  note",
  builder: {
    title: {
      describe: "note title",
      demandOption: true,
      type: "string",
    },
  },
  handler(argv) {
    notes.removeNote(argv.title);
  },
});
//creat a read command
yargs.command({
  command: "read",
  describe: "read a note",
  builder: {
    title: {
      command: "title",
      describe: "note title",
      demandOption: true,
      type: "string",
    },
  },
  handler(argv) {
    notes.readNote(argv.title);
  },
});

//creat a list command
yargs.command({
  command: "list",
  describe: "list your notes",
  handler() {
    notes.listNotes();
  },
});
// for yargs work
yargs.parse();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;4.notes file to handle all command:&lt;br&gt;
work with file system to storing notes and chalk&lt;br&gt;
to style output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require("fs");
const chalk = require("chalk");

const saveNotes = (notes) =&amp;gt; {
  let notesJSON = JSON.stringify(notes);
  fs.writeFileSync("notes.json", notesJSON);
};

const loadNotes = () =&amp;gt; {
  try {
    return JSON.parse(fs.readFileSync("notes.json").toString());
  } catch (e) {
    return [];
  }
};

let notes = loadNotes();

const addNote = (title, body) =&amp;gt; {
  let note = {
    title: title,
    body: body,
  };
  const duplicateNote = notes.find((note) =&amp;gt; note.title === title);
  if (!duplicateNote) {
    notes.push(note);
    saveNotes(notes);
    console.log(chalk.green.inverse("New note added!"));
  } else {
    console.log(chalk.red.inverse("note title taken!"));
  }
};

const removeNote = (title) =&amp;gt; {
  let newNotes = notes.filter((note) =&amp;gt; note.title !== title);
  if (notes.length &amp;gt; newNotes.length) {
    console.log(chalk.green.inverse("Note removed!"));
    saveNotes(newNotes);
  } else {
    console.log(chalk.red.inverse("No note found! or it removed"));
  }
};

const listNotes = () =&amp;gt; {
  if (notes.length == 0) {
    console.log(chalk.inverse("no note found add one"));
    return;
  }
  console.log(chalk.inverse("Your notes"));
  notes.forEach((note) =&amp;gt; {
    console.log(chalk.blue(note.title));
  });
};

const readNote = (title) =&amp;gt; {
  const note = notes.find((note) =&amp;gt; note.title === title);
  if (note) {
    console.log("title: " + chalk.blue.inverse(note.title));
    console.log("body: " + chalk.blue(note.body));
  } else {
    console.log(chalk.red.inverse("note not found!"));
  }
};

module.exports = {
  addNote,
  removeNote,
  listNotes,
  readNote,
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;5.test app :&lt;br&gt;
add command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node .\app.js add  --title="test app" --body="test"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;remove command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node .\app.js remove  --title="test app"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;read note command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node .\app.js read  --title="test app"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;list command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node .\app.js list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! You have now created a basic CLI tool using Node.js that can be shared and used by others. You can expand it with more commands and functionalities based on your specific use case.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>cli</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
