Skip to content

Commit 742b597

Browse files
committed
fix: support help option with early quit
1 parent 0f317d8 commit 742b597

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/command.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,12 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
326326
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
327327
_args[option.getOutputName() as keyof Args] = option.defaultValue as Args[keyof Args]
328328
}
329+
if (this.helpConfig.bindOption && option.name === 'help') {
330+
if (parseCommands) {
331+
this.printHelp()
332+
}
333+
return
334+
}
329335
}
330336

331337
// parse options

test/help.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ test('binds command', () => {
3434
expect(command.commands.find((o) => o.name === 'help')).toBeTruthy()
3535
})
3636

37+
test('prints help from command', () => {
38+
const command = massarg(opts).help({
39+
bindCommand: true,
40+
})
41+
const log = jest.spyOn(console, 'log').mockImplementation(() => { })
42+
command.parse(['help'])
43+
expect(log).toHaveBeenCalled()
44+
})
45+
3746
test('binds option', () => {
3847
const command = massarg(opts).help({
3948
bindOption: true,
@@ -43,13 +52,35 @@ test('binds option', () => {
4352
expect(command.options.find((o) => o.name === 'help')).toBeTruthy()
4453
})
4554

55+
describe('prints help from option', () => {
56+
test('when no main command', () => {
57+
const command = massarg(opts).help({
58+
bindOption: true,
59+
})
60+
const log = jest.spyOn(console, 'log').mockImplementation(() => { })
61+
command.parse(['--help'])
62+
expect(log).toHaveBeenCalled()
63+
})
64+
65+
test('when main command', () => {
66+
const mainCmd = jest.fn()
67+
const command2 = massarg(opts)
68+
.help({
69+
bindOption: true,
70+
})
71+
.main(mainCmd)
72+
command2.parse(['--help'])
73+
expect(mainCmd).not.toHaveBeenCalled()
74+
})
75+
})
76+
4677
test('help string', () => {
4778
const command = massarg(opts)
4879
expect(command.helpString()).toContain(`Usage:`)
4980
})
5081

5182
test('print help', () => {
52-
const log = jest.spyOn(console, 'log').mockImplementation(() => {})
83+
const log = jest.spyOn(console, 'log').mockImplementation(() => { })
5384
const command = massarg(opts)
5485
command.printHelp()
5586
expect(log).toHaveBeenCalled()

0 commit comments

Comments
 (0)