private void InitializeCommandOptions(ICommand command, CommandCandidate commandCandidate) { if (commandCandidate.Schema is null) { throw new ArgumentException("Cannot initialize command without a schema."); } // Keep track of unset required options to report an error at a later stage var unsetRequiredOptions = commandCandidate.Schema.Options.Where(o => o.IsRequired).ToList(); //Set command options foreach (var optionSchema in commandCandidate.Schema.Options) { // Ignore special options that are not backed by a property if (optionSchema.Property == null) { continue; } //Find matching option input var optionInput = commandCandidate.CommandInput.Options.FindByOptionSchema(optionSchema); //If no option input is available fall back to environment variable values if (optionInput == null && !string.IsNullOrWhiteSpace(optionSchema.EnvironmentVariableName)) { var fallbackEnvironmentVariableExists = commandCandidate.CommandInput.EnvironmentVariables.ContainsKey(optionSchema.EnvironmentVariableName !); //If no environment variable is found or there is no valid value for this option skip it if (!fallbackEnvironmentVariableExists || string.IsNullOrWhiteSpace(commandCandidate.CommandInput.EnvironmentVariables[optionSchema.EnvironmentVariableName !]))
public void InitializeCommand_Negative_Test(ICommand command, CommandCandidate commandCandidate) { // Arrange var initializer = new CommandInitializer(); // Act & Assert initializer.Invoking(i => i.InitializeCommand(command, commandCandidate)) .Should().ThrowExactly <CliFxException>(); }
public void InitializeCommand_Test(ICommand command, CommandCandidate commandCandidate, ICommand expectedCommand) { // Arrange var initializer = new CommandInitializer(); // Act initializer.InitializeCommand(command, commandCandidate); // Assert command.Should().BeEquivalentTo(expectedCommand, o => o.RespectingRuntimeTypes()); }
private int?HandleHelpOption(CommandInput commandInput, IReadOnlyList <CommandSchema> availableCommandSchemas, CommandCandidate?commandCandidate) { // Help should be rendered if it was requested, or when executing a command which isn't defined var shouldRenderHelp = commandInput.IsHelpOptionSpecified() || commandCandidate == null; // If shouldn't render help, pass execution to the next handler if (!shouldRenderHelp) { return(null); } // Keep track whether there was an error in the input var isError = false; // Report error if no command matched the arguments if (commandCandidate is null) { // If a command was specified, inform the user that the command is not defined if (commandInput.HasArguments()) { _console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine($"No command could be matched for input [{string.Join(" ", commandInput.Arguments)}]")); isError = true; } commandCandidate = new CommandCandidate(CommandSchema.StubDefaultCommand, new string[0], commandInput); } // Build help text source var helpTextSource = new HelpTextSource(_metadata, availableCommandSchemas, commandCandidate.Schema); // Render help text _helpTextRenderer.RenderHelpText(_console, helpTextSource); // Short-circuit with appropriate exit code return(isError ? -1 : 0); }