Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
A flexible and easy-to-use command-line flag parsing library for Node.js applications.
Install using your favorite package manager:
npm install flags
# or
yarn add flags
# or
pnpm add flags
Here's a quick example of how to use Node-Flags:
import * as flags from "flags";
// Define flags
flags.defineString("name", "Anonymous", "Your name");
flags.defineInteger("age", 21, "Your age in years");
flags.defineNumber("height", 1.8, "Your height in meters");
flags.defineStringList("pets", [], "List of your pets");
flags.defineMultiString("hobby", [], "Your hobbies");
// Parse command-line arguments
flags.parse();
// Access flag values
const info = [
`Name: ${flags.get("name")}`,
`Age: ${flags.get("age")}`,
`Height: ${flags.get("height")}m`,
`Pets: ${flags.get("pets").join(", ")}`,
`Hobbies:\n ${flags.get("hobby").join("\n ")}`,
];
console.log(info.join("\n"));
Run your script with flags:
node example.js --name="John Doe" --age=30 --height=1.75 --pets=dog,cat --hobby=reading --hobby=gaming
Node-Flags provides several methods to define flags:
defineString(name, defaultValue, description)
defineBoolean(name, defaultValue, description)
defineInteger(name, defaultValue, description)
defineNumber(name, defaultValue, description)
defineStringList(name, defaultValue, description)
defineMultiString(name, defaultValue, description)
Each method returns a Flag
object that allows further configuration:
flags
.defineString("api-key")
.setDefault("your-default-key")
.setDescription("API key for authentication")
.setValidator((value) => {
if (value.length < 10) {
throw new Error("API key must be at least 10 characters long");
}
})
.setSecret(true);
--flagname
--flagname=value
or --flagname value
--message="Hello, World!"
--
to separate flags from additional arguments: --flag1 value1 -- arg1 arg2
Access flag values using flags.get(flagName)
or flags.FLAGS.flagName.get()
.
Flag objects also provide properties like name
, defaultValue
, currentValue
, and isSet
.
Node-Flags automatically generates help text. Access it by running your script with the --help
flag.
For testing, you can pass predefined arguments to flags.parse()
:
flags.parse(["--flag1", "--noflag2", "--flag3=value"]);
Reset flags between test cases:
flags.reset();
This project is licensed under the MIT License. See the LICENSE file for details.
FAQs
Flag library for node.js
The npm package flags receives a total of 7,656 weekly downloads. As such, flags popularity was classified as popular.
We found that flags demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.