Minimist

Learn how to retrieve command line arguments and parse these arguments for easier use.

Up until now, you have been passing arguments to your scripts like this:

$ node my-script.js 'My Template' 'My Ticket' 'My Game'
// run script

But, this process is not very expressive. I may want to pass values without having to concern myself with how they are ordered. I might want something like this:

$ node my-script.js --name='My Game' --ticket='My Ticket' --template='My Template'
// run script

This way, regardless of the order in which these values were passed, I know that the correct value is being picked up. You can do this with a module called minimist. This module will parse arguments from the process.argv array and transform them into an object. This allows you easy access to the arguments, as they will now be available as key-value pairs inside the object.

You will stick with the convention above —option=value.

Get hands-on with 1400+ tech skills courses.