
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
The has-flag npm package is a simple utility that checks if a specific flag is provided in the command line arguments (process.argv). It is commonly used to determine if a flag is set when running a Node.js script from the terminal.
Check for the presence of a command line flag
This feature allows you to check if a flag, such as '--unicorn', is passed to the script. The code sample demonstrates how to use has-flag to check for the 'unicorn' flag and log a message if it is present.
const hasFlag = require('has-flag');
if (hasFlag('unicorn')) {
console.log('We have a unicorn!');
}
Minimist is a more comprehensive command line argument parsing library. Unlike has-flag, which only checks for the existence of flags, minimist parses all arguments into a structured object, making it easier to work with multiple types of command line parameters.
Commander is a complete solution for building command line applications in Node.js. It provides a high-level API for parsing command line options, as well as a variety of other features such as subcommands and automated help generation. Commander is more feature-rich compared to has-flag, which is focused solely on flag detection.
Yargs is another full-featured command line option parser that offers advanced features like command chaining, argument validation, and automatic help generation. It is more complex and feature-complete than has-flag, which is a minimalistic tool for flag checking.
Check if
argv
has a specific flag
$ npm install has-flag
// foo.js
import hasFlag from 'has-flag';
hasFlag('unicorn');
//=> true
hasFlag('--unicorn');
//=> true
hasFlag('f');
//=> true
hasFlag('-f');
//=> true
hasFlag('foo=bar');
//=> true
hasFlag('foo');
//=> false
hasFlag('rainbow');
//=> false
$ node foo.js -f --unicorn --foo=bar -- --rainbow
Returns a boolean for whether the flag exists.
It correctly stops looking after an --
argument terminator.
Type: string
CLI flag to look for. The --
prefix is optional.
Type: string[]
Default: process.argv
CLI arguments.
FAQs
Check if argv has a specific flag
The npm package has-flag receives a total of 37,685,530 weekly downloads. As such, has-flag popularity was classified as popular.
We found that has-flag demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.