diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0bfc8aa..217f7cb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,9 +15,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup .NET - uses: actions/setup-dotnet@v3 + uses: actions/setup-dotnet@v4 with: dotnet-version: 8.0.x - name: Restore dependencies diff --git a/.gitignore b/.gitignore index dfcfd56..1395441 100644 --- a/.gitignore +++ b/.gitignore @@ -348,3 +348,6 @@ MigrationBackup/ # Ionide (cross platform F# VS Code tools) working folder .ionide/ + +# Jetbrains +.idea/* \ No newline at end of file diff --git a/README.md b/README.md index f2177c7..cdb7c50 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ [![.NET](https://github.com/aimenux/SpectreCliDemo/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/aimenux/SpectreCliDemo/actions/workflows/ci.yml) -# SpectreCliDemo +# SpectreConsoleCliDemo ``` Exploring ways of using Spectre.Console to build CLI tools ``` @@ -13,10 +13,10 @@ In this repo, i m exploring various ways in order to build a simple cli tool bas > To run code in debug or release mode, type the following commands in your favorite terminal : -> - `.\BasicWay.exe math add [number1] [number2]` -> - `.\CustomWay.exe math add [number1] [number2]` -> - `.\BasicWay.exe path list [path] -e [extension]` -> - `.\CustomWay.exe path list [path] -e [extension]` +> - `dotnet run --project .\src\BasicWay\BasicWay.csproj math add [number1] [number2]` +> - `dotnet run --project .\src\CustomWay\CustomWay.csproj math add [number1] [number2]` +> - `dotnet run --project .\src\BasicWay\BasicWay.csproj path list [path] -e [extension]` +> - `dotnet run --project .\src\CustomWay\CustomWay.csproj path list [path] -e [extension]` > -**`Tools`** : vs22, net 6.0, spectre.console.cli +**`Tools`** : net 8.0, spectre.console.cli diff --git a/SpectreCliDemo.sln b/SpectreConsoleCliDemo.sln similarity index 98% rename from SpectreCliDemo.sln rename to SpectreConsoleCliDemo.sln index 2d5f7bb..9adcc22 100644 --- a/SpectreCliDemo.sln +++ b/SpectreConsoleCliDemo.sln @@ -13,6 +13,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution README.md = README.md .editorconfig = .editorconfig Directory.Build.props = Directory.Build.props + .gitignore = .gitignore EndProjectSection EndProject Global diff --git a/src/BasicWay/BasicWay.csproj b/src/BasicWay/BasicWay.csproj index 0172886..6727799 100644 --- a/src/BasicWay/BasicWay.csproj +++ b/src/BasicWay/BasicWay.csproj @@ -7,13 +7,13 @@ - - + + - - - - + + + + diff --git a/src/BasicWay/Commands/Math/AddCommand.cs b/src/BasicWay/Commands/Math/AddCommand.cs index 9c0b0cb..d2a9401 100644 --- a/src/BasicWay/Commands/Math/AddCommand.cs +++ b/src/BasicWay/Commands/Math/AddCommand.cs @@ -3,7 +3,7 @@ namespace BasicWay.Commands.Math; -public class AddCommand : AsyncCommand +public sealed class AddCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, MathSettings settings) { diff --git a/src/BasicWay/Commands/Math/MathSettings.cs b/src/BasicWay/Commands/Math/MathSettings.cs index 125dd0b..5628985 100644 --- a/src/BasicWay/Commands/Math/MathSettings.cs +++ b/src/BasicWay/Commands/Math/MathSettings.cs @@ -3,7 +3,7 @@ namespace BasicWay.Commands.Math; -public class MathSettings : CommandSettings +public sealed class MathSettings : CommandSettings { [CommandArgument(0, "")] public int FirstNumber { get; set; } diff --git a/src/BasicWay/Commands/Math/SubCommand.cs b/src/BasicWay/Commands/Math/SubCommand.cs index f4b28dd..fa6bebc 100644 --- a/src/BasicWay/Commands/Math/SubCommand.cs +++ b/src/BasicWay/Commands/Math/SubCommand.cs @@ -3,7 +3,7 @@ namespace BasicWay.Commands.Math; -public class SubCommand : AsyncCommand +public sealed class SubCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, MathSettings settings) { diff --git a/src/BasicWay/Commands/Path/CountCommand.cs b/src/BasicWay/Commands/Path/CountCommand.cs index cec386f..6e93187 100644 --- a/src/BasicWay/Commands/Path/CountCommand.cs +++ b/src/BasicWay/Commands/Path/CountCommand.cs @@ -3,7 +3,7 @@ namespace BasicWay.Commands.Path; -public class CountCommand : AsyncCommand +public sealed class CountCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, PathSettings settings) { diff --git a/src/BasicWay/Commands/Path/ListCommand.cs b/src/BasicWay/Commands/Path/ListCommand.cs index 3e6aaa6..0d27c9d 100644 --- a/src/BasicWay/Commands/Path/ListCommand.cs +++ b/src/BasicWay/Commands/Path/ListCommand.cs @@ -3,7 +3,7 @@ namespace BasicWay.Commands.Path; -public class ListCommand : AsyncCommand +public sealed class ListCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, PathSettings settings) { diff --git a/src/BasicWay/Commands/Path/PathSettings.cs b/src/BasicWay/Commands/Path/PathSettings.cs index 584a714..ba02900 100644 --- a/src/BasicWay/Commands/Path/PathSettings.cs +++ b/src/BasicWay/Commands/Path/PathSettings.cs @@ -4,7 +4,7 @@ namespace BasicWay.Commands.Path; -public class PathSettings : CommandSettings +public sealed class PathSettings : CommandSettings { [CommandArgument(0, "")] public string PathName { get; set; } diff --git a/src/BasicWay/Common/Spectre/SpectreCommandApp.cs b/src/BasicWay/Common/Spectre/SpectreCommandApp.cs index 88ca425..9d2d586 100644 --- a/src/BasicWay/Common/Spectre/SpectreCommandApp.cs +++ b/src/BasicWay/Common/Spectre/SpectreCommandApp.cs @@ -1,29 +1,28 @@ using Spectre.Console.Cli; -namespace BasicWay.Common.Spectre +namespace BasicWay.Common.Spectre; + +public sealed class SpectreCommandApp : ICommandApp { - public class SpectreCommandApp : ICommandApp - { - private readonly ICommandApp _commandApp; + private readonly ICommandApp _commandApp; - public SpectreCommandApp(ITypeRegistrar registrar) - { - _commandApp = new CommandApp(registrar); - } + public SpectreCommandApp(ITypeRegistrar registrar) + { + _commandApp = new CommandApp(registrar); + } - public void Configure(Action configuration) - { - _commandApp.Configure(configuration); - } + public void Configure(Action configuration) + { + _commandApp.Configure(configuration); + } - public int Run(IEnumerable args) - { - return _commandApp.Run(args); - } + public int Run(IEnumerable args) + { + return _commandApp.Run(args); + } - public Task RunAsync(IEnumerable args) - { - return _commandApp.RunAsync(args); - } + public Task RunAsync(IEnumerable args) + { + return _commandApp.RunAsync(args); } -} +} \ No newline at end of file diff --git a/src/BasicWay/Common/Spectre/SpectreExtensions.cs b/src/BasicWay/Common/Spectre/SpectreExtensions.cs index b219bdb..4630617 100644 --- a/src/BasicWay/Common/Spectre/SpectreExtensions.cs +++ b/src/BasicWay/Common/Spectre/SpectreExtensions.cs @@ -1,18 +1,17 @@ using Spectre.Console; -namespace BasicWay.Common.Spectre +namespace BasicWay.Common.Spectre; + +public static class SpectreExtensions { - public static class SpectreExtensions + public static void RenderException(this T exception) where T : Exception { - public static void RenderException(this T exception) where T : Exception - { - const ExceptionFormats formats = ExceptionFormats.ShortenTypes - | ExceptionFormats.ShortenPaths - | ExceptionFormats.ShortenMethods; + const ExceptionFormats formats = ExceptionFormats.ShortenTypes + | ExceptionFormats.ShortenPaths + | ExceptionFormats.ShortenMethods; - AnsiConsole.WriteLine(); - AnsiConsole.WriteException(exception, formats); - AnsiConsole.WriteLine(); - } + AnsiConsole.WriteLine(); + AnsiConsole.WriteException(exception, formats); + AnsiConsole.WriteLine(); } -} +} \ No newline at end of file diff --git a/src/BasicWay/Program.cs b/src/BasicWay/Program.cs index 7cac97f..8228127 100644 --- a/src/BasicWay/Program.cs +++ b/src/BasicWay/Program.cs @@ -32,25 +32,25 @@ public static async Task Main(string[] args) .AddCommand("add") .WithAlias("addition") .WithDescription("Addition of two numbers") - .WithExample(new[] { "math", "add", "8", "5" }); + .WithExample("math", "add", "8", "5"); math .AddCommand("sub") .WithAlias("subtraction") .WithDescription("Subtraction of two numbers") - .WithExample(new[] { "math", "sub", "6", "2" }); + .WithExample("math", "sub", "6", "2"); }); config.AddBranch("path", path => { path .AddCommand("list") .WithDescription("List directory files") - .WithExample(new[] { "path", "list", "c:/", "-e", "*" }); + .WithExample("path", "list", "c:/", "-e", "*"); path .AddCommand("count") .WithDescription("Count directory files") - .WithExample(new[] { "path", "count", "c:/", "-e", "*" }); + .WithExample("path", "count", "c:/", "-e", "*"); }); }); return await app.RunAsync(args); diff --git a/src/BasicWay/Settings.cs b/src/BasicWay/Settings.cs index 1364287..a12df2b 100644 --- a/src/BasicWay/Settings.cs +++ b/src/BasicWay/Settings.cs @@ -1,10 +1,10 @@ namespace BasicWay; -public class Settings +public sealed class Settings { - public string ApplicationName { get; set; } = "spectre-cli"; + public string ApplicationName { get; init; } = "spectre-console-cli"; - public string ApplicationVersion { get; set; } = "1.0"; + public string ApplicationVersion { get; init; } = "1.0"; public static class ExitCode { diff --git a/src/BasicWay/appsettings.json b/src/BasicWay/appsettings.json index dcbe709..5795db7 100644 --- a/src/BasicWay/appsettings.json +++ b/src/BasicWay/appsettings.json @@ -20,7 +20,7 @@ ] }, "Settings": { - "ApplicationName": "spectre-cli", + "ApplicationName": "spectre-console-cli", "ApplicationVersion": "1.0" } } \ No newline at end of file diff --git a/src/CustomWay/Commands/Math/AddCommand.cs b/src/CustomWay/Commands/Math/AddCommand.cs index ce3bcec..e114a33 100644 --- a/src/CustomWay/Commands/Math/AddCommand.cs +++ b/src/CustomWay/Commands/Math/AddCommand.cs @@ -3,7 +3,7 @@ namespace CustomWay.Commands.Math; -public class AddCommand : AsyncCommand +public sealed class AddCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, MathSettings settings) { diff --git a/src/CustomWay/Commands/Math/MathConfigurator.cs b/src/CustomWay/Commands/Math/MathConfigurator.cs index 9757a71..078e6a4 100644 --- a/src/CustomWay/Commands/Math/MathConfigurator.cs +++ b/src/CustomWay/Commands/Math/MathConfigurator.cs @@ -1,28 +1,27 @@ using CustomWay.Common.Spectre; using Spectre.Console.Cli; -namespace CustomWay.Commands.Math +namespace CustomWay.Commands.Math; + +public sealed class MathConfigurator : ISpectreConfigurator { - public class MathConfigurator : ISpectreConfigurator - { - public int Rank => 1; + public int Rank => 1; - public void Configure(IConfigurator config) + public void Configure(IConfigurator config) + { + config.AddBranch("math", math => { - config.AddBranch("math", math => - { - math - .AddCommand("add") - .WithAlias("addition") - .WithDescription("Addition of two numbers") - .WithExample(new[] { "math", "add", "8", "5" }); + math + .AddCommand("add") + .WithAlias("addition") + .WithDescription("Addition of two numbers") + .WithExample("math", "add", "8", "5"); - math - .AddCommand("sub") - .WithAlias("subtraction") - .WithDescription("Subtraction of two numbers") - .WithExample(new[] { "math", "sub", "6", "2" }); - }); - } + math + .AddCommand("sub") + .WithAlias("subtraction") + .WithDescription("Subtraction of two numbers") + .WithExample("math", "sub", "6", "2"); + }); } -} +} \ No newline at end of file diff --git a/src/CustomWay/Commands/Math/MathSettings.cs b/src/CustomWay/Commands/Math/MathSettings.cs index a21874b..37766dd 100644 --- a/src/CustomWay/Commands/Math/MathSettings.cs +++ b/src/CustomWay/Commands/Math/MathSettings.cs @@ -3,7 +3,7 @@ namespace CustomWay.Commands.Math; -public class MathSettings : CommandSettings +public sealed class MathSettings : CommandSettings { [CommandArgument(0, "")] public int FirstNumber { get; set; } diff --git a/src/CustomWay/Commands/Math/SubCommand.cs b/src/CustomWay/Commands/Math/SubCommand.cs index b316e4d..c22b893 100644 --- a/src/CustomWay/Commands/Math/SubCommand.cs +++ b/src/CustomWay/Commands/Math/SubCommand.cs @@ -3,7 +3,7 @@ namespace CustomWay.Commands.Math; -public class SubCommand : AsyncCommand +public sealed class SubCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, MathSettings settings) { diff --git a/src/CustomWay/Commands/Path/CountCommand.cs b/src/CustomWay/Commands/Path/CountCommand.cs index 574c6de..de04b7d 100644 --- a/src/CustomWay/Commands/Path/CountCommand.cs +++ b/src/CustomWay/Commands/Path/CountCommand.cs @@ -3,7 +3,7 @@ namespace CustomWay.Commands.Path; -public class CountCommand : AsyncCommand +public sealed class CountCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, PathSettings settings) { diff --git a/src/CustomWay/Commands/Path/ListCommand.cs b/src/CustomWay/Commands/Path/ListCommand.cs index 7bdf021..f446610 100644 --- a/src/CustomWay/Commands/Path/ListCommand.cs +++ b/src/CustomWay/Commands/Path/ListCommand.cs @@ -3,7 +3,7 @@ namespace CustomWay.Commands.Path; -public class ListCommand : AsyncCommand +public sealed class ListCommand : AsyncCommand { public override Task ExecuteAsync(CommandContext context, PathSettings settings) { diff --git a/src/CustomWay/Commands/Path/PathConfigurator.cs b/src/CustomWay/Commands/Path/PathConfigurator.cs index c15d93e..a5185a2 100644 --- a/src/CustomWay/Commands/Path/PathConfigurator.cs +++ b/src/CustomWay/Commands/Path/PathConfigurator.cs @@ -1,26 +1,25 @@ using CustomWay.Common.Spectre; using Spectre.Console.Cli; -namespace CustomWay.Commands.Path +namespace CustomWay.Commands.Path; + +public sealed class PathConfigurator : ISpectreConfigurator { - public class PathConfigurator : ISpectreConfigurator - { - public int Rank => 2; + public int Rank => 2; - public void Configure(IConfigurator config) + public void Configure(IConfigurator config) + { + config.AddBranch("path", path => { - config.AddBranch("path", path => - { - path - .AddCommand("list") - .WithDescription("List directory files") - .WithExample(new[] { "path", "list", "c:/", "-e", "*" }); + path + .AddCommand("list") + .WithDescription("List directory files") + .WithExample("path", "list", "c:/", "-e", "*"); - path - .AddCommand("count") - .WithDescription("Count directory files") - .WithExample(new[] { "path", "count", "c:/", "-e", "*" }); - }); - } + path + .AddCommand("count") + .WithDescription("Count directory files") + .WithExample("path", "count", "c:/", "-e", "*"); + }); } -} +} \ No newline at end of file diff --git a/src/CustomWay/Commands/Path/PathSettings.cs b/src/CustomWay/Commands/Path/PathSettings.cs index bcc7bdb..dbf75a8 100644 --- a/src/CustomWay/Commands/Path/PathSettings.cs +++ b/src/CustomWay/Commands/Path/PathSettings.cs @@ -4,7 +4,7 @@ namespace CustomWay.Commands.Path; -public class PathSettings : CommandSettings +public sealed class PathSettings : CommandSettings { [CommandArgument(0, "")] public string PathName { get; set; } diff --git a/src/CustomWay/Common/Extensions/LoggingExtensions.cs b/src/CustomWay/Common/Extensions/LoggingExtensions.cs index 429adf6..69c5e46 100644 --- a/src/CustomWay/Common/Extensions/LoggingExtensions.cs +++ b/src/CustomWay/Common/Extensions/LoggingExtensions.cs @@ -9,7 +9,7 @@ namespace CustomWay.Common.Extensions; public static class LoggingExtensions { - private const string DefaultCategoryName = "SpectreCliDemo"; + private const string DefaultCategoryName = "SpectreConsoleCliDemo"; public static void AddDefaultLogger(this ILoggingBuilder loggingBuilder) { diff --git a/src/CustomWay/Common/Spectre/SpectreCommandApp.cs b/src/CustomWay/Common/Spectre/SpectreCommandApp.cs index 4b39e91..ea8062b 100644 --- a/src/CustomWay/Common/Spectre/SpectreCommandApp.cs +++ b/src/CustomWay/Common/Spectre/SpectreCommandApp.cs @@ -1,29 +1,28 @@ using Spectre.Console.Cli; -namespace CustomWay.Common.Spectre +namespace CustomWay.Common.Spectre; + +public sealed class SpectreCommandApp : ICommandApp { - public class SpectreCommandApp : ICommandApp - { - private readonly ICommandApp _commandApp; + private readonly ICommandApp _commandApp; - public SpectreCommandApp(ITypeRegistrar registrar) - { - _commandApp = new CommandApp(registrar); - } + public SpectreCommandApp(ITypeRegistrar registrar) + { + _commandApp = new CommandApp(registrar); + } - public void Configure(Action configuration) - { - _commandApp.Configure(configuration); - } + public void Configure(Action configuration) + { + _commandApp.Configure(configuration); + } - public int Run(IEnumerable args) - { - return _commandApp.Run(args); - } + public int Run(IEnumerable args) + { + return _commandApp.Run(args); + } - public Task RunAsync(IEnumerable args) - { - return _commandApp.RunAsync(args); - } + public Task RunAsync(IEnumerable args) + { + return _commandApp.RunAsync(args); } -} +} \ No newline at end of file diff --git a/src/CustomWay/Common/Spectre/SpectreExtensions.cs b/src/CustomWay/Common/Spectre/SpectreExtensions.cs index 7e56285..aec4a03 100644 --- a/src/CustomWay/Common/Spectre/SpectreExtensions.cs +++ b/src/CustomWay/Common/Spectre/SpectreExtensions.cs @@ -4,50 +4,46 @@ using Spectre.Console; using Spectre.Console.Cli; -namespace CustomWay.Common.Spectre +namespace CustomWay.Common.Spectre; + +public static class SpectreExtensions { - public static class SpectreExtensions + public static async Task RunSpectreCliAsync(this IHostBuilder hostBuilder, string[] args) { - public static async Task RunSpectreCliAsync(this IHostBuilder hostBuilder, string[] args) - { - using var host = hostBuilder.Build(); - var app = host.Services.GetRequiredService(); - var settings = host.Services.GetRequiredService>().Value; - var configurators = host.Services.GetServices(); - app.Configure(config => - { - config.ValidateExamples(); - config.PropagateExceptions(); - config.SetApplicationName(settings.ApplicationName); - config.SetApplicationVersion(settings.ApplicationVersion); - foreach (var configurator in configurators.OrderBy(x => x.Rank)) - { - configurator.Configure(config); - } - }); - return await app.RunAsync(args); - } - - public static async Task RunSpectreCliAsync(this IHostBuilder hostBuilder, string[] args, Action configureCommandApp) + using var host = hostBuilder.Build(); + var app = host.Services.GetRequiredService(); + var settings = host.Services.GetRequiredService>().Value; + var configurators = host.Services.GetServices(); + app.Configure(config => { - using var host = hostBuilder.Build(); - var app = host.Services.GetRequiredService(); - if (configureCommandApp != null) + config.ValidateExamples(); + config.PropagateExceptions(); + config.SetApplicationName(settings.ApplicationName); + config.SetApplicationVersion(settings.ApplicationVersion); + foreach (var configurator in configurators.OrderBy(x => x.Rank)) { - app.Configure(configureCommandApp); + configurator.Configure(config); } - return await app.RunAsync(args); - } + }); + return await app.RunAsync(args); + } - public static void RenderException(this T exception) where T : Exception + public static async Task RunSpectreCliAsync(this IHostBuilder hostBuilder, string[] args, Action configureCommandApp) + { + using var host = hostBuilder.Build(); + var app = host.Services.GetRequiredService(); + if (configureCommandApp != null) { - const ExceptionFormats formats = ExceptionFormats.ShortenTypes - | ExceptionFormats.ShortenPaths - | ExceptionFormats.ShortenMethods; - - AnsiConsole.WriteLine(); - AnsiConsole.WriteException(exception, formats); - AnsiConsole.WriteLine(); + app.Configure(configureCommandApp); } + return await app.RunAsync(args); + } + + public static void RenderException(this T exception) where T : Exception + { + const ExceptionFormats formats = ExceptionFormats.ShortenEverything; + AnsiConsole.WriteLine(); + AnsiConsole.WriteException(exception, formats); + AnsiConsole.WriteLine(); } -} +} \ No newline at end of file diff --git a/src/CustomWay/CustomWay.csproj b/src/CustomWay/CustomWay.csproj index 0172886..6727799 100644 --- a/src/CustomWay/CustomWay.csproj +++ b/src/CustomWay/CustomWay.csproj @@ -7,13 +7,13 @@ - - + + - - - - + + + + diff --git a/src/CustomWay/Settings.cs b/src/CustomWay/Settings.cs index 0e0d5da..579e090 100644 --- a/src/CustomWay/Settings.cs +++ b/src/CustomWay/Settings.cs @@ -1,10 +1,10 @@ namespace CustomWay; -public class Settings +public sealed class Settings { - public string ApplicationName { get; set; } = "spectre-cli"; + public string ApplicationName { get; init; } = "spectre-console-cli"; - public string ApplicationVersion { get; set; } = "1.0"; + public string ApplicationVersion { get; init; } = "1.0"; public static class ExitCode { diff --git a/src/CustomWay/appsettings.json b/src/CustomWay/appsettings.json index dcbe709..5795db7 100644 --- a/src/CustomWay/appsettings.json +++ b/src/CustomWay/appsettings.json @@ -20,7 +20,7 @@ ] }, "Settings": { - "ApplicationName": "spectre-cli", + "ApplicationName": "spectre-console-cli", "ApplicationVersion": "1.0" } } \ No newline at end of file