···11+---
22+title: Args
33+description: Learn about the Args package and its capabilities
44+---
55+import { Aside } from '@astrojs/starlight/components';
66+77+The `@bomb.sh/args` package is a <1kB library for parsing CLI flags. Inspired by Deno's `std/cli` [`parseArgs`](https://github.com/denoland/std/blob/main/cli/parse_args.ts) module.
88+99+## Features
1010+1111+🤏 very small
1212+1313+🍃 very simple
1414+1515+🏃 very fast (beats [`node:util`](https://nodejs.org/api/util.html#utilparseargsconfig))
1616+1717+🔏 strongly typed
1818+1919+## Parsing the arguments
2020+2121+```ts twoslash
2222+import { parse } from "@bomb.sh/args"
2323+2424+const args = parse(process.argv, {
2525+ default: { a: 1, b: 2, c: "value" },
2626+ alias: { h: "help" },
2727+ boolean: ["foo", "bar"],
2828+ string: ["baz", "qux"],
2929+ array: ["input"],
3030+});
3131+```
3232+3333+- The first parameter is the raw CLI parameters list (in most case, it will be `process.argv`)
3434+- The second parameter is the (optional) configuration to parse raw CLI parameters
3535+3636+<Aside type="note">
3737+ In this documentation,
3838+ the term <var>option</var> indicate a string prefixed by one or two hyphen (`-`),<br />
3939+ the term <var>argument</var> indicate a string that is not prefixed by hyphen:
4040+ - `--long`: is an option (long variant), named `long`
4141+ - `-l`: is an option (short variant), named `l`
4242+ - `hello-world`: is an argument, its value is `hello-world`
4343+4444+ ---
4545+ the term <var>flag</var> indicate an option that take two value: `true` or `false`
4646+</Aside>
4747+4848+### Parse options
4949+5050+#### `default` option
5151+5252+It provides the default value to set if the <var>option</var> is missing
5353+```ts twoslash
5454+import { parse } from "@bomb.sh/args"
5555+5656+const args = parse(process.argv, {
5757+ default: { a: 1, b: 2, c: "value" },
5858+});
5959+```
6060+the variable `args` will be equals to (assuming CLI parameters are `my-command --a=27`):
6161+```js
6262+args = {
6363+ _: [],
6464+ a: 27,
6565+ b: 2,
6666+ c: 'value'
6767+}
6868+```
6969+7070+#### `alias` option
7171+7272+It offers an alternative name for an <var>option</var>.<br />
7373+The object key is the alternative name, the value the name used in the parsing result
7474+7575+```ts twoslash
7676+import { parse } from "@bomb.sh/args"
7777+7878+const args = parse(process.argv, {
7979+ alias: { h: 'help' },
8080+});
8181+```
8282+the variable `args` will be equals to (assuming CLI parameters are `my-command -h`):
8383+```js
8484+args = {
8585+ _: [],
8686+ help: true
8787+}
8888+```
8989+9090+#### `boolean` option
9191+9292+Indicate that an <var>option</var> is <var>flag</var>, and so <var>argument</var> after it **is not** its value
9393+9494+```ts twoslash
9595+import { parse } from "@bomb.sh/args"
9696+9797+const args = parse(process.argv, {
9898+ boolean: ['get'],
9999+});
100100+```
101101+the variable `args` will be equals to (assuming CLI parameters are `my-command --get http://my-url.com`):
102102+```js
103103+args = {
104104+ _: ['http://my-url.com'],
105105+ get: true
106106+}
107107+```
108108+109109+#### `string` option
110110+111111+Indicate that an <var>option</var> have a value, and so the <var>argument</var> after it is its value (or an empty string is none is available)
112112+113113+```ts twoslash
114114+import { parse } from "@bomb.sh/args"
115115+116116+const args = parse(process.argv, {
117117+ string: ['get', 'user'],
118118+});
119119+```
120120+the variable `args` will be equals to (assuming CLI parameters are `my-command --user --get http://my-url.com`):
121121+```js
122122+args = {
123123+ _: [],
124124+ user: '',
125125+ get: 'http://my-url.com'
126126+}
127127+```
128128+129129+#### `array` option
130130+131131+Indicate that an <var>option</var> have a value and can be repeated several times
132132+133133+```ts twoslash
134134+import { parse } from "@bomb.sh/args"
135135+136136+const args = parse(process.argv, {
137137+ array: ['tag'],
138138+});
139139+```
140140+the variable `args` will be equals to (assuming CLI parameters are `my-command --tag app:v1 --tag app:latest`):
141141+```js
142142+args = {
143143+ _: [],
144144+ tag: ['app:v1', 'app:latest']
145145+}
146146+```
147147+148148+### Special <var>option</var>
149149+150150+#### Negation variant
151151+152152+If a boolean <var>option</var> is prefixed by `--no-` it will parse as a `false` <var>flag</var> (without the `no-` prefix)
153153+154154+```ts twoslash
155155+import { parse } from "@bomb.sh/args"
156156+157157+const args = parse(process.argv);
158158+```
159159+the variable `args` will be equals to (assuming CLI parameters are `my-command --no-color`):
160160+```js
161161+args = {
162162+ _: [],
163163+ color: false
164164+}
165165+```
166166+167167+<Aside type="note">
168168+ If the <var>option</var> is defined as a `string` this behavior is ignored
169169+ ```ts twoslash
170170+ import { parse } from "@bomb.sh/args"
171171+172172+ const args = parse(process.argv, { string: ['no-name'] });
173173+ ```
174174+ the variable `args` will be equals to (assuming CLI parameters are `my-command --no-color --no-name John`):
175175+ ```js
176176+ args = {
177177+ _: [],
178178+ 'color': false
179179+ 'no-name': 'John'
180180+ }
181181+ ```
182182+</Aside>
+93
src/content/docs/args/getting-started.mdx
···11+---
22+title: Getting Started
33+description: Learn how to get started with Args
44+---
55+66+import { Tabs, TabItem } from '@astrojs/starlight/components';
77+88+A <1kB library for parsing CLI flags. Inspired by Deno's `std/cli` [`parseArgs`](https://github.com/denoland/std/blob/main/cli/parse_args.ts) module.
99+1010+## Features
1111+1212+🤏 very small
1313+1414+🍃 very simple
1515+1616+🏃 very fast (beats [`node:util`](https://nodejs.org/api/util.html#utilparseargsconfig))
1717+1818+🔏 strongly typed
1919+2020+## Installation
2121+2222+You can install Args using npm, yarn, or pnpm:
2323+2424+<Tabs>
2525+ <TabItem label="npm" icon="seti:npm">
2626+ ```bash
2727+ npm install @bomb.sh/args
2828+ ```
2929+ </TabItem>
3030+ <TabItem label="pnpm" icon="pnpm">
3131+ ```bash
3232+ pnpm add @bomb.sh/args
3333+ ```
3434+ </TabItem>
3535+ <TabItem label="Yarn" icon="seti:yarn">
3636+ ```bash
3737+ yarn add @bomb.sh/args
3838+ ```
3939+ </TabItem>
4040+</Tabs>
4141+4242+4343+## Quick Start
4444+4545+Basic usage does not require any configuration.
4646+4747+```ts twoslash
4848+import { parse } from "@bomb.sh/args";
4949+5050+// my-cli build --bundle -rf --a value --b=value --c 1
5151+const argv = process.argv.slice(2);
5252+const args = parse(argv);
5353+5454+console.log(args);
5555+// { _: ['build'], bundle: true, r: true, f: true, a: "value", b: "value", c: 1 }
5656+```
5757+5858+Parsing can be configured to ensure arguments are coerced to specific types, which enhances type safety.
5959+6060+```ts twoslash
6161+import { parse } from "@bomb.sh/args";
6262+6363+const args = parse(process.argv, {
6464+ default: { a: 1, b: 2, c: "value" },
6565+ alias: { h: "help" },
6666+ boolean: ["foo", "bar"],
6767+ string: ["baz", "qux"],
6868+ array: ["input"],
6969+});
7070+```
7171+7272+## Benchmarks
7373+7474+```
7575+mri x 1,650,986 ops/sec ±0.32% (97 runs sampled)
7676+@bomb.sh/args x 1,407,191 ops/sec ±0.38% (99 runs sampled)
7777+minimist x 383,506 ops/sec ±0.28% (99 runs sampled)
7878+node:util x 320,953 ops/sec ±0.35% (98 runs sampled)
7979+yargs-parser x 31,874 ops/sec ±1.32% (92 runs sampled)
8080+```
8181+8282+## Acknowledgements
8383+8484+This package was previously published as `ultraflag` up until `v0.3.0`, when it was renamed to `@bomb.sh/args`.
8585+8686+## Next Steps
8787+8888+1. Explore the [API Reference](/docs/args/api) for detailed documentation
8989+2. Join our [Discord community](https://bomb.sh/chat) for support and discussions
9090+9191+## Contributing
9292+9393+We welcome contributions! Please check out our [Contributing Guide](/contributing) for details on our code of conduct and the process for submitting pull requests.