🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

cli-flag-parser

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli-flag-parser

A simple and lightweight CLI flag parser for Node.js that allows you to easily define and parse command-line flags.

latest
Source
npmnpm
Version
1.1.1
Version published
Weekly downloads
961
-44.77%
Maintainers
0
Weekly downloads
 
Created
Source

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

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.

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.

const parsedFlags = flags.parse();
console.log(parsedFlags);

Example

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.

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.

FAQs

Package last updated on 27 Sep 2024

Did you know?

Socket

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.

Install

Related posts