811 B
811 B
title, tags, expertise, cover, firstSeen, lastUpdated
| title | tags | expertise | cover | firstSeen | lastUpdated |
|---|---|---|---|---|---|
| Check if process arguments contain flags | node | intermediate | blog_images/white-tablet.jpg | 2018-01-01T18:24:43+02:00 | 2020-10-22T20:23:47+03:00 |
Checks if the current process's arguments contain the specified flags.
- Use
Array.prototype.every()andArray.prototype.includes()to check ifprocess.argvcontains all the specified flags. - Use a regular expression to test if the specified flags are prefixed with
-or--and prefix them accordingly.
const hasFlags = (...flags) =>
flags.every(flag =>
process.argv.includes(/^-{1,2}/.test(flag) ? flag : '--' + flag)
);
// node myScript.js -s --test --cool=true
hasFlags('-s'); // true
hasFlags('--test', 'cool=true', '-s'); // true
hasFlags('special'); // false