-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support options with only a short flag (#1256)
* Support short flag alone * Weaken option parsing for backwards compatibility * Have .version allow short only flag * Add tests for lone short and long flags
- Loading branch information
1 parent
168ff5b
commit a31bb3f
Showing
6 changed files
with
150 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,66 @@ | ||
const commander = require('../'); | ||
|
||
test('when helpOption has custom flags then custom flag invokes help', () => { | ||
// Optional. Suppress normal output to keep test output clean. | ||
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.helpOption('--custom-help', 'custom help output'); | ||
expect(() => { | ||
program.parse(['node', 'test', '--custom-help']); | ||
}).toThrow('(outputHelp)'); | ||
writeSpy.mockClear(); | ||
}); | ||
describe('helpOption', () => { | ||
let writeSpy; | ||
|
||
beforeAll(() => { | ||
// Optional. Suppress normal output to keep test output clean. | ||
writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { }); | ||
}); | ||
|
||
afterEach(() => { | ||
writeSpy.mockClear(); | ||
}); | ||
|
||
afterAll(() => { | ||
writeSpy.mockRestore(); | ||
}); | ||
|
||
test('when helpOption has custom flags then custom short flag invokes help', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.helpOption('-c,--custom-help', 'custom help output'); | ||
expect(() => { | ||
program.parse(['-c'], { from: 'user' }); | ||
}).toThrow('(outputHelp)'); | ||
}); | ||
|
||
test('when helpOption has custom flags then custom long flag invokes help', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.helpOption('-c,--custom-help', 'custom help output'); | ||
expect(() => { | ||
program.parse(['--custom-help'], { from: 'user' }); | ||
}).toThrow('(outputHelp)'); | ||
}); | ||
|
||
test('when helpOption has just custom short flag then custom short flag invokes help', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.helpOption('-c', 'custom help output'); | ||
expect(() => { | ||
program.parse(['-c'], { from: 'user' }); | ||
}).toThrow('(outputHelp)'); | ||
}); | ||
|
||
test('when helpOption has just custom long flag then custom long flag invokes help', () => { | ||
const program = new commander.Command(); | ||
program | ||
.exitOverride() | ||
.helpOption('--custom-help', 'custom help output'); | ||
expect(() => { | ||
program.parse(['--custom-help'], { from: 'user' }); | ||
}).toThrow('(outputHelp)'); | ||
}); | ||
|
||
test('when helpOption has custom description then helpInformation include custom description', () => { | ||
const program = new commander.Command(); | ||
program | ||
.helpOption('-C,--custom-help', 'custom help output'); | ||
const helpInformation = program.helpInformation(); | ||
expect(helpInformation).toMatch(/-C,--custom-help +custom help output/); | ||
test('when helpOption has custom description then helpInformation include custom description', () => { | ||
const program = new commander.Command(); | ||
program | ||
.helpOption('-C,--custom-help', 'custom help output'); | ||
const helpInformation = program.helpInformation(); | ||
expect(helpInformation).toMatch(/-C,--custom-help +custom help output/); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters