diff --git a/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/HelloCommand.cs b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/HelloCommand.cs index fa532e1..a08bf2b 100644 --- a/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/HelloCommand.cs +++ b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/HelloCommand.cs @@ -48,14 +48,23 @@ public override async Task ExecuteAsync(CommandContext context, Options opt return 0; } + /// + /// + /// [Description("Says 'Hello' to you and optionally your dog.")] public class Options : CommandSettings { + /// + /// + /// [Description("Your Name")] [CommandArgument(0, "")] [Required] public string? Name { get; set; } + /// + /// + /// [Description("Your Dogs Name (Optional)")] [CommandArgument(1, "[name]")] [CommandOption("-p|--pup")] diff --git a/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/OtherCommand.cs b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/OtherCommand.cs index e8bd7a7..092f3bb 100644 --- a/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/OtherCommand.cs +++ b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/OtherCommand.cs @@ -12,15 +12,19 @@ namespace Community.Extensions.Spectre.Cli.Hosting.Sample.Commands; /// public class OtherCommand : AsyncCommand { + private readonly SampleService _service; + private readonly IAnsiConsole _console; /// /// Creates a OtherCommand with access to the console and logging /// + /// A sample dependency /// /// - public OtherCommand(IAnsiConsole console, ILogger log) + public OtherCommand(SampleService service, IAnsiConsole console, ILogger log) { + _service = service; _console = console; } @@ -28,16 +32,22 @@ public OtherCommand(IAnsiConsole console, ILogger log) /// The command context. /// The command options. /// An integer indicating whether or not the command executed successfully. - public override async Task ExecuteAsync(CommandContext context, Options options) + public override Task ExecuteAsync(CommandContext context, Options options) { - _console.MarkupLineInterpolated($"[springgreen2_1] Other {options.Stuff}![/]"); + _console.MarkupLineInterpolated($"[springgreen2_1] Other {options.Stuff} - {_service.Message}![/]"); - return 0; + return Task.FromResult(0); } + /// + /// + /// [Description("OtherOptions")] public class Options : CommandSettings { + /// + /// + /// [Description("Other Stuff")] [CommandArgument(0, "")] public string? Stuff { get; set; } diff --git a/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/SampleService.cs b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/SampleService.cs new file mode 100644 index 0000000..11b524f --- /dev/null +++ b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Commands/SampleService.cs @@ -0,0 +1,12 @@ +namespace Community.Extensions.Spectre.Cli.Hosting.Sample.Commands; + +/// +/// A SampleService to show commands can have dependencies injected +/// +public class SampleService(string message) +{ + /// + /// A message + /// + public string Message { get; set; } = message; +} \ No newline at end of file diff --git a/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Program.cs b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Program.cs index c1e819e..b4e7dab 100644 --- a/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Program.cs +++ b/src/Community.Extensions.Spectre.Cli.Hosting.Sample/Program.cs @@ -1,8 +1,8 @@ using System.Diagnostics; using Community.Extensions.Spectre.Cli.Hosting; using Community.Extensions.Spectre.Cli.Hosting.Sample.Commands; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; using Spectre.Console; using Spectre.Console.Cli; @@ -14,8 +14,11 @@ cmd.WithDescription("A command that says hello"); }); -// Add another command + +// Add another command and its dependent service + builder.Services.AddCommand("other"); +builder.Services.AddScoped(s => new SampleService("Other Service")); // // The standard call save for the commands will be pre-added & configured @@ -23,6 +26,7 @@ builder.UseSpectreConsole(config => { // All commands above are passed to config.AddCommand() by this point + #if DEBUG config.PropagateExceptions(); config.ValidateExamples(); diff --git a/src/Community.Extensions.Spectre.Cli.Hosting/SpectreConsoleHostBuilderExtensions.cs b/src/Community.Extensions.Spectre.Cli.Hosting/SpectreConsoleHostBuilderExtensions.cs index b660d72..838fb9a 100644 --- a/src/Community.Extensions.Spectre.Cli.Hosting/SpectreConsoleHostBuilderExtensions.cs +++ b/src/Community.Extensions.Spectre.Cli.Hosting/SpectreConsoleHostBuilderExtensions.cs @@ -27,7 +27,7 @@ public static IServiceCollection AddCommand(this IServiceCollection se where TCommand : class, ICommand { - services.AddSingleton(); + services.AddScoped(); services.RegisterCommand(name, commandConfigurator); return services; } diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 90bf25d..d247185 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -1,7 +1,7 @@ true - 10 + preview true embedded true