diff --git a/astro.config.mjs b/astro.config.mjs index 3822a44..41a8ab6 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -89,6 +89,19 @@ export default defineConfig({ }, { label: "Guides", autogenerate: { directory: "clack/guides" } }, ], + }, + { + label: "Args", + id: "args", + icon: "seti:shell", + link: "/args/getting-started", + items: [ + { label: "Basics", link: "/args/getting-started" }, + { + label: "API", + link: "args/api", + }, + ], } ]), ], diff --git a/package.json b/package.json index f0502d8..803608a 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "@astrojs/starlight": "^0.32.2", + "@bomb.sh/args": "^0.3.1", "@clack/core": "^0.4.1", "@clack/prompts": "^0.10.0", "@types/node": "^22.13.11", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b61c78f..d79f914 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ importers: '@astrojs/starlight': specifier: ^0.32.2 version: 0.32.4(astro@5.5.4(@types/node@22.13.11)(rollup@4.36.0)(typescript@5.8.2)) + '@bomb.sh/args': + specifier: ^0.3.1 + version: 0.3.1 '@clack/core': specifier: ^0.4.1 version: 0.4.1 @@ -87,6 +90,9 @@ packages: resolution: {integrity: sha512-emqcG3vHrpxUKTrxcblR36dcrcoRDvKmnL/dCL6ZsHaShW80qxCAcNhzQZrpeM765VzEos+xOi4s+r4IXzTwdQ==} engines: {node: '>=6.9.0'} + '@bomb.sh/args@0.3.1': + resolution: {integrity: sha512-CwxKrfgcorUPP6KfYD59aRdBYWBTsfsxT+GmoLVnKo5Tmyoqbpo0UNcjngRMyU+6tiPbd18RuIYxhgAn44wU/Q==} + '@clack/core@0.4.1': resolution: {integrity: sha512-Pxhij4UXg8KSr7rPek6Zowm+5M22rbd2g1nfojHJkxp5YkFqiZ2+YLEM/XGVIzvGOcM0nqjIFxrpDwWRZYWYjA==} @@ -1891,6 +1897,8 @@ snapshots: '@babel/helper-string-parser': 7.25.9 '@babel/helper-validator-identifier': 7.25.9 + '@bomb.sh/args@0.3.1': {} + '@clack/core@0.4.1': dependencies: picocolors: 1.1.1 diff --git a/src/content/docs/args/api.mdx b/src/content/docs/args/api.mdx new file mode 100644 index 0000000..fb83d36 --- /dev/null +++ b/src/content/docs/args/api.mdx @@ -0,0 +1,182 @@ +--- +title: Args +description: Learn about the Args package and its capabilities +--- +import { Aside } from '@astrojs/starlight/components'; + +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. + +## Features + +🤏 very small + +🍃 very simple + +🏃 very fast (beats [`node:util`](https://nodejs.org/api/util.html#utilparseargsconfig)) + +🔏 strongly typed + +## Parsing the arguments + +```ts twoslash +import { parse } from "@bomb.sh/args" + +const args = parse(process.argv, { + default: { a: 1, b: 2, c: "value" }, + alias: { h: "help" }, + boolean: ["foo", "bar"], + string: ["baz", "qux"], + 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 + + + +### Parse options + +#### `default` option + +It provides the default value to set if the option is missing +```ts twoslash +import { parse } from "@bomb.sh/args" + +const args = parse(process.argv, { + default: { a: 1, b: 2, c: "value" }, +}); +``` +the variable `args` will be equals to (assuming CLI parameters are `my-command --a=27`): +```js +args = { + _: [], + a: 27, + b: 2, + c: 'value' +} +``` + +#### `alias` option + +It offers an alternative name for an option.
+The object key is the alternative name, the value the name used in the parsing result + +```ts twoslash +import { parse } from "@bomb.sh/args" + +const args = parse(process.argv, { + alias: { h: 'help' }, +}); +``` +the variable `args` will be equals to (assuming CLI parameters are `my-command -h`): +```js +args = { + _: [], + help: true +} +``` + +#### `boolean` option + +Indicate that an option is flag, and so argument after it **is not** its value + +```ts twoslash +import { parse } from "@bomb.sh/args" + +const args = parse(process.argv, { + boolean: ['get'], +}); +``` +the variable `args` will be equals to (assuming CLI parameters are `my-command --get http://my-url.com`): +```js +args = { + _: ['http://my-url.com'], + get: true +} +``` + +#### `string` option + +Indicate that an option have a value, and so the argument after it is its value (or an empty string is none is available) + +```ts twoslash +import { parse } from "@bomb.sh/args" + +const args = parse(process.argv, { + string: ['get', 'user'], +}); +``` +the variable `args` will be equals to (assuming CLI parameters are `my-command --user --get http://my-url.com`): +```js +args = { + _: [], + user: '', + get: 'http://my-url.com' +} +``` + +#### `array` option + +Indicate that an option have a value and can be repeated several times + +```ts twoslash +import { parse } from "@bomb.sh/args" + +const args = parse(process.argv, { + array: ['tag'], +}); +``` +the variable `args` will be equals to (assuming CLI parameters are `my-command --tag app:v1 --tag app:latest`): +```js +args = { + _: [], + tag: ['app:v1', 'app:latest'] +} +``` + +### Special option + +#### Negation variant + +If a boolean option is prefixed by `--no-` it will parse as a `false` flag (without the `no-` prefix) + +```ts twoslash +import { parse } from "@bomb.sh/args" + +const args = parse(process.argv); +``` +the variable `args` will be equals to (assuming CLI parameters are `my-command --no-color`): +```js +args = { + _: [], + color: false +} +``` + + diff --git a/src/content/docs/args/getting-started.mdx b/src/content/docs/args/getting-started.mdx new file mode 100644 index 0000000..d8639ad --- /dev/null +++ b/src/content/docs/args/getting-started.mdx @@ -0,0 +1,93 @@ +--- +title: Getting Started +description: Learn how to get started with Args +--- + +import { Tabs, TabItem } from '@astrojs/starlight/components'; + +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. + +## Features + +🤏 very small + +🍃 very simple + +🏃 very fast (beats [`node:util`](https://nodejs.org/api/util.html#utilparseargsconfig)) + +🔏 strongly typed + +## Installation + +You can install Args using npm, yarn, or pnpm: + + + + ```bash + npm install @bomb.sh/args + ``` + + + ```bash + pnpm add @bomb.sh/args + ``` + + + ```bash + yarn add @bomb.sh/args + ``` + + + + +## Quick Start + +Basic usage does not require any configuration. + +```ts twoslash +import { parse } from "@bomb.sh/args"; + +// my-cli build --bundle -rf --a value --b=value --c 1 +const argv = process.argv.slice(2); +const args = parse(argv); + +console.log(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. + +```ts twoslash +import { parse } from "@bomb.sh/args"; + +const args = parse(process.argv, { + default: { a: 1, b: 2, c: "value" }, + alias: { h: "help" }, + boolean: ["foo", "bar"], + string: ["baz", "qux"], + 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](/docs/args/api) for detailed documentation +2. Join our [Discord community](https://bomb.sh/chat) for support and discussions + +## Contributing + +We welcome contributions! Please check out our [Contributing Guide](/contributing) for details on our code of conduct and the process for submitting pull requests.