Skip to content

Commit ba8a840

Browse files
committed
feat(core): show message when user tries to run command that does not exist
also add an extra carriage return to the usage when user does not specify a command
1 parent 9824234 commit ba8a840

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/classy.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,19 @@ export class Commander {
5454
const args = argv || process.argv;
5555

5656
if (args.length <= 2) {
57-
cli.help();
57+
cli.help((text) => `${text}\n`);
5858
}
5959

60+
cli.on('command:*', () => {
61+
console.error();
62+
console.error(
63+
'Invalid command: %s\nSee --help for a list of available commands.',
64+
cli.args.join(' ')
65+
);
66+
console.error();
67+
process.exit(1);
68+
});
69+
6070
cli.parse(args);
6171
}
6272

src/functional.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,31 @@ describe('Functional Tests', () => {
7575
);
7676
});
7777

78-
it('should return usage if not supplied any args', async () => {
79-
jest.spyOn(commander, 'help').mockImplementation(noop);
78+
it('should show usage if not supplied any args', async () => {
79+
let modifiedHelp: string | undefined;
80+
81+
const helpSpy = jest.spyOn(commander, 'help').mockImplementation((cb: (output: string) => string) => {
82+
modifiedHelp = cb('some help');
83+
});
8084

8185
await cli.execute(['', '']);
8286

8387
expect(commander.help).toHaveBeenCalled();
88+
89+
expect(modifiedHelp).toEqual('some help\n');
90+
});
91+
92+
it('should show usage if supplied a command that does not exist', () => {
93+
jest.spyOn(console, 'error').mockImplementation(noop);
94+
95+
cli.execute(['', '', 'find-prime-factors', '1290833']);
96+
97+
// tslint:disable-next-line:no-console
98+
expect(console.error).toHaveBeenCalledWith(
99+
'Invalid command: %s\nSee --help for a list of available commands.',
100+
'find-prime-factors 1290833'
101+
);
102+
expect(process.exit).toHaveBeenCalled();
84103
});
85104

86105
describe('when not explicitly providing args', () => {

0 commit comments

Comments
 (0)