mirror of
https://github.com/chenasraf/massarg.git
synced 2026-05-18 01:39:05 +00:00
fix: support help option with early quit
This commit is contained in:
@@ -326,6 +326,12 @@ export class MassargCommand<Args extends ArgsObject = ArgsObject> {
|
|||||||
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
|
if (option.defaultValue !== undefined && _a[option.name] === undefined) {
|
||||||
_args[option.getOutputName() as keyof Args] = option.defaultValue as Args[keyof Args]
|
_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
|
// parse options
|
||||||
|
|||||||
@@ -34,6 +34,15 @@ test('binds command', () => {
|
|||||||
expect(command.commands.find((o) => o.name === 'help')).toBeTruthy()
|
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', () => {
|
test('binds option', () => {
|
||||||
const command = massarg(opts).help({
|
const command = massarg(opts).help({
|
||||||
bindOption: true,
|
bindOption: true,
|
||||||
@@ -43,13 +52,35 @@ test('binds option', () => {
|
|||||||
expect(command.options.find((o) => o.name === 'help')).toBeTruthy()
|
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', () => {
|
test('help string', () => {
|
||||||
const command = massarg(opts)
|
const command = massarg(opts)
|
||||||
expect(command.helpString()).toContain(`Usage:`)
|
expect(command.helpString()).toContain(`Usage:`)
|
||||||
})
|
})
|
||||||
|
|
||||||
test('print help', () => {
|
test('print help', () => {
|
||||||
const log = jest.spyOn(console, 'log').mockImplementation(() => {})
|
const log = jest.spyOn(console, 'log').mockImplementation(() => { })
|
||||||
const command = massarg(opts)
|
const command = massarg(opts)
|
||||||
command.printHelp()
|
command.printHelp()
|
||||||
expect(log).toHaveBeenCalled()
|
expect(log).toHaveBeenCalled()
|
||||||
|
|||||||
Reference in New Issue
Block a user