adds flag coalescing to argparser

adds handling of inlined args to call tools
fixes terminal layout clearing troubles
This commit is contained in:
2025-04-30 03:06:19 -06:00
parent 65f0b4e0b7
commit 26b7089cc2
5 changed files with 58 additions and 60 deletions

View File

@@ -9,8 +9,11 @@ import { cliAlert, cliLog } from "./prompts.ts";
export class PdfToolsCli {
private tools: Map<string, ITool> = new Map();
private terminalLayout = new TerminalLayout();
closeMessage?: string;
private args = ArgParser.parse(Deno.args);
private args = ArgParser.parse(Deno.args).setFlagDefs({
help: ["-h", "--help"],
});
async importTools(tools?: string) {
tools = tools?.replace(/\/$/, "").replace(/^\.?\//, "") || "tools";
@@ -51,17 +54,22 @@ export class PdfToolsCli {
this.terminalLayout.register("title", titleBlock);
const bodyBlock = new TerminalBlock();
this.terminalLayout.register("body", bodyBlock);
this.embiggenHeader();
if (Deno.args.length === 0) {
// console.log(
// colorize("No tool specified. Importing all tools...", "gray"),
// );
if (this.args.getFlag("help") && !this.args.task) {
await this.help();
return;
} else if (this.args.nonFlags.length === 0 || !this.args.task) {
this.embiggenHeader();
await this.importTools();
await this.toolMenu();
} else {
await this.importTools();
const task = this.args.task;
await this.runTool(toCase(task, "title"));
}
await this.toolMenu();
} finally {
this.terminalLayout.clearAll();
Deno.stdin.setRaw(false);
if (this.closeMessage) console.log(this.closeMessage);
}
}
@@ -95,9 +103,15 @@ export class PdfToolsCli {
const bodyBlock = this.terminalLayout.getBlock("body");
bodyBlock.clearAll();
tool.setBlock?.(bodyBlock);
await tool.run();
await tool.done?.();
this.embiggenHeader();
if (this.args.getFlag("help")) {
await tool.help?.();
} else {
await tool.run(...this.args.taskArgs);
await tool.done?.();
}
await this.embiggenHeader();
} else {
this.closeMessage = "No tool found for " + toolName;
}
}