引數向量 (argument vector) 是程式執行時傳遞給程式的引數列表。它可以從 Bun.argv 獲取。
console.log(Bun.argv);
執行此檔案並帶引數將產生以下結果
bun run cli.ts --flag1 --flag2 value[ '/path/to/bun', '/path/to/cli.ts', '--flag1', '--flag2', 'value' ]為了將 argv 解析成更有用的格式,util.parseArgs 會很有幫助。
Example
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);
然後它會輸出
bun run cli.ts --flag1 --flag2 value{
flag1: true,
flag2: "value",
}
[ "/path/to/bun", "/path/to/cli.ts" ]