-
Notifications
You must be signed in to change notification settings - Fork 1
/
help.ts
50 lines (44 loc) · 1005 Bytes
/
help.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { z } from "./z.ts";
import { flag, FlagConfig } from "./flags.ts";
export const SHOW_HELP = Symbol("SHOW_HELP");
export function helpFlag(config: FlagConfig = {}) {
return flag({ aliases: ["h"], ...config }).ostring()
.refine(
(value) => {
return typeof value === "undefined";
},
{
message: "Show help",
params: {
interrupt: SHOW_HELP,
},
},
);
}
export function showHelp() {
throw new z.ZodError([
{
code: z.ZodIssueCode.custom,
message: "Show help",
path: [],
params: {
interrupt: SHOW_HELP,
},
},
]);
}
export function isHelp(err: unknown): boolean {
if (err instanceof z.ZodError) {
const issue = err.issues.find(
(issue) =>
"params" in issue &&
// @ts-expect-error: blah blah
"interrupt" in issue.params &&
issue.params.interrupt === SHOW_HELP,
);
if (issue) {
return true;
}
}
return false;
}