private async ValueTask <int> RunAsync(ApplicationSchema applicationSchema, CommandInput commandInput) { // Handle debug directive if (IsDebugModeEnabled(commandInput)) { await PromptDebuggerAsync(); } // Handle preview directive if (IsPreviewModeEnabled(commandInput)) { _console.Output.WriteCommandInput(commandInput); return(0); } // Try to get the command schema that matches the input var commandSchema = applicationSchema.TryFindCommand(commandInput.CommandName) ?? applicationSchema.TryFindDefaultCommand() ?? FallbackDefaultCommand.Schema; // Activate command instance var commandInstance = commandSchema == FallbackDefaultCommand.Schema ? new FallbackDefaultCommand() // bypass activator : (ICommand)_typeActivator.CreateInstance(commandSchema.Type); // Assemble help context var helpContext = new HelpContext( Metadata, applicationSchema, commandSchema, commandSchema.GetValues(commandInstance) ); // Handle help option if (ShouldShowHelpText(commandSchema, commandInput)) { _console.Output.WriteHelpText(helpContext); return(0); } // Handle version option if (ShouldShowVersionText(commandSchema, commandInput)) { _console.Output.WriteLine(Metadata.Version); return(0); } // Starting from this point, we may produce exceptions that are meant for the // end user of the application (i.e. invalid input, command exception, etc). // Catch these exceptions here, print them to the console, and don't let them // propagate further. try { // Bind and execute command _commandBinder.Bind(commandInput, commandSchema, commandInstance); await commandInstance.ExecuteAsync(_console); return(0); } catch (CliFxException ex) { _console.Error.WriteException(ex); if (ex.ShowHelp) { _console.Output.WriteLine(); _console.Output.WriteHelpText(helpContext); } return(ex.ExitCode); } }