How to accept command line arguments in Node.js scripts?

How to accept command line arguments in Node.js scripts?

In this short blog post, we’re going to see how we can write a Node.js script that accepts command line arguments and named arguments.

As we know in any Node.js script we have an object called…


This content originally appeared on DEV Community and was authored by Mohammad Moallemi

How to accept command line arguments in Node.js scripts?How to accept command line arguments in Node.js scripts?

In this short blog post, we’re going to see how we can write a Node.js script that accepts command line arguments and named arguments.

As we know in any Node.js script we have an object called process which contains a lot of information about the current running process from environment variables to PID and etc...

One of the available keys in process object is argv and we can easily access it via process.argv.

The two first items are Node.js executable path and JavaScript file path followed by provided command-line arguments unless you run it in an interactive Node.js shell.

// From script file named main.js  
console.log(process.argv)  
[ '/usr/bin/node', '/home/mmoallemi/main.js' ]  

// From interactive node  
❯ node  
Welcome to Node.js v15.14.0.  
Type ".help" for more information.  
> process.argv  
[ '/usr/bin/node' ]

We should use process.argv.slice(2) instead:

node main.js first second third  

~   
❯ node main.js  first second third  
[  
  'first',  
  'second',  
  'third'  
]

Great! So far we have achieved to pass positional arguments to our script but if we want to use named arguments or some kind of flags?

node main.js extract --input=test.txt --output=results.txt  
[  
  'extract',  
  '--input=test.txt',  
  '--output=results.txt'  
]

We passed one positional argument and two named arguments, we can go ahead and clean the values and split them by = and this kind of stuff, but let's do it right.

Install minimist using your favorite package manager, I use yarn:

yarn add minimist

and then pass arguments to minimist to parse it:

// node main.js extract --input=test.txt --output=results.txt  
// main.js script  
const minimist = require('minimist')  

const args = process.argv.slice(2)  

const parsedArgs = minimist(args)  

console.log('Parsed Arguments:', parsedArgs)  

console.log('Input:', parsedArgs.input)  
console.log('Output:', parsedArgs.output)  

console.table(parsedArgs)

Enjoy the results!

Parsed Arguments: { _: [ 'extract' ], input: 'test.txt', output: 'results.txt' }  

Input: test.txt  
Output: results.txt  

┌─────────┬───────────┬───────────────┐  
│ (index) │     ۰     │    Values     │  
├─────────┼───────────┼───────────────┤  
│    _    │ 'extract' │               │  
│  input  │           │  'test.txt'   │  
│ output  │           │ 'results.txt' │  
└─────────┴───────────┴───────────────┘

Happy scripting!

Originally published at https://mmoallemi99.com on May 2, 2021.


This content originally appeared on DEV Community and was authored by Mohammad Moallemi


Print Share Comment Cite Upload Translate Updates
APA

Mohammad Moallemi | Sciencx (2021-05-02T20:34:35+00:00) How to accept command line arguments in Node.js scripts?. Retrieved from https://www.scien.cx/2021/05/02/how-to-accept-command-line-arguments-in-node-js-scripts/

MLA
" » How to accept command line arguments in Node.js scripts?." Mohammad Moallemi | Sciencx - Sunday May 2, 2021, https://www.scien.cx/2021/05/02/how-to-accept-command-line-arguments-in-node-js-scripts/
HARVARD
Mohammad Moallemi | Sciencx Sunday May 2, 2021 » How to accept command line arguments in Node.js scripts?., viewed ,<https://www.scien.cx/2021/05/02/how-to-accept-command-line-arguments-in-node-js-scripts/>
VANCOUVER
Mohammad Moallemi | Sciencx - » How to accept command line arguments in Node.js scripts?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/02/how-to-accept-command-line-arguments-in-node-js-scripts/
CHICAGO
" » How to accept command line arguments in Node.js scripts?." Mohammad Moallemi | Sciencx - Accessed . https://www.scien.cx/2021/05/02/how-to-accept-command-line-arguments-in-node-js-scripts/
IEEE
" » How to accept command line arguments in Node.js scripts?." Mohammad Moallemi | Sciencx [Online]. Available: https://www.scien.cx/2021/05/02/how-to-accept-command-line-arguments-in-node-js-scripts/. [Accessed: ]
rf:citation
» How to accept command line arguments in Node.js scripts? | Mohammad Moallemi | Sciencx | https://www.scien.cx/2021/05/02/how-to-accept-command-line-arguments-in-node-js-scripts/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.