private async Task <bool> ProcessDefinedDirectives(ICliContext context) { bool isInteractiveMode = context.IsInteractiveMode; IReadOnlyList <DirectiveInput> directives = context.Input.Directives; foreach (DirectiveInput directiveInput in directives) { // Try to get the directive matching the input or fallback to default DirectiveSchema directive = context.RootSchema.TryFindDirective(directiveInput.Name) ?? throw EndUserTypinExceptions.UnknownDirectiveName(directiveInput); if (!isInteractiveMode && directive.InteractiveModeOnly) { throw EndUserTypinExceptions.InteractiveModeDirectiveNotAvailable(directiveInput.Name); } // Get directive instance IDirective instance = (IDirective)_serviceProvider.GetRequiredService(directive.Type); await instance.HandleAsync(context.Console); if (!instance.ContinueExecution) { return(false); } } return(true); }
private int?Execute(CliContext context) { // Get configuration and input from context ApplicationConfiguration _configuration = context.Configuration; CommandInput input = context.Input; // Handle interactive directive not supported in application if (!_configuration.IsInteractiveModeAllowed && input.IsInteractiveDirectiveSpecified) { throw EndUserTypinExceptions.InteractiveModeNotSupported(); } return(null); }
private void BindParameters(ICommand instance, IReadOnlyList <CommandParameterInput> parameterInputs) { // All inputs must be bound List <CommandParameterInput> remainingParameterInputs = parameterInputs.ToList(); // Scalar parameters CommandParameterSchema[] scalarParameters = Parameters.OrderBy(p => p.Order) .TakeWhile(p => p.IsScalar) .ToArray(); for (int i = 0; i < scalarParameters.Length; i++) { CommandParameterSchema parameter = scalarParameters[i]; CommandParameterInput scalarInput = i < parameterInputs.Count ? parameterInputs[i] : throw EndUserTypinExceptions.ParameterNotSet(parameter); parameter.BindOn(instance, scalarInput.Value); remainingParameterInputs.Remove(scalarInput); } // Non-scalar parameter (only one is allowed) CommandParameterSchema nonScalarParameter = Parameters.OrderBy(p => p.Order) .FirstOrDefault(p => !p.IsScalar); if (nonScalarParameter != null) { string[] nonScalarValues = parameterInputs.Skip(scalarParameters.Length) .Select(p => p.Value) .ToArray(); // Parameters are required by default and so a non-scalar parameter must // be bound to at least one value if (!nonScalarValues.Any()) { throw EndUserTypinExceptions.ParameterNotSet(nonScalarParameter); } nonScalarParameter.BindOn(instance, nonScalarValues); remainingParameterInputs.Clear(); } // Ensure all inputs were bound if (remainingParameterInputs.Any()) { throw EndUserTypinExceptions.UnrecognizedParametersProvided(remainingParameterInputs); } }
private int?Execute(CliContext context) { //Get configuration and input from context ApplicationConfiguration _configuration = context.Configuration; CommandInput input = context.Input; // Get command schema from context CommandSchema commandSchema = context.CommandSchema; // Handle commands not supported in normal mode if (!_configuration.IsInteractiveModeAllowed && commandSchema.InteractiveModeOnly) { throw EndUserTypinExceptions.InteractiveOnlyCommandButThisIsNormalApplication(commandSchema); } // Handle commands supported only in interactive mode when interactive mode was not started if (_configuration.IsInteractiveModeAllowed && commandSchema.InteractiveModeOnly && !(context.IsInteractiveMode || input.IsInteractiveDirectiveSpecified)) { throw EndUserTypinExceptions.InteractiveOnlyCommandButInteractiveModeNotStarted(commandSchema); } return(null); }