From c577c64aa705db8e699351b079d3d2821d40c01f Mon Sep 17 00:00:00 2001 From: moznion Date: Sun, 21 Jan 2024 19:54:49 -0800 Subject: [PATCH] docs: add description how to parse the CLI ARGV by the built-in `parseArg` (#8345) Signed-off-by: moznion --- docs/guides/process/argv.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/docs/guides/process/argv.md b/docs/guides/process/argv.md index f9f3c77e48..7804f5b621 100644 --- a/docs/guides/process/argv.md +++ b/docs/guides/process/argv.md @@ -19,4 +19,39 @@ $ bun run cli.tsx --flag1 --flag2 value --- -To parse `argv` into a more useful format, consider using [minimist](https://github.com/minimistjs/minimist) or [commander](https://github.com/tj/commander.js). +To parse `argv` into a more useful format, `util.parseArgs` would be helpful. + +Example: + +```ts#cli.ts +import { parseArgs } from "util"; + +const { values, positionals } = parseArgs({ + args: Bun.argv, + options: { + flag1: { + type: 'boolean', + }, + flag2: { + type: 'string', + }, + }, + strict: true, + allowPositionals: true, +}); + +console.log(values); +console.log(positionals); +``` + +then it outputs + +``` +$ bun run cli.tsx --flag1 --flag2 value +{ + flag1: true, + flag2: "value", +} +[ "/path/to/bun", "/path/to/cli.ts" ] +``` +