Skip to content

Commit 459c130

Browse files
committed
feat: fixes cursor comment add boolean flags parameter
1 parent 5533364 commit 459c130

4 files changed

Lines changed: 52 additions & 4 deletions

File tree

‎packages/shared/src/cliArgs.test.ts‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from "vitest";
22

3-
import { parseCliArgs } from "./cliArgs.ts";
3+
import { parseCliArgs } from "./cliArgs";
44

55
describe("parseCliArgs", () => {
66
it("returns empty result for empty string", () => {
@@ -102,4 +102,33 @@ describe("parseCliArgs", () => {
102102
it("ignores bare -- with no flag name", () => {
103103
expect(parseCliArgs("--")).toEqual({ flags: {}, positionals: [] });
104104
});
105+
106+
it("boolean flag does not consume next token as value", () => {
107+
expect(parseCliArgs(["--github-output", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
108+
{
109+
flags: { "github-output": null },
110+
positionals: ["1.2.3"],
111+
},
112+
);
113+
});
114+
115+
it("non-boolean flag still consumes next token", () => {
116+
expect(parseCliArgs(["--root", "/path", "1.2.3"], { booleanFlags: ["github-output"] })).toEqual(
117+
{
118+
flags: { root: "/path" },
119+
positionals: ["1.2.3"],
120+
},
121+
);
122+
});
123+
124+
it("mixes boolean and value flags with positionals", () => {
125+
expect(
126+
parseCliArgs(["--github-output", "--root", "/path", "1.2.3"], {
127+
booleanFlags: ["github-output"],
128+
}),
129+
).toEqual({
130+
flags: { "github-output": null, root: "/path" },
131+
positionals: ["1.2.3"],
132+
});
133+
});
105134
});

‎packages/shared/src/cliArgs.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ export interface ParsedCliArgs {
33
readonly positionals: string[];
44
}
55

6+
export interface ParseCliArgsOptions {
7+
readonly booleanFlags?: readonly string[];
8+
}
9+
610
/**
711
* Parse CLI-style arguments into flags and positionals.
812
*
@@ -21,12 +25,13 @@ export interface ParsedCliArgs {
2125
* parseCliArgs("--effort=high")
2226
* → { flags: { effort: "high" }, positionals: [] }
2327
*
24-
* parseCliArgs(["1.2.3", "--root", "/path", "--github-output"])
28+
* parseCliArgs(["1.2.3", "--root", "/path", "--github-output"], { booleanFlags: ["github-output"] })
2529
* → { flags: { root: "/path", "github-output": null }, positionals: ["1.2.3"] }
2630
*/
27-
export function parseCliArgs(args: string | readonly string[]): ParsedCliArgs {
31+
export function parseCliArgs(args: string | readonly string[], options?: ParseCliArgsOptions): ParsedCliArgs {
2832
const tokens =
2933
typeof args === "string" ? args.trim().split(/\s+/).filter(Boolean) : Array.from(args);
34+
const booleanSet = options?.booleanFlags ? new Set(options.booleanFlags) : undefined;
3035

3136
const flags: Record<string, string | null> = {};
3237
const positionals: string[] = [];
@@ -45,6 +50,12 @@ export function parseCliArgs(args: string | readonly string[]): ParsedCliArgs {
4550
continue;
4651
}
4752

53+
// Known boolean flag — never consumes next token
54+
if (booleanSet?.has(rest)) {
55+
flags[rest] = null;
56+
continue;
57+
}
58+
4859
// Handle --key value or --flag (boolean)
4960
const next = tokens[i + 1];
5061
if (next !== undefined && !next.startsWith("--")) {

‎scripts/update-release-package-versions.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ describe("parseArgs", () => {
6060
it("throws on --root without value", () => {
6161
expect(() => parseArgs(["1.2.3", "--root"])).toThrow("Missing value for --root.");
6262
});
63+
64+
it("does not consume version as --github-output value", () => {
65+
expect(parseArgs(["--github-output", "1.2.3"])).toEqual({
66+
version: "1.2.3",
67+
rootDir: undefined,
68+
writeGithubOutput: true,
69+
});
70+
});
6371
});

‎scripts/update-release-package-versions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function parseArgs(argv: ReadonlyArray<string>): {
4747
rootDir: string | undefined;
4848
writeGithubOutput: boolean;
4949
} {
50-
const { flags, positionals } = parseCliArgs(argv);
50+
const { flags, positionals } = parseCliArgs(argv, { booleanFlags: ["github-output"] });
5151

5252
const unknownFlags = Object.keys(flags).filter((k) => k !== "github-output" && k !== "root");
5353
if (unknownFlags.length > 0) {

0 commit comments

Comments
 (0)