AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Nodejs command line arguments4/10/2023 So, if you are writing an application that has even a moderate level of complexity, you will likely want to use something like minimist. The one I have probably heard about the most is minimist. There are Node.js modules that provide powerful abstractions when it comes to Node.js command-line arguments. While the examples here were quite basic, they are perfectly valid and could be used in your code. Execute the following command in a terminal prompt: node showArgs-3.js arg0 arg1 arg2.Inside of each iteration, we use a console.log statement to output a more human-friendly message that shows the argument that is contained in that element. Instead of a simple console.dir statement, we use the forEach method of the args array for iteration. In Example # 4A, we make the output a bit easier on the eyes. Execute the following command in a terminal prompt: node showArgs-2.js arg0 arg1 arg2.Example # 3C shows the output, which is an array containing only the arguments that we passed to the program. In Example # 3B, we execute our program, passing three arguments: “arg0”, “arg1” and “arg2’. We then inspect the args array using a console.dir statement. Specifically, process.argv is an array, so we use its slice method to remove the first two elements of the array, and assign that return value to the variable args. In Example # 3A, we use the method to remove the first two elements of the arguments array. Execute the following command in a terminal prompt: node showArgs-1.js arg2 arg3 arg4.Since we know that the first two arguments will be the same every time, let’s try to get rid of those first two arguments. Remember: arguments 0 and 1 are node and the file that contains your program, so the additional arguments here are indexed as 2, 3 and 4. I used the numbers 2, 3, and 4 because they make more sense, due to the 0-based nature of this arguments array. In Example # 2B, you’ll see that we passed some additional arguments to our program: “arg2”, “arg3” and “arg4”. Execute the following command in a terminal prompt: node showArgs-1.js.This path will vary for each user so I simply put “” for the folder structure that leads up to that file in the github repo that you cloned (see “How to Demo” below). The first element of that array is node, and the second element is the path to the program. So, in the console, you will see an array. In Example # 1B, we have the actual command you use to execute the program first node (the executable for Node.js), and then the name of the file we want to execute: showArgs-1.js.Įxample # 1C shows the output of this program. The output will go straight to the terminal window the moment you execute your program. Then this line simply inspects the argv property of the process objet. In Example # 1A, we have the contents of the file: showArgs-1.js. The argv property is an array that contains all arguments provided when you executed your program on the command line. The process object is a global object, which means that you can access it from anywhere in your program. The key to accessing command-line arguments lies in the argv property of the process global. So, in this article, I will explain the basics of how to gain access to the arguments that are passed to your node application on the command line. Once your application grows to even a modest level of complexity or sophistication, you will probably want to accept arguments on the command line. When you use Node.js as a command-line tool, you most likely type the following into your terminal application: node some-file.js. Learn how to access command-line arguments passed to your node.js file
0 Comments
Read More
Leave a Reply. |