Fix bug where positional arguments after switch arguments were skipped
This commit is contained in:
parent
32c17c40f6
commit
710106af8b
2 changed files with 12 additions and 6 deletions
|
@ -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",
|
||||
]));
|
||||
});
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue