Skip to content

Commit

Permalink
Add rest collector (collect everything after stray in arguments to
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Mar 17, 2024
1 parent f8922c4 commit 906dbee
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cross/utils",
"version": "0.5.0",
"version": "0.6.0",
"exports": {
".": "./mod.ts",
"./ansi": "./utils/ansi.ts",
Expand Down
58 changes: 58 additions & 0 deletions utils/args.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ test("Parse arguments using space as separator", () => {
configFile: ["app.config"],
},
loose: [],
rest: "",
});
});

Expand All @@ -26,6 +27,7 @@ test("Parse arguments using equal sign as separator", () => {
arg: ["asd"],
},
loose: [],
rest: "",
});
});

Expand All @@ -39,6 +41,7 @@ test("Handle flags with no values", () => {
debug: [true],
},
loose: [],
rest: "",
});
});

Expand All @@ -51,6 +54,7 @@ test("Handle an argument at the end", () => {
port: ["8080"],
},
loose: ["app.config"],
rest: "",
});
});

Expand All @@ -63,6 +67,7 @@ test("Handle empty arguments", () => {
flag: [""],
},
loose: [],
rest: "",
});
});

Expand All @@ -75,6 +80,7 @@ test("Handle arguments with embedded equals signs", () => {
path: ["/my/path=with/equals"],
},
loose: [],
rest: "",
});
});

Expand All @@ -88,6 +94,7 @@ test("Handle multiple occurrences of a flag", () => {
config: ["prod.config"],
},
loose: [],
rest: "",
});
});

Expand All @@ -111,3 +118,54 @@ test("Test ArgsParser methods", () => {
// Add a method to get loose arguments for completeness (optional)
assertEquals(parser.getLoose(), ["file.txt"]);
});

test("Handle everything after '--' as the rest command", () => {
const cmdArgs = ["--port", "8080", "--", "run", "server", "--debug"];
const parsedArgs = ArgsParser.parseArgs(cmdArgs);

assertEquals(parsedArgs, {
args: {
port: ["8080"],
},
loose: [],
rest: "run server --debug",
});

const parser = new ArgsParser(cmdArgs);
assertEquals(parser.getRest(), "run server --debug");
assertEquals(parser.hasRest(), true);
});

test("Handle multiple '--' delimiters", () => {
const cmdArgs = ["--flag", "--", "start", "--", "build"];
const parsedArgs = ArgsParser.parseArgs(cmdArgs);

assertEquals(parsedArgs, {
args: {
flag: [true],
},
loose: [],
rest: "start -- build",
});

const parser = new ArgsParser(cmdArgs);
assertEquals(parser.getRest(), "start -- build");
assertEquals(parser.hasRest(), true);
});

test("Handle the '--' delimiter at the end", () => {
const cmdArgs = ["--flag", "--"];
const parsedArgs = ArgsParser.parseArgs(cmdArgs);

assertEquals(parsedArgs, {
args: {
flag: [true],
},
loose: [],
rest: "",
});

const parser = new ArgsParser(cmdArgs);
assertEquals(parser.getRest(), "");
assertEquals(parser.hasRest(), false);
});
36 changes: 32 additions & 4 deletions utils/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export function args(all: boolean = false): string[] {
export class ArgsParser {
private readonly parsedArgs: Record<string, (string | boolean)[]>;
private readonly looseArgs: string[];
private readonly restCommand: string = "";

/**
* Parses command-line arguments.
Expand All @@ -58,13 +59,23 @@ export class ArgsParser {
*/
public static parseArgs(
cmdArgs: string[],
): { args: Record<string, (string | boolean)[]>; loose: string[] } {
): {
args: Record<string, (string | boolean)[]>;
loose: string[];
rest: string;
} {
const parsedArgs: Record<string, (string | boolean)[]> = {};
const looseArgs: string[] = [];

let restCommand = "";
let collectingRest = false;
for (let i = 0; i < cmdArgs.length; i++) {
const arg = cmdArgs[i];
if (arg.startsWith("--") || arg.startsWith("-")) {

if (collectingRest) {
restCommand += (restCommand ? " " : "") + arg; // Join with spaces
} else if (arg === "--") {
collectingRest = true;
} else if (arg.startsWith("--") || arg.startsWith("-")) {
const parts = arg.slice(arg.startsWith("--") ? 2 : 1).split("=");
const key = parts[0];
let value: string | boolean = true; // Default to boolean for flags
Expand All @@ -90,13 +101,14 @@ export class ArgsParser {
}
}

return { args: parsedArgs, loose: looseArgs };
return { args: parsedArgs, loose: looseArgs, rest: restCommand };
}

constructor(cmdArgs: string[]) {
const result = ArgsParser.parseArgs(cmdArgs);
this.parsedArgs = result.args;
this.looseArgs = result.loose;
this.restCommand = result.rest;
}

/**
Expand Down Expand Up @@ -146,4 +158,20 @@ export class ArgsParser {
countLoose(): number {
return this.looseArgs.length;
}

/**
* Returns the remaining command portion collected after the "--" delimiter.
* @returns {string} The rest of the command.
*/
getRest(): string {
return this.restCommand;
}

/**
* Checks whether a command portion was collected after the "--" delimiter.
* @returns {boolean} True if a rest command exists, false otherwise.
*/
hasRest(): boolean {
return this.restCommand !== "";
}
}

0 comments on commit 906dbee

Please sign in to comment.