DEV Community

Mohammad Moallemi
Mohammad Moallemi

Posted on • Originally published at mmoallemi99.com

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

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' ]
Enter fullscreen mode Exit fullscreen mode

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

node main.js first second third  

~   
❯ node main.js  first second third  
[  
  'first',  
  'second',  
  'third'  
]
Enter fullscreen mode Exit fullscreen mode

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'  
]
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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' │  
└─────────┴───────────┴───────────────┘
Enter fullscreen mode Exit fullscreen mode

Happy scripting!

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

Top comments (0)