/// <summary> /// Initializes an instance of <see cref="LineInputHandler"/>. /// </summary> public LineInputHandler(IConsole console, HashSet <ShortcutDefinition>?internalShortcuts = null, HashSet <ShortcutDefinition>?userDefinedShortcuts = null) : this(console) { if (internalShortcuts != null) { //TODO: maybe hashset is not the best collection //_shortcuts.Union(internalShortcuts); foreach (ShortcutDefinition shortcut in internalShortcuts) { if (!_shortcuts.Add(shortcut)) { //Replace when already exists _shortcuts.Remove(shortcut); _shortcuts.Add(shortcut); } } } if (userDefinedShortcuts != null) { //_shortcuts.Union(userDefinedShortcut); foreach (ShortcutDefinition shortcut in userDefinedShortcuts) { if (!_shortcuts.Add(shortcut)) { throw ModeEndUserExceptions.DuplicatedShortcut(shortcut); } } } }
public async Task HandleAsync(ICliContext context, CommandPipelineHandlerDelegate next, CancellationToken cancellationToken) { //Get current CLI mode and input directives Type currentModeType = _applicationLifetime.CurrentModeType !; IReadOnlyList <DirectiveInput> directives = context.Input.Directives; //Initialize collections List <IDirective> directivesInstances = new List <IDirective>(); List <IPipelinedDirective> pipelinedDirectivesInstances = new List <IPipelinedDirective>(); //Process directive input 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 ArgumentBindingExceptions.UnknownDirectiveName(directiveInput); // Handle interactive directives not supported in current mode if (!directive.CanBeExecutedInMode(currentModeType)) { throw ModeEndUserExceptions.DirectiveExecutedInInvalidMode(directive, currentModeType); } // Get directive instance IDirective instance = (IDirective)_serviceProvider.GetRequiredService(directive.Type); //Initialize directive await instance.OnInitializedAsync(cancellationToken); //Add directive to list directivesInstances.Add(instance); if (directive.IsPipelinedDirective && instance is IPipelinedDirective pd) { pipelinedDirectivesInstances.Add(pd); } } //Set directives lists in context CliContext internalCliContext = (CliContext)context; internalCliContext.Directives = directivesInstances; internalCliContext.PipelinedDirectives = pipelinedDirectivesInstances; await next(); }
internal void Initialize() { Type startupMode = _configuration.StartupMode; _logger.LogDebug("Initializing startup mode '{StartupMode}'.", startupMode); if (_serviceProvider.GetService(startupMode) is ICliMode cm) { CurrentMode = cm; CurrentModeType = startupMode; State = CliLifetimes.Running; } else { _logger.LogError("Failed to initialize startup mode '{StartupMode}'.", startupMode); throw ModeEndUserExceptions.InvalidStartupModeType(startupMode); } }
public async Task HandleAsync(ICliContext context, CommandPipelineHandlerDelegate next, CancellationToken cancellationToken) { //Get input and command schema from context CommandInput input = context.Input; CommandSchema commandSchema = context.CommandSchema; Type currentModeType = _applicationLifetime.CurrentModeType !; // Handle commands not supported in current mode if (!commandSchema.CanBeExecutedInMode(currentModeType)) { throw ModeEndUserExceptions.CommandExecutedInInvalidMode(commandSchema, currentModeType); } // Get command instance from context and bind arguments ICommand instance = context.Command; commandSchema.BindParameters(instance, input.Parameters); commandSchema.BindOptions(instance, input.Options, _optionFallbackProvider); await next(); }