/// <summary> /// Adds an interactive mode to the application (enabled with [interactive] directive or `interactive` command). /// By default this adds [interactive], [default], [>], [.], and [..], as well as `interactive` command and advanced command input. /// </summary> public static CliApplicationBuilder UseInteractiveMode(this CliApplicationBuilder builder, bool asStartup = false, Action <InteractiveModeOptions>?options = null, InteractiveModeBuilderSettings?builderSettings = null) { builderSettings ??= new InteractiveModeBuilderSettings(); builder.RegisterMode <InteractiveMode>(asStartup); options ??= (InteractiveModeOptions cfg) => { }; builder.ConfigureServices((IServiceCollection sc) => sc.Configure(options)); builder.AddDirective <DefaultDirective>(); if (builderSettings.AddInteractiveCommand) { builder.AddCommand <InteractiveCommand>(); } if (builderSettings.AddInteractiveDirective) { builder.AddDirective <InteractiveDirective>(); } if (builderSettings.AddScopeDirectives) { builder.AddDirective <ScopeDirective>(); builder.AddDirective <ScopeResetDirective>(); builder.AddDirective <ScopeUpDirective>(); } return(builder); }
public void Configure(CliApplicationBuilder app) { var(console, _, _) = VirtualConsole.CreateBuffered(); app.AddCommand <DefaultCommand>() .AddCommandsFrom(typeof(DefaultCommand).Assembly) .AddCommands(new[] { typeof(DefaultCommand) }) .AddCommandsFrom(new[] { typeof(DefaultCommand).Assembly }) .AddCommandsFromThisAssembly() .AddDirective <DebugDirective>() .AddDirective <PreviewDirective>() .AddDirective <CustomInteractiveModeOnlyDirective>() .AddDirective <CustomDirective>() .UseTitle("test") .UseExecutableName("test") .UseVersionText("test") .UseDescription("test") .UseConsole(console); }
public void Smoke_Test() { // Arrange var builder = new CliApplicationBuilder(); // Act builder .AddCommand(typeof(HelloWorldDefaultCommand)) .AddCommandsFrom(typeof(HelloWorldDefaultCommand).Assembly) .AddCommands(new[] { typeof(HelloWorldDefaultCommand) }) .AddCommandsFrom(new[] { typeof(HelloWorldDefaultCommand).Assembly }) .AddCommandsFromThisAssembly() .AllowDebugMode() .AllowPreviewMode() .UseTitle("test") .UseExecutableName("test") .UseVersionText("test") .UseDescription("test") .UseConsole(new VirtualConsole(TextWriter.Null)) .UseTypeActivator(Activator.CreateInstance) .Build(); }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await using var scope = Services.CreateAsyncScope(); var app = new CliApplicationBuilder(); { HashSet <Type> addedCommands = new(); foreach (var module in Collection.LoadedModules) { stoppingToken.ThrowIfCancellationRequested(); var cmanifest = Collection.GetManifest(module.GetType()); foreach (var cmd in cmanifest.Commands) { stoppingToken.ThrowIfCancellationRequested(); if (addedCommands.Contains(cmd)) { continue; } app.AddCommand(cmd); addedCommands.Add(cmd); } } } var exitCode = await app .UseTypeActivator(scope.ServiceProvider.GetRequiredService) .Build() .RunAsync().ConfigureAwait(false); Environment.ExitCode = exitCode; Lifetime.StopApplication(); }
public void All_Smoke_Test() { // Arrange var builder = new CliApplicationBuilder(); // Act builder .AddCommand(typeof(HelloWorldDefaultCommand)) .AddCommandsFrom(typeof(HelloWorldDefaultCommand).Assembly) .AddCommands(new[] { typeof(HelloWorldDefaultCommand) }) .AddCommandsFrom(new[] { typeof(HelloWorldDefaultCommand).Assembly }) .AddCommandsFromThisAssembly() .AllowDebugMode() .AllowPreviewMode() .UseTitle("test") .UseExecutableName("test") .UseVersionText("test") .UseDescription("test") .UseConsole(new VirtualConsole(TextWriter.Null)) .UseCommandFactory(schema => (ICommand)Activator.CreateInstance(schema.Type !) !) .UseCommandOptionInputConverter(new CommandInputConverter()) .UseEnvironmentVariablesProvider(new EnvironmentVariablesProviderStub()) .Build(); }