Just like in bash, we can pass CLI to a node application. NodeJS has process , something that allows us to access CLI. Just like how the browser JS supports document Node has its own. Consider the following:
Above, we we to run console.log(process.argv);, we would get:
As seen above, the first two are paths to node and the file we are running, and lastly, the CLI is shown. Of course, to access one of the command line arguments, we simply pass an index to the argv vector.
Argument Parsing with Yargs
Yargs is a module we can install from NPM, and it simply provides a more clean UI experience and introduces some new functionality. For instance, when we do the following:
A nice utility of Yargs is that we can add commands:
So, we can add the command line argument add, and through Yargs, it will execute the handler for that command. But, realistically, if we are to add a note through the add CLI, we would need to know more about the title, perhaps something like “title”, etc. So, we introduce the builder property, which is an object:
Storing Data with JSON
Now, prior to this, we’ve done some work that will help us build towards a “notes app”. Before that, we should look at where such data could be stored.
The above is the general way you will handle JSON’s, reading them in, parsing them, turning them into strings, etc.
Adding a Note
Here is the logic code for adding a note:
The rest of the command line argument functions are finished in the folder notes-app. Reference that directory for information. This wraps up the main things we need to know about JSON manipulation in Node.