이제 아래와 같이 CLI 파서를 선언하면:
const parser = or(
object("Group 1", {
type: constant("group1"),
allow: option("-a", "--allow"),
value: option("-v", "--value", integer()),
arg: argument(string({ metavar: "ARG" })),
}),
object("Group 2", {
type: constant("group2"),
foo: option("-f", "--foo"),
bar: option("-b", "--bar", string({ metavar: "VALUE" })),
}),
merge(
object("Group 3", {
type: constant("group34"),
deny: option("-d", "--deny"),
test: option("-t", "--test", integer()),
}),
object("Group 4", {
baz: option("-z", "--baz"),
qux: option("-q", "--qux", string({ metavar: "QUX" })),
}),
),
);
InferValue<typeof parser>
타입 함수가 아래와 같은 타입을 반환한다:
const _:
| {
readonly type: "group1";
readonly allow: boolean;
readonly value: number;
readonly arg: string;
}
| {
readonly type: "group2";
readonly foo: boolean;
readonly bar: string;
}
| {
readonly type: "group34";
readonly deny: boolean;
readonly test: number;
readonly baz: boolean;
readonly qux: string;
};
현재 구현한 파서 프리미티브는 option()
과 argument()
두 개, 그리고 파서 컴비네이터는 or()
, object()
, tuple()
, merge()
, optional()
, multiple()
정도. 그 외에 concat()
같은 것도 구현하고 싶긴 한데, 일단 도움말 먼저 만들어 보자.