Fix bug where positional arguments after switch arguments were skipped

This commit is contained in:
Tangent Wantwight 2023-11-20 21:34:37 -05:00
parent 32c17c40f6
commit 710106af8b
2 changed files with 12 additions and 6 deletions

View File

@ -42,15 +42,21 @@ describe("getOpt", () => {
expectOpts("cmd -unsolicited", {}).toEqual([
{ error: "cmd: -unsolicited is not a switch this command knows about" },
]));
it("doesn't count quoted words as switches", () =>
expectOpts('cmd "-purple"', { purple: 0 }).toEqual([
{ purple: false },
"-purple",
]));
it("doesn't count switches as positional arguments", () =>
expectOpts("cmd -yellow banana", { $min: 1, $max: 1, yellow: 0 }).toEqual([
{ yellow: true },
"banana",
]));
it("doesn't count quoted words as switches", () =>
expectOpts('cmd "-purple"', { purple: 0 }).toEqual([
{ purple: false },
"-purple",
it("can mix positional and switch arguments", () =>
expectOpts("cmd early -lunch noon evening", { lunch: 1 }).toEqual([
{ lunch: ["noon"] },
"early",
"evening",
]));
});

View File

@ -146,8 +146,8 @@ function getOptCore<P extends Options>(
];
} else {
const takeUntil = i + switchArgCount;
for (i++; i <= takeUntil; i++) {
(flags[switchName] as TextPiece[]).push(textPieces[i]);
for (; i < takeUntil; i++) {
(flags[switchName] as TextPiece[]).push(textPieces[i + 1]);
}
}
}