After writing article about Creating Simple CRUD Using Node js, Express, Mongodb and Bootstrap For Beginner and Using fs (File System) Module in Node js to Write and Read File - Node Js Tutorial , now in this article I am going to share about the use of Yargs. Just heard about yargs ?. Me too.
Node Js Tutorial - Examples of Yargs Module in Node Js
yargs is one of the famous javascript module. Yargs helps us build interactive command line tools, by parsing arguments and generating an elegant user interface.
Ok so, let's try together what can we do with this nodejs module, Yargs.
Ok so, let's try together what can we do with this nodejs module, Yargs.
install yargs
npm install yargs
make a folder containing 2 files
yargs files |
yargs.js file - Node Js Tutorial - Yargs Examples
yargs.js contains yargs command. Later we will run this file.
const yargs = require('yargs')
const note = require('./yargfunction')
//EXAMPLE TO WRITE
yargs.command({
command: 'add', // set the command
describe: 'to add data', //give description
builder: { //set the builder -- it contains the key to sore the value
title: {
demandOption: true, //required?
type: 'string',
describe: 'this is the title'
},
content: {
type: 'string',
demandOption: true,
describe: 'this is the content'
}
},
handler: function (argv) { //handler is the place we can put function
console.log(`title: ${argv.title}, conten: ${argv.content}`)
}
})
//EXAMPLE 2 - TO WRITE
yargs.command({
command: 'mymessage',
describe: 'maybe you need to say hello first',
builder: {
message: {
type: 'string',
demandOption: 'required'
}
},
handler: function (argv) {
if (argv.message == "hello") {
console.log(`oh you say ${argv.message} ? hiiiii :*`)
} else {
console.log(`why you don't say hello first ??`)
}
}
})
// EXAMPLE 3 CREATE and UPDATE JSON FILE USING FS and Yargs
yargs.command({
command: 'addJSONFile',
describe: 'function to add json file',
builder: {
title: {
type: 'string',
describe: 'this is the title'
},
content: {
type: 'string',
describe: 'this is the content'
}
},
handler: function (argv) {
//call function from yargfunction.js
note.addNote(argv.title, argv.content)
}
})
//Update JSON
yargs.command({
command: 'updateJSONFile',
describe: 'this is to update existing json file',
builder: {
thefilename: {
type: 'string',
demandOption: true,
describe: 'this is the filename'
},
updatedTitle: {
type: 'string',
demandOption: true,
describe: 'this is the titile'
},
updatedContent: {
type: 'string',
demandOption: true,
describe: "this is the content content"
}
},
handler: function (argv) {
//The name of the variable argv stands for "argument vector". A vector is a one-dimensional array, and argv is a one-dimensional array of strings. Each string is one of the arguments that was passed to the program. source: crasseux.com
//call function from yargfunction.js
note.updateNote(argv.thefilename, argv.updatedTitle, argv.updatedContent)
}
})
yargs.parse() // to parse the result
yargfunction.js file - Node Js Tutorial - Yargs Examples
yargfunction.js is the file containing functions that are needed to run one command in yargs,js file.
const fs = require('fs')
//addnote function
const addNote = function (title, content) { //set the parameter
const rawData = {
title: title,
content: content
}
const toJsonstring = JSON.stringify(rawData) //make the objevt becomes json form in string
fs.writeFileSync('yarg.json', toJsonstring) //use fs function to create a file and insert data (toJsonString variable)
}
//update function
const updateNote = function (thefilename, title, content) {
const readFileBuffer = fs.readFileSync(`${thefilename}`) //read the file
const stringIt = readFileBuffer.toString() // change the readFileBuffer to string, because the result of readFileBuffer is in binary
console.log(stringIt)
const makeItJsonObject = JSON.parse(stringIt) //convert the json string to object, so we will be able to modify the value
console.log(makeItJsonObject)
//set the change
makeItJsonObject.title = title
makeItJsonObject.content = content
console.log(makeItJsonObject)
const stringitagain = JSON.stringify(makeItJsonObject) //after changing the value, then we can convert it again to json string
console.log(stringitagain)
fs.writeFileSync(`${thefilename}`, stringitagain) // and finally we can rewrite the file with the new data
}
module.exports = { addNote, updateNote }
Simple example
1. Make simple note
in this first example we are going to create yargs command to record our input through console.
how to run it?
open console and run :
node yarg.js add --title="YOUR_TITLE" --content="YOUR_CONTENT"
if we run that command the result will be shown in the console
FIrst example of yargs - Examples of Yargs Module in Node Js - Node Js Tutorial |
2.Combining yargs with else if condition
in this example I am going to use yargs to return values regarding the input.how to run it
open console and run:
node yarg.js mymessage --message="easy ?"
The result will be like this
second example of yargs - Examples of Yargs Module in Node Js - Node Js Tutorial |
3. Creating Json file using yargs and update it
first we will add a json file and write a json value. you can write the code like below
then, we can see that the json file is already created and inside the file there are some lines of code of json. yarg.json is resulted from the yargfunction.js.
if we want to update it, we can use this command (like beow image)
then if we see the json file, it will change automatically.
I hope it can be useful. You can check about this simple project in my github : https://github.com/ZenHuzaini/nodejs-yargs-examples
Tag :
nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, Examples of Yargs Module in Node Js - Node Js Tutorial, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs
third example of yargs - Examples of Yargs Module in Node Js - Node Js Tutorial |
then, we can see that the json file is already created and inside the file there are some lines of code of json. yarg.json is resulted from the yargfunction.js.
yarg.json - Examples of Yargs Module in Node Js - Node Js Tutorial |
if we want to update it, we can use this command (like beow image)
update commnd yargs - Examples of Yargs Module in Node Js - Node Js Tutorial |
then if we see the json file, it will change automatically.
yarg.json - Examples of Yargs Module in Node Js - Node Js Tutorial |
I hope it can be useful. You can check about this simple project in my github : https://github.com/ZenHuzaini/nodejs-yargs-examples
Tag :
nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, Examples of Yargs Module in Node Js - Node Js Tutorial, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs, how to use yargs nodejs, yargs module in nodejs, nodejs, yargs, yargs examples, yargs for nodejs, how to use yargs nodejs, yargs module in nodejs
EmoticonEmoticon