adds flag coalescing to argparser
adds handling of inlined args to call tools fixes terminal layout clearing troubles
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
export class ArgParser {
|
||||
export class ArgParser<T extends Record<string, string[]>> {
|
||||
private args: string[];
|
||||
private flags: Map<keyof T, boolean> = new Map();
|
||||
|
||||
constructor(args: string[]) {
|
||||
this.args = args;
|
||||
@@ -11,7 +12,22 @@ export class ArgParser {
|
||||
return this.args[index + 1];
|
||||
}
|
||||
|
||||
get flags() {
|
||||
setFlagDefs(flagDefs: T) {
|
||||
for (const [flag, defs] of Object.entries(flagDefs)) {
|
||||
for (const def of defs) {
|
||||
if (this.argFlags.includes(def)) {
|
||||
this.flags.set(flag, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
getFlag(flag: keyof T) {
|
||||
return this.flags.get(flag);
|
||||
}
|
||||
|
||||
get argFlags() {
|
||||
return this.args.filter((arg) => arg.startsWith("-"));
|
||||
}
|
||||
|
||||
@@ -26,6 +42,9 @@ export class ArgParser {
|
||||
get task() {
|
||||
return this.nonFlags[0];
|
||||
}
|
||||
get taskArgs() {
|
||||
return this.nonFlags.slice(1);
|
||||
}
|
||||
|
||||
static parse(args: string[]) {
|
||||
return new ArgParser(args);
|
||||
|
Reference in New Issue
Block a user