feature: Adds better command line argument parsing.
This commit is contained in:
parent
ea2fd4bff1
commit
e85838d438
6 changed files with 663 additions and 37 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
24
lom/src/libs/patchCommander.ts
Normal file
24
lom/src/libs/patchCommander.ts
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue