diff --git a/apps/t3/src/cli.test.ts b/apps/t3/src/cli.test.ts index 8d172fddf5..671d9cbc36 100644 --- a/apps/t3/src/cli.test.ts +++ b/apps/t3/src/cli.test.ts @@ -292,6 +292,17 @@ describe("parseCliOptions", () => { expect(options.launchCwd).toBe(path.resolve("/workspace", "apps/renderer")); }); + it("accepts dash-prefixed cwd values with separate --cwd argument", () => { + const options = parseCliOptions(["--cwd", "-project"], {}, "/workspace"); + expect(options.launchCwd).toBe(path.resolve("/workspace", "-project")); + }); + + it("treats known flags after --cwd as missing values", () => { + expect(() => parseCliOptions(["--cwd", "--help"], {}, "/workspace")).toThrow( + "Missing value for --cwd", + ); + }); + it("throws for unknown arguments", () => { expect(() => parseCliOptions(["--wat"], {}, "/workspace")).toThrow( "Unknown argument: --wat", diff --git a/apps/t3/src/cli.ts b/apps/t3/src/cli.ts index 260c9f5f93..5aa9e6c777 100644 --- a/apps/t3/src/cli.ts +++ b/apps/t3/src/cli.ts @@ -75,9 +75,36 @@ interface StartupErrorShape { message?: unknown; } -function readArgValue(args: string[], index: number, key: string): string { +const KNOWN_CLI_FLAGS = new Set([ + "--help", + "-h", + "--version", + "-v", + "--no-open", + "--backend-port", + "--web-port", + "--cwd", + "--", +]); + +function readArgValue( + args: string[], + index: number, + key: string, + options?: { + allowDashPrefixed?: boolean; + }, +): string { const value = args[index + 1]; - if (!value || value.startsWith("-")) { + if (!value) { + throw new Error(`Missing value for ${key}.`); + } + + const dashPrefixed = value.startsWith("-"); + if (!options?.allowDashPrefixed && dashPrefixed) { + throw new Error(`Missing value for ${key}.`); + } + if (options?.allowDashPrefixed && dashPrefixed && KNOWN_CLI_FLAGS.has(value)) { throw new Error(`Missing value for ${key}.`); } @@ -198,7 +225,11 @@ export function parseCliOptions( } if (arg === "--cwd") { - launchCwd = parseExplicitPath(readArgValue(argv, index, "--cwd"), "--cwd", parserCwd); + launchCwd = parseExplicitPath( + readArgValue(argv, index, "--cwd", { allowDashPrefixed: true }), + "--cwd", + parserCwd, + ); index += 1; continue; }