using System.ComponentModel.DataAnnotations; using System.Diagnostics; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Shelldon; using Shelldon.Attributes; using Shelldon.Logging; using Shelldon.Middleware; using Shelldon.Services; using Shelldon.Utils; ShellApplicationBuilder builder = ShellApplication.CreateBuilder(args); builder.Logging.SetMinimumLevel(LogLevel.Trace); builder.Services.AddSingleton(); ShellApplication app = builder.Build(); app.UseConsoleLogger(); app .UseGlobalOptions() .UseHelpBanner() .MapHelpOption() .MapVersionOption("version"); app.MapCommand((ILogger logger, CommandContext context) => { logger.LogTrace("This is a trace log message."); logger.LogDebug("This is a debug log message."); logger.LogInformation("This is an information log message."); logger.LogWarning("This is a warning log message."); logger.LogError("This is an error log message."); logger.LogCritical("This is a critical log message."); Console.WriteLine("This is default command."); }); app.MapCommand("serviceprovider", (IServiceProvider services) => Console.WriteLine(services.GetRequiredService().Message)); app.MapCommand("service", (TestService testService) => Console.WriteLine(testService.Message)); app.MapCommand("test", () => Console.WriteLine("Test command")); app.MapCommand("arg1", (bool? isTrue) => Console.WriteLine("The Universe says: " + (isTrue == true ? "Yes" : "No"))); app.MapCommand("arg3", (Guid id, int count, decimal price) => Console.WriteLine($"ID: {id}, Count: {count}, Price: {price}")); app.MapCommand("mixed-args", ([FromArgument] Guid id, [FromOption(ShortNames = "c")] int count, TestService service, decimal price) => Console.WriteLine($"ID: {id}, Count: {count}, Service Message: {service.Message}, Price: {price}")); app.MapCommand("string", ([FromArgument] string text) => Console.WriteLine($"Simon says: '{text}'")); app.MapCommand("arg-order", ([FromArgument(Order = 2)] string arg2, [FromArgument(Order = 3)] string arg3, [FromArgument(Order = 1)] string arg1) => { Console.WriteLine($"Arg1: {arg1}, Arg2: {arg2}, Arg3: {arg3}"); }); app.MapCommand("arg-array", ([FromArgument] string arg1, [FromArgument(Order = 1, Description = "Array")] string[] arg2, [FromArgument, Hidden] string arg3, [FromArgument] string arg4) => Console.WriteLine($"Arg1: {arg1}, Arg2: [{string.Join(", ", arg2)}], Arg3: {arg3}, Arg4: {arg4}")); app.MapCommand("help", (CommandTreeProvider treeProvider) => { // Display Usage: filename command [arguments] Console.WriteLine($"Usage: "); }); app.MapCommand("arg-test", ([FromArgument] int[] array) => { // Console.WriteLine($"count: {count}"); // Console.WriteLine($"price: {price}"); Console.WriteLine($"array: [{string.Join(", ", array)}]"); }); app.MapCommand("validation", ( [FromOption, Required, MinLength(5), MaxLength(10)] string name, [FromArgument, Range(1, 100)] int count, [FromArgument] double price ) => { Console.WriteLine("All validation checks passed."); }); app.MapCommand("test1", ([FromArgument] Guid id, string?[]? name, int count, [Hidden] decimal price, [Hidden] bool accept, TestService service) => { Console.WriteLine($"ID: {id}, Name: {name}, Count: {count}, Price: {price}, Accept: {accept}, Service Message: {service.Message}"); }); /* app.Use(async (next) => { Console.WriteLine("Waiting for 2 seconds before executing the next middleware..."); await Task.Delay(2000); await next(); }); */ app.MapCommand("stopwatch", async (CancellationToken token, int time, bool noToken) => { Console.WriteLine("Use Ctrl+C to stop the stopwatch."); int elapsedSeconds = 0; Console.WriteLine(noToken); do { Console.WriteLine($"Elapsed seconds: {elapsedSeconds} (cancellation requested: {token.IsCancellationRequested})"); await Task.Delay(1000); } while ((!token.IsCancellationRequested || noToken) && ++elapsedSeconds < time); if (token.IsCancellationRequested) Console.WriteLine("Stopwatch stopped by user."); else Console.WriteLine("Stopwatch completed."); return 1; }); app.MapSection("docker", docker => { docker.MapGlobalOption("test", "t", next => { Console.WriteLine("Pre-action: --test"); next(); Console.WriteLine("Post-action: --test"); }) .Scoped(); docker.MapSection("compose", compose => { compose.WithDescription("Define and run multi-container applications with Docker"); compose.WithExample("docker compose up -d --build"); compose.MapCommand(() => Console.WriteLine("Docker Compose Default Command Executed")) .WithDescription("Run docker compose command") .WithExample("docker compose"); compose.MapCommand("up", () => Console.WriteLine("Docker Compose Up Command Executed")) .WithDescription("Create and start containers"); compose.MapCommand("down", () => Console.WriteLine("Docker Compose Down Command Executed")) .WithDescription("Stop and remove containers, networks"); compose.MapCommand("error", () => 15) .WithDescription("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."); }); docker.MapCommand("build", (bool test) => Console.WriteLine($"Docker Build Command Executed ({test})")); docker.MapCommand(([FromArgument] string imageName, bool test) => { Console.WriteLine($"Docker Image Command Executed for Image: {imageName}."); }); }); app.Use((context, next) => { Console.WriteLine("Resolution path:".FormatConsole(false, ConsoleForegroundColor.BrightWhite)); Console.WriteLine(string.Join(" -> ", [.. context.ResolutionPath.Select(x => x.Name), context.Command?.Name ?? "[X]"]).FormatConsole(false, ConsoleForegroundColor.BrightWhite)); next(); }); app.Run(); public class TestService(CommandContext context) { private readonly CommandContext _context = context; public string Message => $"Hello from TestService! (Current command: {_context.CommandName})"; }