Skip to content

Commit 3b7cd6f

Browse files
MathijsVerbeeckJwaegebaert
authored andcommitted
Enhances cli warning when non-existing command is called. Closes #5768
1 parent 28ff16f commit 3b7cd6f

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/cli/cli.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,18 @@ describe('cli', () => {
334334
assert(cliLogStub.calledWith(`CLI for Microsoft 365 v${packageJSON.version}`));
335335
});
336336

337+
it('shows message that command cannot be found when an unknown command is entered', async () => {
338+
const commandName = 'unknown';
339+
await cli.execute([commandName]);
340+
assert(cliErrorStub.calledWith(chalk.red(`Command '${cli.currentCommandName}' was not found. Below you can find the commands and command groups you can use. For detailed information on a command group, use 'm365 [command group] --help'.`)));
341+
});
342+
343+
it('does not show message that command cannot be found when a uncompleted command is entered', async () => {
344+
const commandName = 'cli mock';
345+
await cli.execute([commandName]);
346+
assert(cliErrorStub.notCalled);
347+
});
348+
337349
it('exits with 0 code when no command specified', async () => {
338350
await cli.execute([]);
339351
assert(processExitStub.calledWith(0));

src/cli/cli.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { CommandOptionInfo } from './CommandOptionInfo.js';
2222
import { Logger } from './Logger.js';
2323
import { SelectionConfig, ConfirmationConfig, prompt } from '../utils/prompt.js';
2424
import { timings } from './timings.js';
25+
import chalk from 'chalk';
2526
const require = createRequire(import.meta.url);
2627

2728
const __dirname = fileURLToPath(new URL('.', import.meta.url));
@@ -120,7 +121,7 @@ async function execute(rawArgs: string[]): Promise<void> {
120121
parsedArgs.h ||
121122
parsedArgs.help) {
122123
if (parsedArgs.output !== 'none') {
123-
printHelp(await getHelpMode(parsedArgs));
124+
await printHelp(await getHelpMode(parsedArgs));
124125
}
125126
return;
126127
}
@@ -652,14 +653,18 @@ function getFirstNonUndefinedArrayItem(arr: any[]): any {
652653
return undefined;
653654
}
654655

655-
function printHelp(helpMode: string, exitCode: number = 0): void {
656+
async function printHelp(helpMode: string, exitCode: number = 0): Promise<void> {
656657
const properties: any = {};
657658

658659
if (cli.commandToExecute) {
659660
properties.command = cli.commandToExecute.name;
660661
printCommandHelp(helpMode);
661662
}
662663
else {
664+
if (cli.currentCommandName && !cli.commands.some(command => command.name.startsWith(cli.currentCommandName!))) {
665+
await cli.error(chalk.red(`Command '${cli.currentCommandName}' was not found. Below you can find the commands and command groups you can use. For detailed information on a command group, use 'm365 [command group] --help'.`));
666+
}
667+
663668
cli.log();
664669
cli.log(`CLI for Microsoft 365 v${app.packageJson().version}`);
665670
cli.log(`${app.packageJson().description} `);
@@ -878,7 +883,7 @@ async function closeWithError(error: any, args: CommandArgs, showHelpIfEnabled:
878883

879884
if (showHelpIfEnabled &&
880885
await cli.getSettingWithDefaultValue<boolean>(settingsNames.showHelpOnFailure, showHelpIfEnabled)) {
881-
printHelp(await getHelpMode(args.options), exitCode);
886+
await printHelp(await getHelpMode(args.options), exitCode);
882887
}
883888
else {
884889
process.exit(exitCode);

0 commit comments

Comments
 (0)