This is a simple and lightweight CLI flag parser for Node.js that allows you to easily define and parse command-line flags.
- Register Flags: Define flags with or without default values.
- Parse Arguments: Automatically parse and retrieve the values of the flags passed via the command line.
- Boolean Flags: Supports flags that can be toggled with a
--flag
syntax.
npm install cli-flag-parser
Before parsing the arguments, you must register the flags that your application will accept. You can register a flag with or without a default value.
const flags = require("cli-flag-parser");
// Registering flags
flags.registerFlag("name", "Name of the person")
.registerFlag("age", "Age of the person", "21");
Once the flags are registered, you can parse the command-line arguments using the parse() function.
const parsedFlags = flags.parse();
console.log(parsedFlags);
Suppose you run your script with the following command:
node yourScript.js --name=John --age 30 --verbose -d --xyz=no
Here’s how you can use the parser to interpret the arguments:
const flags = require("cli-flag-parser");
// Registering flags
flags.registerFlag("name", "name of person")
.registerFlag("age", "age of person")
.registerFlag("run", "run", true)
.registerFlag("verbose", "verbose boolean flag")
.registerFlag("d", "d boolean flag");
// Parsing the command-line arguments
const parsedFlags = flags.parse();
console.log(parsedFlags);
Output:
{
"name": "John",
"age": "30",
"run": true, // defaultValue
"verbose": true,
"d": true
}
Note: 'xyz' is not present in the output because it was not registered.
- flag (string, required) - The name of the flag.
- description (string, required)- A description of what the flag does
- defaultValue (any, optional) - Default value of flag.
- Parses the command-line arguments and returns an object with the flag names as keys and their corresponding values.
- Print the help text to the console.
- Print help text to the console and exit with error.
- Unregister all the flags.