Check if process arguments contain flags

JavaScript, Node · Oct 22, 2020

Checks if the current process's arguments contain the specified flags.

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

More like this

  • Stream is duplex

    Checks if the given argument is a duplex (readable and writable) stream.

    JavaScript, Node · Oct 20, 2020

  • Stream is writable

    Checks if the given argument is a writable stream.

    JavaScript, Node · Oct 20, 2020

  • Command-line arguments

    Gets the command-line arguments passed to a Node.js script.

    JavaScript, Node · Apr 26, 2022