diff --git a/.changeset/calm-frogs-dash.md b/.changeset/calm-frogs-dash.md new file mode 100644 index 0000000..117bf9d --- /dev/null +++ b/.changeset/calm-frogs-dash.md @@ -0,0 +1,5 @@ +--- +"@bomb.sh/args": minor +--- + +Support positional arguments after a bare `--` terminator, including values that start with `-` or `--` ([#12](https://github.com/bombshell-dev/args/issues/12)) diff --git a/src/index.ts b/src/index.ts index 524e527..c9953be 100644 --- a/src/index.ts +++ b/src/index.ts @@ -101,6 +101,14 @@ export function parse< let key = ""; let value: string | undefined; + // Everything after a bare `--` is positional, even if it looks like a flag. + if (curr === "--") { + for (let j = i + 1; j < argv.length; j++) { + (obj as any)._.push(coerce(argv[j])); + } + break; + } + if (curr.length > 1 && curr[0] === "-") { if (curr[1] !== "-" && curr.length > 2 && !curr.includes("=")) { if (curr.includes(".")) { diff --git a/test/flags.test.ts b/test/flags.test.ts index 61e01d2..0cefcb7 100644 --- a/test/flags.test.ts +++ b/test/flags.test.ts @@ -258,6 +258,39 @@ describe("special cases", () => { }); }); + +describe("double dash terminator", () => { + it("treats arguments after -- as positionals even with leading dashes", () => { + const input = ["--", "-pos"]; + const output = { _: ["-pos"] }; + expect(parse(input)).toEqual(output); + }); + + it("supports positionals starting with double dashes after --", () => { + const input = ["--", "--pos"]; + const output = { _: ["--pos"] }; + expect(parse(input)).toEqual(output); + }); + + it("keeps flags before -- and positionals after", () => { + const input = ["-a", "1", "--", "-b", "--c", "plain"]; + const output = { _: ["-b", "--c", "plain"], a: 1 }; + expect(parse(input)).toEqual(output); + }); + + it("does not include the -- terminator itself in positionals", () => { + const input = ["foo", "--"]; + const output = { _: ["foo"] }; + expect(parse(input)).toEqual(output); + }); + + it("treats a later -- after the terminator as a positional", () => { + const input = ["--", "a", "--", "b"]; + const output = { _: ["a", "--", "b"] }; + expect(parse(input)).toEqual(output); + }); +}); + describe("boolean flags", () => { it("should handle long-form boolean flags correctly", () => { const input = ["--add"];