https://github.com/abhi11210646/cli-flag-parser
A simple and lightweight CLI flag parser for Node.js that allows you to easily define and parse command-line flags
https://github.com/abhi11210646/cli-flag-parser
cli flag-parser flags
Last synced: 6 months ago
JSON representation
A simple and lightweight CLI flag parser for Node.js that allows you to easily define and parse command-line flags
- Host: GitHub
- URL: https://github.com/abhi11210646/cli-flag-parser
- Owner: abhi11210646
- Created: 2024-08-23T08:16:52.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-09-27T17:39:51.000Z (8 months ago)
- Last Synced: 2024-11-10T10:05:35.360Z (6 months ago)
- Topics: cli, flag-parser, flags
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/cli-flag-parser
- Size: 23.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CLI Flag Parser
This is a simple and lightweight CLI flag parser for Node.js that allows you to easily define and parse command-line flags.
## Features
- **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.## Installation
```shell
npm install cli-flag-parser
```## Usage
### Registering Flags
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.
```javascript
const flags = require("cli-flag-parser");// Registering flags
flags.registerFlag("name", "Name of the person")
.registerFlag("age", "Age of the person", "21");```
### Parsing Arguments
Once the flags are registered, you can parse the command-line arguments using the parse() function.```javascript
const parsedFlags = flags.parse();
console.log(parsedFlags);
```## Example
Suppose you run your script with the following command:```shell
node yourScript.js --name=John --age 30 --verbose -d --xyz=no
```
Here’s how you can use the parser to interpret the arguments:
```javascript
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.
```
## API Reference
#### registerFlag(flag, description, defaultValue)
- 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.
#### parse()
- Parses the command-line arguments and returns an object with the flag names as keys and their corresponding values.
#### help()
- Print the help text to the console.
#### showHelp()
- Print help text to the console and exit with error.
#### unregisterFlags()
- Unregister all the flags.