|
| 1 | +using SquidStd.ConsoleCommands.Attributes; |
| 2 | +using SquidStd.ConsoleCommands.Data; |
| 3 | +using SquidStd.ConsoleCommands.Extensions; |
| 4 | +using SquidStd.ConsoleCommands.Interfaces; |
| 5 | +using SquidStd.Core.Data.Bootstrap; |
| 6 | +using SquidStd.Generators.ConsoleCommands; |
| 7 | +using SquidStd.Services.Core.Extensions; |
| 8 | +using SquidStd.Services.Core.Services.Bootstrap; |
| 9 | + |
| 10 | +var bootstrap = SquidStdBootstrap.Create( |
| 11 | + new SquidStdOptions |
| 12 | + { |
| 13 | + ConfigName = "squidstd", |
| 14 | + RootDirectory = AppContext.BaseDirectory, |
| 15 | + AppName = "ConsoleDemo" |
| 16 | + } |
| 17 | +); |
| 18 | + |
| 19 | +bootstrap.ConfigureServices( |
| 20 | + container => |
| 21 | + { |
| 22 | + container.RegisterCoreServices(); |
| 23 | + container.AddConsoleCommands(); |
| 24 | + container.RegisterGeneratedConsoleCommands(); |
| 25 | + |
| 26 | + return container; |
| 27 | + } |
| 28 | +); |
| 29 | + |
| 30 | +// The prompt sink renders the log lines: disable the standard console sink to avoid duplicates. |
| 31 | +bootstrap.OnConfigLoaded<SquidStdLoggerOptions>(o => o.EnableConsole = false); |
| 32 | + |
| 33 | +await bootstrap.StartAsync(); |
| 34 | + |
| 35 | +var commands = bootstrap.Resolve<ICommandSystemService>(); |
| 36 | +commands.RegisterCommand( |
| 37 | + "echo", |
| 38 | + ctx => |
| 39 | + { |
| 40 | + ctx.WriteLine(string.Join(' ', ctx.Arguments)); |
| 41 | + |
| 42 | + return Task.CompletedTask; |
| 43 | + }, |
| 44 | + "Echoes the arguments back." |
| 45 | +); |
| 46 | + |
| 47 | +foreach (var line in await commands.ExecuteCommandWithOutputAsync("help")) |
| 48 | +{ |
| 49 | + Console.WriteLine(line); |
| 50 | +} |
| 51 | + |
| 52 | +foreach (var line in await commands.ExecuteCommandWithOutputAsync("ping")) |
| 53 | +{ |
| 54 | + Console.WriteLine(line); |
| 55 | +} |
| 56 | + |
| 57 | +await bootstrap.StopAsync(); |
| 58 | + |
| 59 | +[RegisterConsoleCommand("ping|p", "Replies pong.")] |
| 60 | +internal sealed class PingCommand : IConsoleCommandExecutor |
| 61 | +{ |
| 62 | + public string Description => "Replies pong."; |
| 63 | + |
| 64 | + public Task ExecuteAsync(ConsoleCommandContext context) |
| 65 | + { |
| 66 | + context.WriteLine("pong"); |
| 67 | + |
| 68 | + return Task.CompletedTask; |
| 69 | + } |
| 70 | +} |
0 commit comments