private int?HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput) { var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified; if (!isPreviewMode) { return(null); } var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset); _console.Output.WriteLine("Parser preview:"); // Command name if (commandSchema != null && argumentOffset > 0) { _console.WithForegroundColor(ConsoleColor.Cyan, () => _console.Output.Write(commandSchema.Name)); _console.Output.Write(' '); } // Parameters foreach (var parameter in commandLineInput.Arguments.Skip(argumentOffset)) { _console.Output.Write('<'); _console.WithForegroundColor(ConsoleColor.White, () => _console.Output.Write(parameter)); _console.Output.Write('>'); _console.Output.Write(' '); } // Options foreach (var option in commandLineInput.Options) { _console.Output.Write('['); _console.WithForegroundColor(ConsoleColor.White, () => _console.Output.Write(option)); _console.Output.Write(']'); _console.Output.Write(' '); } _console.Output.WriteLine(); return(0); }
private int?HandleHelpOption(ApplicationSchema applicationSchema, CommandLineInput commandLineInput) { // Help is rendered either when it's requested or when the user provides no arguments and there is no default command var shouldRenderHelp = commandLineInput.IsHelpOptionSpecified || !applicationSchema.Commands.Any(c => c.IsDefault) && !commandLineInput.Arguments.Any() && !commandLineInput.Options.Any(); if (!shouldRenderHelp) { return(null); } // Get the command schema that matches the input or use a dummy default command as a fallback var commandSchema = applicationSchema.TryFindCommand(commandLineInput) ?? CommandSchema.StubDefaultCommand; RenderHelp(applicationSchema, commandSchema); return(0); }
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); } }