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;
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"],
});
  • The first parameter is the raw CLI parameters list (in most case, it will be 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

@sincev0.1.27

argv
, {
ParseOptions<undefined, undefined, undefined, { a: number; b: number; c: string; }, undefined>.default?: {
a: number;
b: number;
c: string;
} & {
[x: string]: unknown;
}

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

@sincev0.1.27

argv
, {
ParseOptions<undefined, undefined, undefined, undefined, { 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' },
});

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

@sincev0.1.27

argv
, {
ParseOptions<"get", undefined, undefined, undefined, undefined>.boolean?: "get" | readonly "get"[]

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;
user?: string;
_: 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

@sincev0.1.27

argv
, {
ParseOptions<undefined, "get" | "user", undefined, undefined, undefined>.string?: "get" | "user" | readonly ("get" | "user")[]

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

@sincev0.1.27

argv
, {
ParseOptions<undefined, undefined, "tag", undefined, undefined>.array?: "tag" | readonly "tag"[]

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']
}

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

@sincev0.1.27

argv
);

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

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