feature: Adds better command line argument parsing.

This commit is contained in:
greysoh 2024-05-06 20:21:10 -04:00
parent ea2fd4bff1
commit e85838d438
No known key found for this signature in database
GPG key ID: FE0F173B8FC01571
6 changed files with 663 additions and 37 deletions

View file

@ -1,32 +1,28 @@
import { parseArgs } from "node:util";
import { Command } from "commander";
import type { Axios } from "axios";
import { type AppState, patchCommander } from "../libs/patchCommander.js";
import type { PrintLine } from "../commands.js";
export async function run(
args: string[],
argv: string[],
println: PrintLine,
axios: Axios,
apiKey: string,
) {
const options = parseArgs({
args,
const program = new Command();
const appState: AppState = {
hasRecievedExitSignal: false
};
strict: false,
allowPositionals: true,
patchCommander(program, appState, println);
options: {
tail: {
type: "boolean",
short: "t",
default: false,
},
},
});
program
.option('-d, --debug', 'output extra debugging')
.option('-s, --small', 'small pizza size')
.option('-p, --pizza-type <type>', 'flavour of pizza');
// Special filtering
const values = options.values;
const positionals = options.positionals
.map(i => (!i.startsWith("-") ? i : ""))
.filter(Boolean);
program.parse(["node", ...argv]);
if (appState.hasRecievedExitSignal) return;
}

View file

@ -78,7 +78,7 @@ server.on("connection", client => {
);
function println(...str: string[]) {
stream.write(format(...str).replace("\n", "\r\n"));
stream.write(format(...str).replaceAll("\n", "\r\n"));
};
while (true) {

View file

@ -0,0 +1,24 @@
// @ts-nocheck
import type { Command } from "commander";
import { PrintLine } from "../commands";
export type AppState = {
hasRecievedExitSignal: boolean
}
export function patchCommander(program: Command, appState: AppState, println: PrintLine) {
program.exitOverride(() => {
appState.hasRecievedExitSignal = true;
});
program._outputConfiguration.writeOut = (str) => println(str);
program._outputConfiguration.writeErr = (str) => {
if (str.includes("--help")) return;
println(str);
};
program._exit = (exitCode, code, message) => {
appState.hasRecievedExitSignal = true;
}
}