Skip to content

Getting Started

A <1kB library for parsing CLI flags. Inspired by Deno’s std/cli parseArgs module.

Features

🤏 very small

🍃 very simple

🏃 very fast (beats node:util)

🔏 strongly typed

Installation

You can install Args using npm, yarn, or pnpm:

Terminal window
npm install @bomb.sh/args

Quick Start

Basic usage does not require any configuration.

import {
function parse<TArgs extends Values<TBooleans, TStrings, TCollectable, undefined, TDefaults, TAliases>, TBooleans extends BooleanType = undefined, TStrings extends StringType = undefined, TCollectable extends Collectable = undefined, TDefaults extends Record<string, unknown> | undefined = undefined, TAliases extends Aliases<TAliasArgNames, TAliasNames> | undefined = undefined, TAliasArgNames extends string = string, TAliasNames extends string = string>(argv: string[], { default: defaults, alias: aliases, ...types }?: ParseOptions<TBooleans, TStrings, TCollectable, TDefaults, TAliases>): Args<TArgs>
parse
} from "@bomb.sh/args";
// my-cli build --bundle -rf --a value --b=value --c 1
const
const argv: string[]
argv
=
var process: NodeJS.Process
process
.
NodeJS.Process.argv: string[]

The process.argv property returns an array containing the command-line arguments passed when the Node.js process was launched. The first element will be

execPath

. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command-line arguments.

For example, assuming the following script for process-args.js:

import { argv } from 'node:process';
// print process.argv
argv.forEach((val, index) => {
console.log(`${index}: ${val}`);
});

Launching the Node.js process as:

Terminal window
node process-args.js one two=three four

Would generate the output:

0: /usr/local/bin/node
1: /Users/mjr/work/node/process-args.js
2: one
3: two=three
4: four

@sincev0.1.27

argv
.
Array<string>.slice(start?: number, end?: number): string[]

Returns a copy of a section of an array. For both start and end, a negative index can be used to indicate an offset from the end of the array. For example, -2 refers to the second to last element of the array.

@paramstart The beginning index of the specified portion of the array. If start is undefined, then the slice begins at index 0.

@paramend The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. If end is undefined, then the slice extends to the end of the array.

slice
(2);
const
const args: {
[x: string]: any;
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, any>, undefined, undefined, undefined, undefined, undefined, string, string>(argv: string[], { default: defaults, alias: aliases, ...types }?: ParseOptions<undefined, undefined, undefined, undefined, undefined> | undefined): {
...;
}
parse
(
const argv: string[]
argv
);
var console: Console

The console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.

The module exports two specific components:

  • A Console class with methods such as console.log(), console.error() and console.warn() that can be used to write to any Node.js stream.
  • A global console instance configured to write to process.stdout and process.stderr. The global console can be used without importing the node:console module.

Warning: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the note on process I/O for more information.

Example using the global console:

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr

Example using the Console class:

const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err

@seesource

console
.
Console.log(message?: any, ...optionalParams: any[]): void

Prints to stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3) (the arguments are all passed to util.format()).

const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout

See util.format() for more information.

@sincev0.1.100

log
(
const args: {
[x: string]: any;
_: Array<string | number | boolean>;
}
args
);
// { _: ['build'], bundle: true, r: true, f: true, a: "value", b: "value", c: 1 }

Parsing can be configured to ensure arguments are coerced to specific types, which enhances type safety.

import {
function parse<TArgs extends Values<TBooleans, TStrings, TCollectable, undefined, TDefaults, TAliases>, TBooleans extends BooleanType = undefined, TStrings extends StringType = undefined, TCollectable extends Collectable = undefined, TDefaults extends Record<string, unknown> | undefined = undefined, TAliases extends Aliases<TAliasArgNames, TAliasNames> | undefined = undefined, TAliasArgNames extends string = string, TAliasNames extends string = string>(argv: string[], { default: defaults, alias: aliases, ...types }?: ParseOptions<TBooleans, TStrings, TCollectable, TDefaults, TAliases>): Args<TArgs>
parse
} from "@bomb.sh/args";
const
const args: {
[x: string]: unknown;
foo: boolean;
bar: boolean;
baz?: string;
qux?: string;
input: unknown[];
a: unknown;
b: unknown;
c: unknown;
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, unknown> & AddAliases<Omit<Partial<Record<"baz", string>> & Record<never, never> & Partial<Record<"qux", string>> & {
...;
} & {
...;
}, "a" | ... 1 more ... | "c"> & {
...;
}, {
...;
}>, "foo" | "bar", "baz" | "qux", "input", {
...;
}, {
...;
}, string, string>(argv: string[], { default: defaults, alias: aliases, ...types }?: ParseOptions<...> | undefined): {
...;
}
parse
(
var process: NodeJS.Process
process
.
NodeJS.Process.argv: string[]

The process.argv property returns an array containing the command-line arguments passed when the Node.js process was launched. The first element will be

execPath

. See process.argv0 if access to the original value of argv[0] is needed. The second element will be the path to the JavaScript file being executed. The remaining elements will be any additional command-line arguments.

For example, assuming the following script for process-args.js:

import { argv } from 'node:process';
// print process.argv
argv.forEach((val, index) => {
console.log(`${index}: ${val}`);
});

Launching the Node.js process as:

Terminal window
node process-args.js one two=three four

Would generate the output:

0: /usr/local/bin/node
1: /Users/mjr/work/node/process-args.js
2: one
3: two=three
4: four

@sincev0.1.27

argv
, {
ParseOptions<"foo" | "bar", "baz" | "qux", "input", { a: number; b: number; c: string; }, { h: "help"; }>.default?: {
a: number;
b: number;
c: string;
} & {
[x: string]: unknown;
baz?: unknown;
foo?: unknown;
bar?: unknown;
qux?: unknown;
}

An object mapping string argument names to default values.

default
: {
a: number
a
: 1,
b: number
b
: 2,
c: string
c
: "value" },
ParseOptions<"foo" | "bar", "baz" | "qux", "input", { a: number; b: number; c: string; }, { h: "help"; }>.alias?: {
h: "help";
}

An object mapping string names to strings or arrays of string argument names to use as aliases.

alias
: {
h: "help"
h
: "help" },
ParseOptions<"foo" | "bar", "baz" | "qux", "input", { a: number; b: number; c: string; }, { h: "help"; }>.boolean?: "foo" | "bar" | readonly ("foo" | "bar")[]

A boolean, string or array of strings to always treat as booleans. If true will treat all double hyphenated arguments without equal signs as boolean (e.g. affects --foo, not -f or --foo=bar). All boolean arguments will be set to false by default.

boolean
: ["foo", "bar"],
ParseOptions<"foo" | "bar", "baz" | "qux", "input", { a: number; b: number; c: string; }, { h: "help"; }>.string?: "baz" | "qux" | readonly ("baz" | "qux")[]

A string or array of strings argument names to always treat as strings.

string
: ["baz", "qux"],
ParseOptions<"foo" | "bar", "baz" | "qux", "input", { a: number; b: number; c: string; }, { h: "help"; }>.array?: "input" | readonly "input"[]

A string or array of strings argument names to always treat as arrays. Array options can be used multiple times. All values will be collected into one array. If a non-array option is used multiple times, the last value is used. All Collectable arguments will be set to [] by default.

array
: ["input"],
});

Benchmarks

mri x 1,650,986 ops/sec ±0.32% (97 runs sampled)
@bomb.sh/args x 1,407,191 ops/sec ±0.38% (99 runs sampled)
minimist x 383,506 ops/sec ±0.28% (99 runs sampled)
node:util x 320,953 ops/sec ±0.35% (98 runs sampled)
yargs-parser x 31,874 ops/sec ±1.32% (92 runs sampled)

Acknowledgements

This package was previously published as ultraflag up until v0.3.0, when it was renamed to @bomb.sh/args.

Next Steps

  1. Explore the API Reference for detailed documentation
  2. Join our Discord community for support and discussions

Contributing

We welcome contributions! Please check out our Contributing Guide for details on our code of conduct and the process for submitting pull requests.