Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-frogs-dash.md
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")) {
Expand Down
33 changes: 33 additions & 0 deletions test/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down