Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better handle calling help on non existent commands #267

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ export default function(yargs: Argv, groupMap: GroupMap): void {
return dojoYargs;
},
(argv: any) => {
const isGroupCommand = argv._.length && argv._.length > 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just !!argv._.length or Boolean(argv._.length) would do I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone was talking about this yesterday, I think Boolean(argv._.length) is nicer but that's just me :)

if (isGroupCommand) {
const groupCommand = groupMap.get(argv._[0]);
if (!groupCommand || !groupCommand.get(argv._[1])) {
throw `Unable to find given command`;
}
}
console.log(formatHelp(argv, groupMap));
}
)
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,43 @@ registerSuite('registerCommands', {
help.reset();
yargsStub.command.lastCall.args[3]({ _: [], h: true });
assert.isTrue(help.calledOnce);
assert.isTrue(consoleLogStub.calledOnce);
},
'group help called'() {
const help = mockModule.getMock('./help').formatHelp;
help.reset();
yargsStub.command.firstCall.args[3]({ _: ['group'], h: true });
assert.isTrue(help.calledOnce);
assert.isTrue(consoleLogStub.calledOnce);
},
'command help called'() {
const help = mockModule.getMock('./help').formatHelp;
help.reset();
yargsStub.command.secondCall.args[3]({ _: ['group', 'command'], h: true });
assert.isTrue(help.calledOnce);
assert.isTrue(consoleLogStub.calledOnce);
},
'error if help called on non existant group'() {
const help = mockModule.getMock('./help').formatHelp;
help.reset();
try {
yargsStub.command.lastCall.args[3]({ _: ['nonexistant'], h: true });
assert.fail(null, null, 'getting unknown group should throw an error');
} catch {
assert.isTrue(help.notCalled);
assert.isTrue(consoleLogStub.notCalled);
}
},
'error if help called on non existant command'() {
const help = mockModule.getMock('./help').formatHelp;
help.reset();
try {
yargsStub.command.lastCall.args[3]({ _: ['group', 'nonexistant'], h: true });
assert.fail(null, null, 'getting unknown command should throw an error');
} catch {
assert.isTrue(help.notCalled);
assert.isTrue(consoleLogStub.notCalled);
}
}
}
},
Expand Down