Skip to content

Commit

Permalink
fix: support help option with early quit
Browse files Browse the repository at this point in the history
  • Loading branch information
chenasraf committed Dec 2, 2023
1 parent 0f317d8 commit 742b597
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
_args[option.getOutputName() as keyof Args] = option.defaultValue as Args[keyof Args]
}
if (this.helpConfig.bindOption && option.name === 'help') {
if (parseCommands) {
this.printHelp()
}
return
}
}

// parse options
Expand Down
33 changes: 32 additions & 1 deletion test/help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ test('binds command', () => {
expect(command.commands.find((o) => o.name === 'help')).toBeTruthy()
})

test('prints help from command', () => {
const command = massarg(opts).help({
bindCommand: true,
})
const log = jest.spyOn(console, 'log').mockImplementation(() => { })
command.parse(['help'])
expect(log).toHaveBeenCalled()
})

test('binds option', () => {
const command = massarg(opts).help({
bindOption: true,
Expand All @@ -43,13 +52,35 @@ test('binds option', () => {
expect(command.options.find((o) => o.name === 'help')).toBeTruthy()
})

describe('prints help from option', () => {
test('when no main command', () => {
const command = massarg(opts).help({
bindOption: true,
})
const log = jest.spyOn(console, 'log').mockImplementation(() => { })
command.parse(['--help'])
expect(log).toHaveBeenCalled()
})

test('when main command', () => {
const mainCmd = jest.fn()
const command2 = massarg(opts)
.help({
bindOption: true,
})
.main(mainCmd)
command2.parse(['--help'])
expect(mainCmd).not.toHaveBeenCalled()
})
})

test('help string', () => {
const command = massarg(opts)
expect(command.helpString()).toContain(`Usage:`)
})

test('print help', () => {
const log = jest.spyOn(console, 'log').mockImplementation(() => {})
const log = jest.spyOn(console, 'log').mockImplementation(() => { })
const command = massarg(opts)
command.printHelp()
expect(log).toHaveBeenCalled()
Expand Down

0 comments on commit 742b597

Please sign in to comment.