Check if process arguments contain flags

OSCC · 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

Recommended snippets

  • Stream is duplex

    JavaScript, Node

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

  • Stream is writable

    JavaScript, Node

    Checks if the given argument is a writable stream.

  • Stream is readable

    JavaScript, Node

    Checks if the given argument is a readable stream.