![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@slimio/arg-parser
Advanced tools
Secure and reliable Command Line Argument parser for Node.js ! ArgParser was designed to be embedded in a SlimIO agent, most popular library was not matching our expectation (and security needs).
It does not aim to replace (or to be) popular CLI lib like yargs
or commander
. Please, do not use this package if you do not know what you are doing.
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @slimio/arg-parser
# or
$ yarn add @slimio/arg-parser
Create the following javascript script:
const { parseArg, argDefinition } = require("@slimio/arg-parser");
const result = parseArg([
argDefinition("-c --colors [array]", "Array of colors"),
argDefinition("--verbose", "Enable verbose mode!")
]);
console.log(result);
And then run the following command line:
$ node yourscript --colors red blue --verbose
$ Map { 'colors' => [ 'red', 'blue' ], 'verbose' => true }
Generate a new Command definition. cmd argument is a string pattern that will be matched against the following regex:
/^(-{1}(?<shortcut>[a-z]){1})?\s?(-{2}(?<name>[a-z]+)){1}\s?(\[(?<type>number|string|boolean|array)(=(?<defaultVal>.*))?\])?$/;
Take a look at the root directory example
for more examples of how to use addCommand !
const { parseArg, argDefinition } = require("@slimio/arg-parser");
const result = parseArg([
argDefinition("--verbose", "Enable verbose mode!"),
argDefinition("-a --autoreload [number=500]", "Configuration Autoreload delay in number")
]);
A command is described as follow on TypeScript:
interface Command {
name: string;
type: string;
description: string;
shortcut?: string;
defaultVal?: number | string | boolean | any[];
}
Feel free to redefine the wrapper as you want !
Parse Argv (or any input string[]
). Return a ECMAScript6 Map Object.
const { parseArg, argDefinition } = require("@slimio/arg-parser");
const argv = parseArg([
argDefinition("--level [number=1]")
], ["--level", "10"]);
console.log(argv.get("level"));
Under the hood we use TypeScript with the following type
export type ArgvResult<T> = Map<keyof T, T[keyof T]>;
Display all commands information
const cmdDef = [
ArgParser.argDefinition("-p --product [number=10]", "Product number description"),
ArgParser.argDefinition("-t --truc [string]"),
ArgParser.argDefinition("--bidule")
];
ArgParser.help(cmdDef);
// output ->
// Usage :
// - node file.js <command>
// - node file.js <command> <value>
//
// <command> <type> <default> <description>
// -p --product number 10 Product number description
// -t --truc string
// --bidule boolean true
This project have no dependencies.
MIT
FAQs
SlimIO argument parser
The npm package @slimio/arg-parser receives a total of 4 weekly downloads. As such, @slimio/arg-parser popularity was classified as not popular.
We found that @slimio/arg-parser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
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.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.