mirror of
https://github.com/oven-sh/bun
synced 2026-02-02 15:08:46 +00:00
docs: add description how to parse the CLI ARGV by the built-in parseArg (#8345)
Signed-off-by: moznion <moznion@mail.moznion.net>
This commit is contained in:
@@ -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" ]
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user