From 710106af8b5381203473842026e39da6e4237d37 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Mon, 20 Nov 2023 21:34:37 -0500 Subject: [PATCH] Fix bug where positional arguments after switch arguments were skipped --- src/lib/options.test.ts | 14 ++++++++++---- src/lib/options.ts | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib/options.test.ts b/src/lib/options.test.ts index 9e72e01..efe9e1f 100644 --- a/src/lib/options.test.ts +++ b/src/lib/options.test.ts @@ -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", ])); }); diff --git a/src/lib/options.ts b/src/lib/options.ts index 410d14b..2935d2c 100644 --- a/src/lib/options.ts +++ b/src/lib/options.ts @@ -146,8 +146,8 @@ function getOptCore

( ]; } 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]); } } }