fix: support help option with early quit

This commit is contained in:
2023-12-02 02:05:52 +02:00
committed by Chen Asraf
parent 070eb601d1
commit 185939bd21
2 changed files with 38 additions and 1 deletions

View File

@@ -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

View File

@@ -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,
@@ -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()