Skip to content

Commit 1c3adc2

Browse files
committed
feat: Add CLI argument parsing and refactor main entry point
1 parent 1e5b54e commit 1c3adc2

2 files changed

Lines changed: 43 additions & 32 deletions

File tree

src/lib/cli.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { defineCommand, parseArgs, showUsage, type ArgsDef } from "citty"
2+
3+
const args = {
4+
help: {
5+
alias: "h",
6+
type: "boolean",
7+
default: false,
8+
description: "Show this help message",
9+
},
10+
stream: {
11+
alias: "s",
12+
type: "boolean",
13+
default: false,
14+
description: "Enable streaming response for chat completions",
15+
},
16+
port: {
17+
alias: "p",
18+
type: "string",
19+
default: "4141",
20+
description: "Port to listen on",
21+
},
22+
} satisfies ArgsDef
23+
24+
export interface CliOptions {
25+
help: boolean
26+
stream: boolean
27+
port: string
28+
}
29+
30+
export async function parseCli() {
31+
const command = defineCommand({ args })
32+
const options = parseArgs<typeof args>(Bun.argv, args)
33+
34+
if (options.help) {
35+
await showUsage(command)
36+
process.exit()
37+
}
38+
39+
return options
40+
}

src/main.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
import type { Serve } from "bun"
22

3-
import { defineCommand, parseArgs, showUsage, type ArgsDef } from "citty"
4-
3+
import { parseCli } from "./lib/cli"
54
import { initialize } from "./lib/initialization"
65
import { server } from "./server"
76

8-
const args: ArgsDef = {
9-
help: {
10-
alias: "h",
11-
type: "boolean",
12-
default: false,
13-
description: "Show this help message",
14-
},
15-
stream: {
16-
alias: "s",
17-
type: "boolean",
18-
default: false,
19-
description: "Enable streaming response for chat completions",
20-
},
21-
port: {
22-
alias: "p",
23-
type: "string",
24-
default: "4141",
25-
description: "Port to listen on",
26-
},
27-
}
28-
29-
const command = defineCommand({ args })
30-
const options = parseArgs(Bun.argv, args)
31-
32-
if (options.help) {
33-
await showUsage(command)
34-
process.exit()
35-
}
36-
7+
const options = await parseCli()
378
await initialize()
389

3910
export default {
4011
fetch: server.fetch,
41-
port: parseInt(options.port as string),
12+
port: parseInt(options.port, 10),
4213
} satisfies Serve

0 commit comments

Comments
 (0)