56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { parseArgs } from "node:util";
|
|
|
|
const { values } = parseArgs({
|
|
options: {
|
|
listen: {
|
|
type: "string",
|
|
short: "l",
|
|
},
|
|
"log-address": {
|
|
type: "string",
|
|
default: "8085",
|
|
},
|
|
},
|
|
strict: true,
|
|
allowPositionals: false,
|
|
});
|
|
|
|
function parseListenAddress(listen: string | undefined): {
|
|
host: string;
|
|
port: number;
|
|
} {
|
|
const defaultHost = "127.0.0.1";
|
|
const defaultPort = 3500;
|
|
|
|
if (!listen) {
|
|
return { host: defaultHost, port: defaultPort };
|
|
}
|
|
|
|
const lastColon = listen.lastIndexOf(":");
|
|
if (lastColon === -1) {
|
|
// Just a port number
|
|
const port = parseInt(listen, 10);
|
|
if (Number.isNaN(port)) {
|
|
throw new Error(`Invalid listen address: ${listen}`);
|
|
}
|
|
return { host: defaultHost, port };
|
|
}
|
|
|
|
const host = listen.slice(0, lastColon);
|
|
const port = parseInt(listen.slice(lastColon + 1), 10);
|
|
|
|
if (Number.isNaN(port)) {
|
|
throw new Error(`Invalid port in listen address: ${listen}`);
|
|
}
|
|
|
|
return { host, port };
|
|
}
|
|
|
|
const listenAddress = parseListenAddress(values.listen);
|
|
const logAddress = parseListenAddress(values["log-address"]);
|
|
|
|
export const cli = {
|
|
listen: listenAddress,
|
|
logAddress,
|
|
};
|