snippetjavascriptTip
Command-line arguments in Node.js
Viewed 0 times
javascriptargumentsnodecommandline
Problem
When working with Node.js scripts, you might need to access the command-line arguments passed to the script. This can be useful for configuring the script or passing additional information to it. But how can you do that?
Node.js provides the
In order to get only the command-line arguments passed to the script, you can use
If you need to check if a specific flag is present in the command-line arguments, you can use
Node.js provides the
process.argv array, which contains the command-line arguments passed to the Node.js process. The first two elements of the array are the path of the Node.js executable and the file being executed. Let's look at an example to make it clearer:In order to get only the command-line arguments passed to the script, you can use
Array.prototype.slice() to remove the first two elements of the process.argv array. Then, you can use them as needed in your script.If you need to check if a specific flag is present in the command-line arguments, you can use
Array.prototype.every() and Array.prototype.includes() to check if process.argv contains the specified flag. Then, you can use a regular expression to test if the specified flag is prefixed with - or -- and prefix it accordingly.Solution
// $ node my-script.js --name=John --age=30
process.argv;
/* [
'/path/to/node',
'/path/to/my-script.js',
'--name=John',
'--age=30'
] */In order to get only the command-line arguments passed to the script, you can use
Array.prototype.slice() to remove the first two elements of the process.argv array. Then, you can use them as needed in your script.If you need to check if a specific flag is present in the command-line arguments, you can use
Array.prototype.every() and Array.prototype.includes() to check if process.argv contains the specified flag. Then, you can use a regular expression to test if the specified flag is prefixed with - or -- and prefix it accordingly.Code Snippets
// $ node my-script.js --name=John --age=30
process.argv;
/* [
'/path/to/node',
'/path/to/my-script.js',
'--name=John',
'--age=30'
] */const getCmdArgs = () => process.argv.slice(2);
// $ node my-script.js --name=John --age=30
getCmdArgs(); // ['--name=John', '--age=30']const hasFlags = (...flags) =>
flags.every(flag =>
process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag)
);
// node my-script.js -s --test --cool=true
hasFlags('-s'); // true
hasFlags('--test', 'cool=true', '-s'); // true
hasFlags('special'); // falseContext
From 30-seconds-of-code: command-line-arguments
Revisions (0)
No revisions yet.