Skip to content

인수 벡터 는 프로그램을 실행할 때 프로그램에 전달된 인수 목록입니다. Bun.argv 로 사용할 수 있습니다.

ts
console.log(Bun.argv);

인수로 이 파일을 실행하면 다음과 같습니다.

sh
bun run cli.ts --flag1 --flag2 value
txt
[ '/path/to/bun', '/path/to/cli.ts', '--flag1', '--flag2', 'value' ]

argv 를 더 유용한 형식으로 파싱하려면 util.parseArgs 가 유용합니다.

예시:

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);

그러면 출력이 다음과 같습니다.

sh
bun run cli.ts --flag1 --flag2 value
txt
{
  flag1: true,
  flag2: "value",
}
[ "/path/to/bun", "/path/to/cli.ts" ]

Bun by www.bunjs.com.cn 편집