From 995a05f47ee40c109d2e0e3be8494c729aba346a Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Mon, 1 Jan 2018 18:24:43 +0200 Subject: [PATCH] Add hasFlags Check if the current process's arguments contain the specified flags. --- snippets/hasFlags.md | 21 +++++++++++++++++++++ tag_database | 1 + 2 files changed, 22 insertions(+) create mode 100644 snippets/hasFlags.md diff --git a/snippets/hasFlags.md b/snippets/hasFlags.md new file mode 100644 index 000000000..124a72aac --- /dev/null +++ b/snippets/hasFlags.md @@ -0,0 +1,21 @@ +### hasFlags + +Check if the current process's arguments contain the specified flags. + +Use `Array.every()` and `Array.includes()` to check if `process.argv` contains all the specified flags. +Use a regular expression to test if the specified flags are prefixed with `-` or `--` and prefix them accordingly. + +```js +const hasFlags = (...flags) => + flags.every(flag => process.argv.includes( + /^-{1,2}/.test(flag) ? flag : '--' + flag + )); +``` + +```js +// node myScript.js -s --test --cool=true +hasFlags('-s'); // true +hasFlags('test', 'cool=true'); // true +hasFlags('--test', 'cool=true', '-s'); // true +hasFlags('special'); // false +``` diff --git a/tag_database b/tag_database index e66052213..c4f3cd421 100644 --- a/tag_database +++ b/tag_database @@ -55,6 +55,7 @@ getURLParameters:utility groupBy:array hammingDistance:math hasClass:browser +hasFlags:node head:array hexToRGB:utility hide:browser