Skip to content

Args

The @bomb.sh/args package is a <1kB library for parsing CLI flags. Inspired by Deno’s std/cli parseArgs module.

🤏 very small

🍃 very simple

🏃 very fast (beats node:util)

🔏 strongly typed

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 | undefined;
qux?: string | undefined;
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

@since ― v0.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.

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

@param ― end 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), {
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;
}) | undefined

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";
} | undefined

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")[] | undefined

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")[] | undefined

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"[] | undefined

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"],
});
  • The first parameter is the list of CLI arguments to parse. In most cases it is process.argv.slice(2), which drops the Node.js binary and script path from process.argv
  • The second parameter is the (optional) configuration to parse raw CLI parameters

It provides the default value to set if the option is missing

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]: any;
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, any>, undefined, undefined, undefined, {
a: number;
b: number;
c: string;
}, undefined, string, string>(argv: string[], { default: defaults, alias: aliases, ...types }?: ParseOptions<undefined, undefined, undefined, {
a: number;
b: number;
c: string;
}, undefined> | 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

@since ― v0.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.

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

@param ― end 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), {
ParseOptions<undefined, undefined, undefined, { a: number; b: number; c: string; }, undefined>.default?: ({
a: number;
b: number;
c: string;
} & {
[x: string]: unknown;
}) | undefined

An object mapping string argument names to default values.

default
: {
a: number
a
: 1,
b: number
b
: 2,
c: string
c
: "value" },
});

the variable args will be equals to (assuming CLI parameters are my-command --a=27):

args = {
_: [],
a: 27,
b: 2,
c: 'value'
}

It offers an alternative name for an option.
The object key is the alternative name, the value the name used in the parsing result

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]: any;
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, any>, undefined, undefined, undefined, undefined, {
h: "help";
}, string, string>(argv: string[], { default: defaults, alias: aliases, ...types }?: ParseOptions<undefined, undefined, undefined, undefined, {
h: "help";
}> | 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

@since ― v0.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.

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

@param ― end 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), {
ParseOptions<undefined, undefined, undefined, undefined, { h: "help"; }>.alias?: {
h: "help";
} | undefined

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

alias
: {
h: "help"
h
: 'help' },
});

the variable args will be equals to (assuming CLI parameters are my-command -h):

args = {
_: [],
help: true
}

Indicate that an option is flag, and so argument after it is not its value

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;
get: boolean;
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, unknown> & AddAliases<Record<never, never> & {
get: boolean;
} & Record<never, unknown[]> & Record<never, false | unknown[]>, undefined>, "get", undefined, undefined, undefined, undefined, 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

@since ― v0.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.

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

@param ― end 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), {
ParseOptions<"get", undefined, undefined, undefined, undefined>.boolean?: "get" | readonly "get"[] | undefined

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
: ['get'],
});

the variable args will be equals to (assuming CLI parameters are my-command --get http://my-url.com):

args = {
_: ['http://my-url.com'],
get: true
}

Indicate that an option have a value, and so the argument after it is its value (or an empty string is none is available)

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;
get?: string | undefined;
user?: string | undefined;
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, unknown> & AddAliases<Partial<Record<"get", string>> & Record<never, never> & Partial<Record<"user", string>> & {} & Record<...> & Record<...>, undefined>, undefined, "get" | "user", undefined, undefined, undefined, 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

@since ― v0.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.

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

@param ― end 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), {
ParseOptions<undefined, "get" | "user", undefined, undefined, undefined>.string?: "get" | "user" | readonly ("get" | "user")[] | undefined

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

string
: ['get', 'user'],
});

the variable args will be equals to (assuming CLI parameters are my-command --user --get http://my-url.com):

args = {
_: [],
user: '',
get: 'http://my-url.com'
}

Indicate that an option have a value and can be repeated several times

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;
tag: unknown[];
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, unknown> & AddAliases<Record<never, never> & {} & {
tag: unknown[];
}, undefined>, undefined, undefined, "tag", undefined, undefined, 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

@since ― v0.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.

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

@param ― end 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), {
ParseOptions<undefined, undefined, "tag", undefined, undefined>.array?: "tag" | readonly "tag"[] | undefined

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
: ['tag'],
});

the variable args will be equals to (assuming CLI parameters are my-command --tag app:v1 --tag app:latest):

args = {
_: [],
tag: ['app:v1', 'app:latest']
}
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;
zip?: string | undefined;
_: Array<string | number | boolean>;
}
args
=
parse<Record<string, unknown> & AddAliases<Partial<Record<"zip", string>> & Record<never, never> & {} & Record<never, unknown[]> & Record<never, false | unknown[]>, undefined>, undefined, "zip", undefined, undefined, undefined, 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

@since ― v0.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.

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

@param ― end 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), {
ParseOptions<undefined, "zip", undefined, undefined, undefined>.string?: "zip" | readonly "zip"[] | undefined

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

string
: ['zip'],
});

the variable args will be equals to (assuming CLI parameters are my-command build 2 --port 3000 --zip 08 --open true -rf src --db.host=localhost):

args = {
_: ['build', 2],
port: 3000,
zip: '08',
open: true,
r: true,
f: 'src',
db: { host: 'localhost' }
}
  • Values that look like numbers become numbers, including arguments. List an option in string to keep its value as text, as zip does above.
  • true and false become booleans. For an option listed in boolean, any value other than true, such as --open=yes, becomes false.
  • A value wrapped in matching quotes loses them.
  • --name=value and -n=value set a value directly.
  • -rf sets each letter to true. The last letter takes the next argument as its value when it does not start with -, so -rf src sets f to 'src'.
  • A dot in the name nests the value, so --db.host=localhost becomes { db: { host: 'localhost' } }.
  • An option used more than once keeps its last value, unless it is listed in array.

If a boolean option is prefixed by --no- it will parse as a false flag (without the no- prefix)

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]: 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
(
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

@since ― v0.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.

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

@param ― end 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));

the variable args will be equals to (assuming CLI parameters are my-command --no-color):

args = {
_: [],
color: false
}