hey guys welcome back to node.js series this is part number nine this video is
kind of preparation for the next video next video we are going to build our
first app for this node.js series which would be like a grocery list application
where you can add things into the grocery list from the command line which
will be saved in inside a file so in this video we gonna learn how to
actually pass parameters through command line to the node application if you have
not been following the entire series please do so I'll provide a list in the
corner here and welcome to techsith tutorials I'm sure most of you have used
some sort of CLI angular has a CLI and I'm sure if you use angular you have
probably seen something like this "ng serve" or if you've used like a
create-react-app then you have something like this create-react-app start okay these
are all the CLI commands so what really happens here when I call start it
actually takes its argument and parses so create react app is actually node
application and it has to understand which command it needs to run so we can
do similar for our application so I've created this app.js here and we are
going to write some commands and we're gonna do similar things but instead of
writing create react app I can write "node app" again you can build your own CLI
you can have a name for this but I'm just gonna use no absence running this
app.js then I'm gonna have some command called fly so it's telling the
application to fly now in order to catch this if I go inside the app.js I can
say console dot log and remember we looked at process which is a global
variable in node and it has a property called argcv argument means arguments vector
and so if I run this "node app fly" it actually converts the command into an
array so I have three items here and the array also has three items the first as
we see as we see it snowed second is up and the third is fly so
the node is just a location of the node app is the location of the app and the
command is fly so if I want to get this command fly I can simply get it here by
because I know it's the second element so now if I run this I will get fly so
this is how I can extract this argument now what I can do with this argument is
I can say okay if I have a command fly I can do fly so instead of console.log
what I can do is I can have command equal to process dot are we - so this
this is how I can capture the fly command and then I can say if command
equal to let's say fly then I need to be flying obviously we are not going to
build a functionality for fly but we just print this out okay so now let's
look at this so if I run it again it says flying now similarly if I have
another command called drive so 'node app drive' I would have to say else if
command equal to drive then I need to console.log driving so now if I run this
I would get driving but as we know the command line usually is a little bit
more complex than this because if I I can have a command called Drive but then
there there might be some other arguments like how fast I want to drive
and what car I want to use so I can have something like this speed equal to let's
say 30 miles per hour I can have a car equal to
that's a Nissan okay but if I still run it says driving because it's not
capturing all these other arguments and let's say console.log the process are we
again as we expected since this argument has this CLI has one two three four five
arguments it actually and a mix an array with five elements now the problem is as
this things gets more and more complex you have to parse all this information
which becomes very complex so yes you can use 'argv' but there is a better
way to do it and as you know if you need some functionality in nodejs
there's always a package somebody else has built and so before you try to build
your own functionality see if is if there's a package available and luckily
for this there's a really good package available which is called 'yargs' so how
to have first of all I have to do 'npm init' and when I do that it creates a
package.json here and then I would say 'NPM install yargs'
that's the package we are trying to install and I would say - - save so it
saves into my package.json okay so now if I look at my package Oh Jason I
would see yargs as a dependency now again you have to pick a version of yargs
set fits with your world but I just got the latest one now instead of processed.argv
we we are going to use yargs.argv we so I need to import this
yargs so I have to say const yargs = require('yargs') so now if I run
this I would get in associative array it's an object where the if you analyze
this it figured out that I am looking for my
argument my command command which is Drive which is a property underscore
then it actually finds out all the property that I need speed and car so
now let's put this code back and instead of processed.args we we can do the
yargs.argv and instead of two I would have to pick dot underscore
because underscore will always give you the command actually the command would
be an array because as you can see the underscore is nothing but an array so I
need to get the zeroth element here and then I will be able to run this so now
it's a striving now if I want to catch the other argument like car and speed I
can say if command dot speed and and command dot car if it has both well I
should put it inside the drive on the fly then I want t o print so instead of
regular driving I would say I can use a templateString and say driving then I
can interpolate command dot car at the speed of dollar sign command dot speed
else just print driving okay so let's run this again
actually here I need let perhaps equal to the yargs.argv and then
instead of command I should use params because command is not command is
actually giving me the zero if the actual command which is drive so I'm
looking for parameters so now if I run this I would get driving Nissan at speed
of 30 I can say miles per hour or whatever but yeah this is how it really
works alright so now that we have understood how the file system works how
external module works how to pass commands into your applications now
let's build a small application before we move further
so next video we are going to build a small grocery list application where we
can pass thing into the groceries via command line and I hope you learned
something new from this video and if you did please like don't forget her like
before you move further like comment and subscribe and I hope you can help me
translate this video it's pretty simple the instructions should be in the
description thank you