Documente Academic
Documente Profesional
Documente Cultură
--------------------------------------------------
app.js
require('./test');
--------------------------------------------------
How browser has window and document objects
Simillarly node has global and process
on process there is property argv
(which contain all the arguments provided)
console.log(process.argv)
So if we write
console.log(process.argv[2])
it will always return, what the value we provided i.e "sam"
ex-2
if we do something like this
console.log(process.argv);
[ 'C:\\Program Files\\nodejs\\node.exe',
'C:\\Users\\sambeet.sahoo.OM\\Desktop\\node\\app',
'add',
'--title=cool' ]
npm i yargs
console.log(process.argv);
console.log(yargs.argv);
node app
[ 'C:\\Program Files\\nodejs\\node.exe',
'C:\\Users\\sambeet.sahoo.OM\\Desktop\\node\\app' ]
{ _: [], '$0': 'app' }
Now do this
----------------------------------
Setting up yargs
Now create add commands, and node will execute by the commands
yargs.command({
command:'add',
describe:'add a new note',
handler: function(){
console.log("Adding a new note")
}
})
// create remove command
yargs.command({
command:'remove',
describe:'remove a new note',
handler: function(){
console.log("removing a new note")
}
})
console.log(yargs.argv);
--------------------------------------------------
File system module
const fs = require('fs');
fs.writeFileSync('notes.txt',"Lorem ipsum eta...")
// this will create a file notes.txt and write 'Lorem ipsum eta...'