Feat: Switch to Spectre.Console.Cli as CLI framework (#3)

This commit is contained in:
Marcello 2022-10-15 12:24:30 +02:00
parent 4ab62ff538
commit 1cf2e05a67
2 changed files with 84 additions and 49 deletions

View file

@ -1,38 +1,42 @@
using Cocona; using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Spectre.Console; using Spectre.Console;
using Spectre.Console.Cli;
var app = CoconaLiteApp.Create(); var app = new CommandApp<RootCommand>();
app.Configure(x => x.SetApplicationName("script-launcher"));
return app.Run(args);
app.AddCommand(RootCommand); sealed class RootCommand : AsyncCommand<RootCommandSettings>
app.Run();
static async Task RootCommand(
[Option("extensions", new[] { 'x' }, Description = "Comma separated list of script extensions")] string? extensions,
[Option("depth", new[] { 'd' }, Description = "Search depth")] int depth = 1,
[Option("elevated", new[] { 'e' }, Description = "Run with elevated privileges")] bool elevated = false,
[Option("group", new[] { 'g' }, Description = "Group scripts by folder")] bool group = false,
[Option("brief", new[] { 'b' }, Description = "Show brief information")] bool brief = false,
[Argument(Name = "Directory", Description = "Starting directory")] string directory = ".")
{ {
if (!Directory.Exists(directory)) private const int Failure = 1;
private const int Success = 0;
public override async Task<int> ExecuteAsync(
[NotNull] CommandContext context,
[NotNull] RootCommandSettings settings
)
{ {
AnsiConsole.Markup($"[red]The directory '{directory}' does not exist.[/]"); if (!Directory.Exists(settings.Directory))
Environment.ExitCode = 1; {
return; AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]");
// Environment.ExitCode = 1;
return Failure;
} }
FileInfo[] files; FileInfo[] files;
var finder = new ScriptFinder(extensions, directory, depth); var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth);
if (group) if (settings.Group)
{ {
var dict = finder.GetScriptsByDirectory(); var dict = finder.GetScriptsByDirectory();
if (dict.Count == 0) if (dict.Count == 0)
{ {
AnsiConsole.Markup($"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"); AnsiConsole.Markup(
Environment.ExitCode = 1; $"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"
return; );
return Failure;
} }
var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray()); var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray());
@ -46,13 +50,44 @@ static async Task RootCommand(
if (files.Length == 0) if (files.Length == 0)
{ {
AnsiConsole.Markup($"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"); AnsiConsole.Markup(
Environment.ExitCode = 1; $"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"
return; );
return Failure;
} }
var prompt = PromptConstructor.GetScriptPrompt(files, brief); var prompt = PromptConstructor.GetScriptPrompt(files, settings.Brief);
var scripts = AnsiConsole.Prompt(prompt); var scripts = AnsiConsole.Prompt(prompt);
await ScriptExecutor.ExecAsync(scripts, elevated); await ScriptExecutor.ExecAsync(scripts, settings.Elevated);
return Success;
}
}
internal class RootCommandSettings : CommandSettings
{
[Description("Comma separated list of script extensions")]
[CommandOption("-x|--extensions")]
public string? Extensions { get; init; }
[Description("Search depth")]
[CommandOption("-d|--depth")]
public int Depth { get; init; } = 1;
[Description("Run with elevated privileges")]
[CommandOption("-e|--elevated")]
public bool Elevated { get; init; } = false;
[Description("Group scripts by folder")]
[CommandOption("-g|--group")]
public bool Group { get; init; } = false;
[Description("Show brief information")]
[CommandOption("-b|--brief")]
public bool Brief { get; init; } = false;
[Description("Starting directory (Default: .)")]
[CommandArgument(0, "<path>")]
public string Directory { get; init; } = ".";
} }

View file

@ -5,14 +5,14 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Version>0.1.0</Version> <Version>0.1.1</Version>
<PackAsTool>true</PackAsTool> <PackAsTool>true</PackAsTool>
<ToolCommandName>script-launcher</ToolCommandName> <ToolCommandName>script-launcher</ToolCommandName>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Cocona.Lite" Version="2.0.3" />
<PackageReference Include="Spectre.Console" Version="0.45.0" /> <PackageReference Include="Spectre.Console" Version="0.45.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.45.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>