UNPKG

node-arg-parser

Version:

A simple, chainable command-line argument parser for Node.js CLI tools. Supports type validation, short & long args, and automatic help display.

154 lines (101 loc) β€’ 2.66 kB
# 🧩 Node Arg Parser A simple, chainable command-line argument parser for Node.js CLI tools. Supports type validation, short & long args, and automatic help display. ## πŸ“¦ Installation Install via npm: ```bash npm install node-arg-parser ``` ## 🧠 Features βœ… Short and long flags (e.g., `-f` and `--file`) βœ… Type validation (`String`, `Number`, `Boolean`) βœ… Required or optional arguments βœ… Built-in `--help` flag support βœ… Automatically aligned usage help ## πŸš€ Getting Started ### Step 1: Import the Parser ```js import ArgParser from 'node-arg-parser'; // or for CommonJS: // const ArgParser = require('node-arg-parser'); ``` ### Step 2: Define Your Arguments ```js const parser = new ArgParser(); parser .add('-f', '--file') .acceptType(String) .setDescription('The path to the input file') .required(); parser .add('-v', '--verbose') .acceptType(Boolean) .setDescription('Enable verbose mode'); parser .add('-n', '--number') .acceptType(Number) .setDescription('A numeric input'); ``` ### Step 3: Parse Arguments ```js const args = parser.parse(); console.log(args); ``` ### Step 4: Run via CLI ```bash node app.js --file input.txt --verbose true --number 123 ``` βœ… Output: ```json { "file": "input.txt", "verbose": true, "number": 123 } ``` ## πŸ” Using `--help` You don’t need to configure this manually. Just run: ```bash node app.js --help ``` 🧾 Output: ``` Usage: -f --file The path to the input file -v --verbose Enable verbose mode -n --number A numeric input ``` ## πŸ“˜ Full API Reference ### `add(short, long)` Defines a new argument. - `short`: e.g. `-f` - `long`: e.g. `--file` Returns a chainable object. ### `.acceptType(type)` Defines expected type: `String`, `Number`, or `Boolean` ### `.setDescription(text)` Sets a human-readable description (used in help output) ### `.required([true|false])` Marks argument as required (default is `false`) ## πŸ§ͺ Example with Validation ```js parser .add('-p', '--port') .acceptType(Number) .setDescription('Port to run the server on') .required(); ``` ```bash # Valid node app.js --port 3000 # Invalid node app.js --port abc # Error: Argument --port must be a number ``` ## πŸ§‘β€πŸ’» Author **Jatin Gohil** GitHub: [@lkjatinc669](https://github.com/lkjatinc669) ## 🐞 Issues & Contributions Found a bug or want to contribute? Open an issue or PR at: πŸ‘‰ [https://github.com/lkjatinc669/node-arg-parser/issues](https://github.com/lkjatinc669/node-arg-parser/issues) ## πŸ“„ License Licensed under the [ISC License](https://opensource.org/licenses/ISC)