Documentation Index
Fetch the complete documentation index at: https://bun.zhcndoc.com/llms.txt
Use this file to discover all available pages before exploring further.
参数向量 是运行程序时传递给程序的参数列表。它可以通过 Bun.argv 获取。
cli.ts
运行该文件并传入参数输出如下:
bun run cli.ts --flag1 --flag2 value
[ '/path/to/bun', '/path/to/cli.ts', '--flag1', '--flag2', 'value' ]
要将 argv 解析为更有用的格式,可以使用 util.parseArgs。
示例:
cli.tsimport { 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);
然后输出:
bun run cli.ts --flag1 --flag2 value
{
flag1: true,
flag2: "value",
}
[ "/path/to/bun", "/path/to/cli.ts" ]