Skip to content

Commit

Permalink
Added a sample dependency to show commands can use injected services …
Browse files Browse the repository at this point in the history
…from standard service collection
  • Loading branch information
jakenuts committed Dec 31, 2023
1 parent a03e860 commit de12952
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@ public override async Task<int> ExecuteAsync(CommandContext context, Options opt
return 0;
}

/// <summary>
///
/// </summary>
[Description("Says 'Hello' to you and optionally your dog.")]
public class Options : CommandSettings
{
/// <summary>
///
/// </summary>
[Description("Your Name")]
[CommandArgument(0, "<name>")]
[Required]
public string? Name { get; set; }

/// <summary>
///
/// </summary>
[Description("Your Dogs Name (Optional)")]
[CommandArgument(1, "[name]")]
[CommandOption("-p|--pup")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,42 @@ namespace Community.Extensions.Spectre.Cli.Hosting.Sample.Commands;
/// </summary>
public class OtherCommand : AsyncCommand<OtherCommand.Options>
{
private readonly SampleService _service;

private readonly IAnsiConsole _console;

/// <summary>
/// Creates a OtherCommand with access to the console and logging
/// </summary>
/// <param name="service">A sample dependency</param>
/// <param name="console"></param>
/// <param name="log"></param>
public OtherCommand(IAnsiConsole console, ILogger<HelloCommand> log)
public OtherCommand(SampleService service, IAnsiConsole console, ILogger<HelloCommand> log)
{
_service = service;
_console = console;
}

/// <summary>Executes the command.</summary>
/// <param name="context">The command context.</param>
/// <param name="options">The command options.</param>
/// <returns>An integer indicating whether or not the command executed successfully.</returns>
public override async Task<int> ExecuteAsync(CommandContext context, Options options)
public override Task<int> 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);
}

/// <summary>
///
/// </summary>
[Description("OtherOptions")]
public class Options : CommandSettings
{
/// <summary>
///
/// </summary>
[Description("Other Stuff")]
[CommandArgument(0, "<stuff>")]
public string? Stuff { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Community.Extensions.Spectre.Cli.Hosting.Sample.Commands;

/// <summary>
/// A SampleService to show commands can have dependencies injected
/// </summary>
public class SampleService(string message)
{
/// <summary>
/// A message
/// </summary>
public string Message { get; set; } = message;
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,15 +14,19 @@
cmd.WithDescription("A command that says hello");
});

// Add another command

// Add another command and its dependent service

builder.Services.AddCommand<OtherCommand>("other");
builder.Services.AddScoped(s => new SampleService("Other Service"));

//
// The standard call save for the commands will be pre-added & configured
//
builder.UseSpectreConsole<HelloCommand>(config =>
{
// All commands above are passed to config.AddCommand() by this point
#if DEBUG
config.PropagateExceptions();
config.ValidateExamples();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static IServiceCollection AddCommand<TCommand>(this IServiceCollection se
where TCommand : class, ICommand

{
services.AddSingleton<TCommand>();
services.AddScoped<TCommand>();
services.RegisterCommand<TCommand>(name, commandConfigurator);
return services;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup Label="Settings">
<Deterministic>true</Deterministic>
<LangVersion>10</LangVersion>
<LangVersion>preview</LangVersion>
<DebugSymbols>true</DebugSymbols>
<DebugType>embedded</DebugType>
<MinVerSkip Condition="'$(Configuration)' == 'Debug'">true</MinVerSkip>
Expand Down

0 comments on commit de12952

Please sign in to comment.