-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.ts
More file actions
69 lines (62 loc) · 1.88 KB
/
example.ts
File metadata and controls
69 lines (62 loc) · 1.88 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { Command, parseCommand, ParseCommandError } from "./index";
async function main() {
const root = new Command({
name: "my-app",
usage: "A type-safe CLI example",
prepare(flags) {
// Define flags
const port = flags.uint({
name: "port",
short: "p",
default: 8080,
usage: "port to listen on",
});
const debug = flags.bool({
name: "debug",
short: "d",
usage: "enable verbose logging",
});
// Return the handler directly
// 'port.value' is inferred as 'number'
// 'debug.value' is inferred as 'boolean'
return async (args) => {
if (debug.value) {
console.log(
`Starting server in debug mode on port ${port.value}...`,
);
} else {
console.log(`Starting server on port ${port.value}...`);
}
console.log("Extra arguments:", args);
};
},
});
// Add a Subcommand
root.add({
name: "greet",
usage: "greet a user",
prepare(flags) {
const name = flags.string({
name: "name",
short: "n",
default: "Guest",
usage: "user to greet",
});
return (args) => {
console.log(`Hello, ${name.value}!`);
};
},
});
// Parse and Execute
try {
await parseCommand(Bun.argv.slice(2), root);
} catch (e) {
if (e instanceof ParseCommandError) {
// Automatically handles usage printing on errors
console.log(`${e}`);
} else {
console.error("Runtime Error:", e);
}
}
}
main();