Skip to content

Commit ecc7b1e

Browse files
committed
fix: default option values with same names as args would not parse correctly
1 parent 243214d commit ecc7b1e

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,16 +204,18 @@ export class Massarg<Options> {
204204
const value = option.parse!(tempValue, this.data)
205205
this._addOptionToData(option, value)
206206

207-
continue
207+
// continue
208208
}
209209

210210
const command = this._commands.find((o) => o.name === arg || o.aliases?.includes(arg))
211211

212212
if (command) {
213213
if (this._runCommand) {
214-
continue
214+
this.data.extras.push(arg)
215+
// continue
216+
} else {
217+
this._runCommand = command
215218
}
216-
this._runCommand = command
217219
}
218220

219221
if (!option && !command) {

tests/commands.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,28 @@ describe("Commands", () => {
2323
.parse(["c"])
2424
expect(cmd).toHaveBeenCalledTimes(1)
2525
})
26+
27+
test("should consume the first command, then treat the rest as extras", () => {
28+
const cmd = jest.fn()
29+
const opt = jest.fn()
30+
const def = massarg()
31+
.command({
32+
name: "cmd",
33+
aliases: ["c"],
34+
run: cmd,
35+
})
36+
.command({
37+
name: "opt",
38+
aliases: ["o"],
39+
run: opt,
40+
})
41+
42+
const options = def.parseArgs(["cmd", "opt"])
43+
44+
def.parse(["cmd", "opt"])
45+
46+
expect(cmd).toHaveBeenCalledTimes(1)
47+
expect(opt).not.toHaveBeenCalled()
48+
expect(options.extras).toContain("opt")
49+
})
2650
})

0 commit comments

Comments
 (0)