Learn nodejs, Argument variables with process.argv

One important object that is available to us globally is the process object. It can be accessed from anywhere, and it contains functionality that allows us to interact with information about the current process instance.

We can use the process object to get environment information, read environment variables, communicate with the terminal, or parent processes, through standard input and standard output. We can even exit the current process. This object essentially gives us a way to work with the current process instance. One of the the things that we can do with the process object is to collect all the information from the terminal, or command prompt, when the application starts.

All of this information will be saved in a variable called process.argv which stands for the argument variables used to start the process. So let’s go ahead and navigate to our finder. In the start folder there is an empty JavaScript file called app.js, I’m going to go ahead and open up that file. And the first thing I’m going to do is just log the process.argv variable. Now process is available to us in any module globally. So we can use the process object wherever we like.

Let me go ahead and save this. And then navigate out to the terminal. And we can run our file simply by typing “node app.js”. And when we do so, we can see that the process.argv variable is an array. It contains a path to node, and a path to app.js. We started the app by running node. User/local/bin is the directory that that executable is found in. We also started this app.js file and we can see that we have the full path to that here. So, what happens if we start the process again and send some more information? Like I might send a user flag and a greeting.

So now, when I start this process, I am sending some more information. Now we can see that our process.argv array has grown. It also contains our user flag and a value for the user, and a greeting flag, and a value for the greeting. Now, because this is an array, we can work with it the same way we would work with JavaScript arrays. Let’s go back to our code. And let’s create a function that will help us grab the values from the process.argv array from their flag name.

So I’m going to create a function called “grab”. And this function is going to take in the flag as an argument, either the greeting or the user. And we want it to return the value of that flag. Well since process.argv is just an array, I can create a variable called “index”, and I can set it to process.argv, that’s an array, so I can use indexOf and then we can go ahead and find the index of the flag in the array.

Now, what we want to do is return the value, or the next variable in the array. So we’re going to have to use the index plus one in order to get that value. So now we want to make sure that we are able to find that flag within the array. I’m going to add a return statement here. And the first thing that we want to do, is we want to test this index to make sure it doesn’t equal negative one. So if this index is equal to negative one, that means that we did not find the flag within the array. So we will just return null from this function.

Now, if this index is not equal to negative one, it means that we did find the flag in the array. So we’re going to use this index to return the next item from the array. Process.argv is an array so we can return index+1, and that should be the value found after the flag. Let’s go ahead and use our new grab function to set some variables from those flags that we used to start the application. I will set a variable for greeting. And we will grab the greeting from the process.argv array, simply by using that grab function and sending the greeting flag.

Let’s create a variable for our user. We will grab the user simply by sending the user flag to our grab function, and that should give us the value for the user. Now, let’s go ahead and say if we do not have a user, or we do not have a greeting, let’s log a message to the console. We will go ahead and log “You Blew it!”. Now, if we do have the user and we do have a greeting, we can use that information and log it back to the console.

I’m going to use a template string for this. And, remember, to create our template string we want to use this backtick characters. So I will add two backticks. And what I’m going to go ahead and do is simply say “Welcome”, and we will use the value of the user variable by placing it in between the curly brackets found after the dollar sign for the template string. And we’ll go ahead and also output their greeting. So if you send us a user and a greeting, we will display that information back to you in the terminal.

Let’s go ahead and save this. And go back out to the terminal and run our new app. So I’m going to run this app first by typing node app. And remember I can use the js, or not. And it says “You Blew it!”. And that’s because we didn’t send the greeting and a user. So let me try this again. Node app; this time I’m going to send a user, Alex, and a greeting, “Hello Hello Hello”. And this time when we run it, it says “Welcome Alex, Hello Hello Hello”.

So the process.argv array gives us a way to get information into the app when we start it. This is a great tool if we want to specify certain ports, or specify certain file folders for our app to use at the very beginning.

Leave a Reply

avatar
1000
  Subscribe  
Notify of