A powerful and type-safe TypeScript library for parsing and handling command-line arguments in Node.js applications. This library provides a set of utilities to help you efficiently parse and process various types of command-line parameters including strings, numbers (integer, float, hexadecimal, octal, binary), booleans, and JSON objects.
true
/false
, yes
/no
, y
/n
, on
/off
, 1
/0
)ArgumentParsingException
chalk
for error formatting)npm install cli-argument-helper
Comprehensive API documentation is available at: https://victorqueiroz.github.io/cli-argument-helper/
import getArgumentAssignment from "cli-argument-helper/getArgumentAssignment";
import { getString } from "cli-argument-helper/string";
import {
getInteger,
getHexadecimal,
getOctal,
getBinary,
getFloat,
getBigInt,
} from "cli-argument-helper/number";
import getBoolean from "cli-argument-helper/boolean/getBoolean";
const args = [
"--a",
"1",
"--b",
"2",
"--user-id",
"3",
"--c",
"X",
"--d=0xFF",
"--e=0o77",
"--f=0b101",
"--g=3.14",
"--h=0x1FFFFFFFFFFFFF",
"--i=false",
"--j=true",
"--k=0",
"--l=1",
"--m=y",
"--n=n",
"--o=yes",
"--p=no",
];
// Get integer values
const userId = getArgumentAssignment(args, "--user-id", getInteger); // 3
const a = getArgumentAssignment(args, "--a", getInteger); // 1
const b = getArgumentAssignment(args, "--b", getInteger); // 2
// Get string values
const text = getArgumentAssignment(args, "--c", getString); // "X"
// Get numeric values in different bases
const hex = getArgumentAssignment(args, "--d", getHexadecimal); // 255 (0xFF)
const octal = getArgumentAssignment(args, "--e", getOctal); // 63 (0o77)
const binary = getArgumentAssignment(args, "--f", getBinary); // 5 (0b101)
const float = getArgumentAssignment(args, "--g", getFloat); // 3.14
const bigInt = getArgumentAssignment(args, "--h", getBigInt); // 0x1fffffffffffffn
// Get boolean values (supports multiple formats)
const boolFalse = getArgumentAssignment(args, "--i", getBoolean); // false
const boolTrue = getArgumentAssignment(args, "--j", getBoolean); // true
const boolZero = getArgumentAssignment(args, "--k", getBoolean); // false (0)
const boolOne = getArgumentAssignment(args, "--l", getBoolean); // true (1)
const boolY = getArgumentAssignment(args, "--m", getBoolean); // true ("y")
const boolN = getArgumentAssignment(args, "--n", getBoolean); // false ("n")
const boolYes = getArgumentAssignment(args, "--o", getBoolean); // true ("yes")
const boolNo = getArgumentAssignment(args, "--p", getBoolean); // false ("no")
// After parsing, the args array is empty (all arguments consumed)
console.log(args); // []
import getJSON from "cli-argument-helper/json/getJSON";
const args = ["--config", '{"host": "localhost", "port": 3000}'];
const config = getArgumentAssignment(args, "--config", getJSON);
console.log(config); // { host: "localhost", port: 3000 }
import ArgumentParsingException from "cli-argument-helper/ArgumentParsingException";
try {
const value = getArgumentAssignment(args, "--number", getInteger);
} catch (error) {
if (error instanceof ArgumentParsingException) {
console.error("Parsing error:", error.what());
console.error("At index:", error.index);
console.error("Reason:", error.reason);
}
}
import getArgumentFromIndex from "cli-argument-helper/getArgumentFromIndex";
function getPositiveInteger(args: string[], index: number = 0): number | null {
return getArgumentFromIndex(
args,
index,
(value) => parseInt(value, 10),
(value) => Number.isInteger(value) && value > 0
);
}
const positiveNum = getArgumentAssignment(args, "--count", getPositiveInteger);
# Clone the repository
git clone https://github.com/VictorQueiroz/cli-argument-helper.git
cd cli-argument-helper
# Install dependencies
npm install
# Build the project
npm run build:clean
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
# Generate documentation
npm run build:docs
The project includes comprehensive tests covering all functionality:
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage report
npm run test:coverage
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
git checkout -b feature/amazing-feature
)npm test
)git commit -m 'Add some amazing feature'
)git push origin feature/amazing-feature
)This project uses:
Please ensure your code follows the existing style and passes all linting checks.
This project is licensed under the MIT License - see the LICENSE file for details.