Skip to content

Commit 158e33a

Browse files
committed
fix: default option values with same names as args would not parse correctly (2)
1 parent dfabba0 commit 158e33a

3 files changed

Lines changed: 68 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "massarg",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "Flexible, powerful, and simple command/argument parser for CLI applications",
55
"keywords": [
66
"shell",

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,16 @@ export class Massarg<Options> {
209209

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

212+
let justFoundCommand = false
213+
212214
if (command) {
213-
if (this._runCommand) {
214-
this.data.extras.push(arg)
215-
// continue
216-
} else {
215+
if (!this._runCommand) {
217216
this._runCommand = command
217+
justFoundCommand = true
218218
}
219219
}
220220

221-
if (!option && !command) {
221+
if (!option && (!command || (command && !justFoundCommand))) {
222222
const defOpts = this._options.filter((o) => o.isDefault)
223223
if (defOpts.length) {
224224
for (const option of defOpts) {

tests/commands.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,66 @@ describe("Commands", () => {
4747
expect(opt).not.toHaveBeenCalled()
4848
expect(options.extras).toContain("opt")
4949
})
50+
51+
test("should consume the first command, then pass to default", () => {
52+
const cmd1 = jest.fn()
53+
const cmd2 = jest.fn()
54+
const def = massarg<{ opt: string }>()
55+
.command({
56+
name: "cmd1",
57+
aliases: ["c1"],
58+
run: cmd1,
59+
})
60+
.command({
61+
name: "cmd2",
62+
aliases: ["c2"],
63+
run: cmd2,
64+
})
65+
.option({
66+
name: "opt",
67+
isDefault: true,
68+
aliases: ["o"],
69+
commands: ["c1"],
70+
required: true,
71+
})
72+
73+
const options = def.parseArgs(["cmd1", "opt"])
74+
75+
def.parse(["cmd1", "opt"])
76+
77+
expect(cmd1).toHaveBeenCalledTimes(1)
78+
expect(cmd2).not.toHaveBeenCalled()
79+
expect(options.opt).toBe("opt")
80+
})
81+
82+
test("should consume the first command, then pass to default when option name matches command name", () => {
83+
const cmd1 = jest.fn()
84+
const opt = jest.fn()
85+
const def = massarg<{ opt: string }>()
86+
.command({
87+
name: "cmd1",
88+
aliases: ["c1"],
89+
run: cmd1,
90+
})
91+
.command({
92+
name: "opt",
93+
aliases: ["o"],
94+
run: opt,
95+
})
96+
.option({
97+
name: "opt",
98+
isDefault: true,
99+
aliases: ["o"],
100+
commands: ["c1"],
101+
required: true,
102+
})
103+
104+
const options = def.parseArgs(["cmd1", "opt"])
105+
106+
def.parse(["cmd1", "opt"])
107+
108+
expect(cmd1).toHaveBeenCalledTimes(1)
109+
expect(opt).not.toHaveBeenCalled()
110+
expect(options.opt).toBe("opt")
111+
})
50112
})

0 commit comments

Comments
 (0)