Skip to content

Commit

Permalink
Remove support for shadowing help flags
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed Dec 16, 2023
1 parent 1d0fe63 commit 4fb4747
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
11 changes: 1 addition & 10 deletions lib/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,7 @@ class Help {
// Built-in help option.
const helpOption = cmd._getHelpOption();
if (helpOption && !helpOption.hidden) {
// Automatically hide conflicting flags. (Bit magical and messy, but preserve for now!)
const removeShort = helpOption.short && cmd._findOption(helpOption.short);
const removeLong = helpOption.long && cmd._findOption(helpOption.long);
if (!removeShort && !removeLong) {
visibleOptions.push(helpOption); // no changes needed
} else if (helpOption.long && !removeLong) {
visibleOptions.push(cmd.createOption(helpOption.long, helpOption.description));
} else if (helpOption.short && !removeShort) {
visibleOptions.push(cmd.createOption(helpOption.short, helpOption.description));
}
visibleOptions.push(helpOption);
}
if (this.sortOptions) {
visibleOptions.sort(this.compareOptions);
Expand Down
30 changes: 15 additions & 15 deletions tests/command.help.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,21 @@ test('when addCommand with hidden:true then not displayed in helpInformation', (
expect(helpInformation).not.toMatch('secret');
});

test('when help short flag masked then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.option('-h, --host', 'select host');
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch(/\W-h\W.*display help/);
});

test('when both help flags masked then not displayed in helpInformation', () => {
const program = new commander.Command();
program
.option('-h, --help', 'custom');
const helpInformation = program.helpInformation();
expect(helpInformation).not.toMatch('display help');
});
// test('when help short flag masked then not displayed in helpInformation', () => {
// const program = new commander.Command();
// program
// .option('-h, --host', 'select host');
// const helpInformation = program.helpInformation();
// expect(helpInformation).not.toMatch(/\W-h\W.*display help/);
// });

// test('when both help flags masked then not displayed in helpInformation', () => {
// const program = new commander.Command();
// program
// .option('-h, --help', 'custom');
// const helpInformation = program.helpInformation();
// expect(helpInformation).not.toMatch('display help');
// });

test('when call .help then output on stdout', () => {
const writeSpy = jest.spyOn(process.stdout, 'write').mockImplementation(() => { });
Expand Down
40 changes: 20 additions & 20 deletions tests/help.visibleOptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,26 @@ describe('implicit help', () => {
expect(helper.optionTerm(implicitHelp)).toEqual('-h, --help');
});

test('when short flag obscured then help term is --help', () => {
const program = new commander.Command();
program.addOption(new commander.Option('-h, --huge').hideHelp());
const helper = new commander.Help();
const implicitHelp = helper.visibleOptions(program)[0];
expect(helper.optionTerm(implicitHelp)).toEqual('--help');
});
// test('when short flag obscured then help term is --help', () => {
// const program = new commander.Command();
// program.addOption(new commander.Option('-h, --huge').hideHelp());
// const helper = new commander.Help();
// const implicitHelp = helper.visibleOptions(program)[0];
// expect(helper.optionTerm(implicitHelp)).toEqual('--help');
// });

test('when long flag obscured then help term is --h', () => {
const program = new commander.Command();
program.addOption(new commander.Option('-H, --help').hideHelp());
const helper = new commander.Help();
const implicitHelp = helper.visibleOptions(program)[0];
expect(helper.optionTerm(implicitHelp)).toEqual('-h');
});
// test('when long flag obscured then help term is --h', () => {
// const program = new commander.Command();
// program.addOption(new commander.Option('-H, --help').hideHelp());
// const helper = new commander.Help();
// const implicitHelp = helper.visibleOptions(program)[0];
// expect(helper.optionTerm(implicitHelp)).toEqual('-h');
// });

test('when help flags obscured then implicit help hidden', () => {
const program = new commander.Command();
program.addOption(new commander.Option('-h, --help').hideHelp());
const helper = new commander.Help();
expect(helper.visibleOptions(program)).toEqual([]);
});
// test('when help flags obscured then implicit help hidden', () => {
// const program = new commander.Command();
// program.addOption(new commander.Option('-h, --help').hideHelp());
// const helper = new commander.Help();
// expect(helper.visibleOptions(program)).toEqual([]);
// });
});

0 comments on commit 4fb4747

Please sign in to comment.