When you run Node.js, you'll have access to the process
object. If you console log it out you'll see that it has quite a bit of information stored on it. Dig through there and check it out!
We will focus on the argv
property. This property is an array that has the path to node and our script as the first and second values. When we pass in arguments or flags to our script, argv include the values that we will pass.
We will see how we can check for flags and run conditional logic based on the flags that are passed.
Kevin Cunningham: [0:00] When I run my script, eventually by running the command quizme, I want to be able to tell whether or not any flags have been passed in. But how do I do that? At the moment, I have a blank index.js. If I run node index.js, Node boots up, parses this file, and when there's nothing to do, it exits. [0:20] I'm going to console.log the process that Node is running when it executes this script. You can see this prints out quite a lot of information about the current running process, but what I want to focus on is this argv right here.
[0:34] This argv has, at the moment, two elements. The first element is the version of Node that was used to execute my script. My version was version 18.2. The second element is the path to the script. Let me change this to process.argv and rerun. We've got the same thing now.
[0:56] If I pass anything else in here, like Hello Egghead, we see now is the process.arg still has the first two elements. They've remained constant, but Hello, which is this argument, is now in the array and Egghead is now in the array. Equally, if I added in any flags, they would be appended to that array.
[1:16] If I want to understand what's been passed in on the command line, I need to look at process.argv and I want to check whether or not a flag is being passed.
[1:27] Let's create a flags array. I want to push any flags that have a dash or a double dash into that array. I want to take every element of process.argv and loop over it. For each argument, I want to test it. I want to know, does it start with a dash? I want to use regex and say, does it start with a dash? I want to test my argument against that.
[1:52] That will return true, if my element of process.argv starts with a dash. In that case, I want to push onto my flags array this particular argument but actually, I want to remove all of the dashes or replace all the dashes just with an empty string.
[2:11] Now if I console.log my flags, I can see I've got my a and add and that means I can control my logic. I can say that if flags.includes('a') or flags.includes('add') then I want to add some values, else do some work. Add some values because I've got the add, add some values because you still got the a, get rid of that. Now, I want to do some work.
[2:45] One last thing to note about process.argv is, if I pass in some values to it...Say I had node index.js, this value, these are separate, could have been separate, as we've seen before, but if they are contained in quotes, they'll be considered as a single value. If you want to group together strings to make a single parameter or a single argument, you would wrap them in quotes.