The ๐ฝ๐ฟ๐ผ๐ฐ๐ฒ๐๐.๐ฎ๐ฟ๐ด๐ method lets you natively parse command line arguments, without the need for any additional libraries.
It returns an array of strings containing all the args that were passed when launching the ๐ก๐ผ๐ฑ๐ฒ๐ท๐ process.
The first and second args are reserved, here's what they mean:
First: Is the ๐ฝ๐ฟ๐ผ๐ฐ๐ฒ๐๐.๐ฒ๐ ๐ฒ๐ฐ๐ฃ๐ฎ๐๐ต which is the absolute path of the executable that started the ๐ก๐ผ๐ฑ๐ฒ๐ท๐ process.
Second: Absolute path for the file currently being executed.
Did you learn something new today?
Like and share this post, and follow me for more!
Top comments (4)
Sorry but this is "get" not "parse". Parsing is when you get command line options. When you pass
script.js -xo --name=hello
and you get that you used option "x" and "o" and "name" with value "hello". You can read what is Parsing on Wikipedia.I've written NPM package for parsing command line called lily. But there is also well establish one called yargs and it's little friend yargs-parser.
I'm aware of yargs and other parsers. I said parser in this post to note what node does under the hood when it takes the values from the standard input stream; not parsing in the context you're referring to.
But this is not something that Node is doing, every single program in the system gets argument as array of strings. So this is by no means parsing by Node. If you will learn C language you will know that arguments in the system are in the form of array of strings. So No Parsing. So your "parsing" is done by the operation system.
I think you're right about the string array args. Because argv seems like a convention taken from C and C++. Java havd something similar too. If OS is doing the parsing then that's great, at least it's done somewhere under the hood haha. Thanks for your explanation. I appreciate it.