Menu

gitpiper

hasFlags javascript Code Snippet in 2024

nodeintermediate

Last Updated: 11 October 2024

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

  • Use Array.prototype.every() and Array.prototype.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.
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

javascript snippet similar to hasFlags For You in October 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️