- [Instructor] One important object that's available
to us globally is the process object.
So it can be accessed from absolutely anywhere,
and it contains functionality that allows us to interact
with information about the current process instance.
With the process object,
we can get environment information,
read environment variables, communicate with the terminal,
and we can exit the current process.
So it gives us a way to work
with that current process instance.
So one of the things that we can do with the process object
is collect information from the terminal.
So this information is stored in the process.argv array.
So check this out.
We're going to get rid of the code that was here before
and replace it with a console log to figure out
what is available in this process.argv value.
Now, we want to make sure that we're navigated
to the right file here, and we can run node global.
This will then return to us this process.argv array.
As you can see, it's an array that contains
the path to node as well as the path to the file.
So basically what we're seeing here is everything
that I have sent when I created this command.
So the location of node where
that's running and then the file itself.
Let's try to run our app again,
but this time we're going to pass in some more information.
So we'll say --user and then our name,
and then we'll say --greeting,
and we'll say "hello from node."
Now if we hit Enter, we see a larger array.
Now the process argv array starts
with the same two things at the beginning,
but each additional array value represents
the rest of what we typed into the console
after the global module.
So we see our user flag and our name,
and we see our greeting flag and the greeting.
So let's try to create the little function
that's going to help us process some of these values.
We're going to create a function called grab,
and here we're going to grab this flag.
We will say let indexAfterFlag
equal process.argv.index of flag, plus one.
So this is going to give us the position
of the index after the flag.
So whatever this value is and whatever this value is.
So we're going to grab it this way.
We'll say return process argv and then the index
we want to grab is the index after the flag.
So now let's try to grab the value after the greeting.
We'll say let greeting equal grab --greeting,
and then we'll say let user equal grab.
We'll call that grab function,
and this time we want to pass it the user flag.
Perfect, so now we can console-log the greeting,
and then we'll console-log the user.
I also want to stop console-logging
the process argv value up here.
We'll just delete that, just so we don't get confused,
and then check it out.
We have been able to isolate the value
that we've passed in with that flag for both.
So utilizing this process.argv array is going
to let us collect user input.
We can create all sorts of cool command line applications
with node just by understanding this array.