From 9c4f12d45e81f73fd77e9055b472cf19c291d209 Mon Sep 17 00:00:00 2001 From: Alek-ban Date: Mon, 16 Feb 2026 12:46:35 +0300 Subject: [PATCH] feat: command line arguments parsing --- GSS2.Test/GSS2.Test.csproj | 1 + GSS2.Test/Program.cs | 133 ++++++++++--------------------- GSS2.Test/ProgramCameraTuning.cs | 57 +++++++++++++ GSS2.Test/PropgramUi.cs | 104 ++++++++++++++++++++++++ 4 files changed, 203 insertions(+), 92 deletions(-) create mode 100644 GSS2.Test/ProgramCameraTuning.cs create mode 100644 GSS2.Test/PropgramUi.cs diff --git a/GSS2.Test/GSS2.Test.csproj b/GSS2.Test/GSS2.Test.csproj index e3979a0..e6c5488 100644 --- a/GSS2.Test/GSS2.Test.csproj +++ b/GSS2.Test/GSS2.Test.csproj @@ -24,6 +24,7 @@ All + diff --git a/GSS2.Test/Program.cs b/GSS2.Test/Program.cs index 46e20f6..5adbb80 100644 --- a/GSS2.Test/Program.cs +++ b/GSS2.Test/Program.cs @@ -1,48 +1,61 @@ -using System.Reflection; - -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging.Console; - using Avalonia; using Avalonia.OpenGL.Egl; -using GSS2.Core; -using GSS2.Core.Logging; -using GSS2.Core.Analysis.AmineContent.Database; - -using ConsoleFormatter = GSS2.Core.Logging.ConsoleFormatter; +using System.CommandLine; namespace GSS2.Test; -public class Program +public partial class Program { - public static IHost ApplicationHost { get; } = BuildHostApp(); + private enum RenderingMode + { + X11, + DRM + } + + private static string[]? _args = null; + private static readonly RootCommand _rootCommand = new("GranuSight Software 2"); + private static readonly Command _cameraTuningCommand = new("camera-tuning") + { + Description = "Tune camera parameters" + }; + private static readonly Option _renderingModeArgument = new("--rendering-mode") + { + Description = "Rendering mode to use", + DefaultValueFactory = (_) => RenderingMode.X11 + }; + private static readonly Option _hideConsoleArgument = new("--hide-console") + { + Description = "Suppress console output", + DefaultValueFactory = (result) => result.GetRequiredValue(_renderingModeArgument) == RenderingMode.DRM + }; + public static event ConsoleCancelEventHandler? ShutdownRequested; [STAThread] public static int Main(string[] args) { - var builder = BuildAvaloniaApp(); + _args = args; - if (args.Contains("--drm")) - { - if (!args.Contains("--console")) - SilenceConsole(); - return builder.StartLinuxDrm(args, "/dev/dri/card1", 1.0); - } - if (args.Contains("--help")) - { - Console.WriteLine($"Usage of {Assembly.GetEntryAssembly()?.GetName().Name}:"); - Console.WriteLine($"\t--help - show this help"); - Console.WriteLine($"\t--drm - use DRM rendering mode"); - Console.WriteLine($"\t--console - in DRM rendering mode do not suppress console output"); - return 0; - } + _rootCommand.Add(_cameraTuningCommand); + _rootCommand.Add(_renderingModeArgument); + _rootCommand.Add(_hideConsoleArgument); + + _rootCommand.SetAction(Ui); + _cameraTuningCommand.SetAction(CameraTuning); Console.CancelKeyPress += new ConsoleCancelEventHandler(OnProgramShutdown); Console.WriteLine("Press Ctrl+C to shut down."); - return builder.StartWithClassicDesktopLifetime(args); + var parseResult = _rootCommand.Parse(_args); + if (parseResult.Errors.Count() != 0) + { + foreach (var parseError in parseResult.Errors) + Console.Error.WriteLine(parseError.Message); + return 1; + } + + return parseResult.Invoke(); } private static void OnProgramShutdown(object? sender, ConsoleCancelEventArgs ea) @@ -50,68 +63,4 @@ public class Program ShutdownRequested?.Invoke(sender, ea); ea.Cancel = true; } - - private static void SilenceConsole() - { - new Thread(() => - { - Console.CursorVisible = false; - while (true) - Console.ReadKey(true); - }) - { IsBackground = true }.Start(); - } - - private static IHost BuildHostApp() - { - var builder = Host.CreateApplicationBuilder(); - - builder.Logging.ClearProviders(); - builder.Logging.AddConsole(options => options.FormatterName = nameof(ConsoleFormatter)); - builder.Logging.AddConsoleFormatter(); - builder.Logging.AddProvider(new FileLoggerProvider("full.log", true)); - builder.Logging.AddProvider(new FileLoggerProvider("last_run.log", false)); - builder.Services.AddSingleton(); - - builder.Services.AddAmineContentCalibrationContext(builder.Configuration); - builder.Services.AddAmineContentResultsContext(builder.Configuration); - - builder.Services.AddLightsService("Hardware:Lights"); - builder.Services.AddTemperatureHumidityService("Hardware:TemperatureHumidity"); - builder.Services.AddIlluminatorService("Hardware:Illuminator"); - builder.Services.AddCameraService("Hardware:Camera", true); - builder.Services.AddComputeResourcesService(); - - builder.Services.AddViewsAndModels(); - - var host = builder.Build(); - host.Services.GetRequiredService(); - - using (var scope = host.Services.CreateScope()) - { - var context = scope.ServiceProvider.GetRequiredService(); - context.Database.Migrate(); - } - using (var scope = host.Services.CreateScope()) - { - var context = scope.ServiceProvider.GetRequiredService(); - context.Database.Migrate(); - } - - return host; - } - - private static AppBuilder BuildAvaloniaApp() - => AppBuilder.Configure() - .UsePlatformDetect() - .UseSkia() - .LogToTrace() - .With(new X11PlatformOptions - { - RenderingMode = [X11RenderingMode.Egl] - }) - .With(new EglDisplayOptions - { - SupportsContextSharing = true - }); } diff --git a/GSS2.Test/ProgramCameraTuning.cs b/GSS2.Test/ProgramCameraTuning.cs new file mode 100644 index 0000000..dca597e --- /dev/null +++ b/GSS2.Test/ProgramCameraTuning.cs @@ -0,0 +1,57 @@ +using System.CommandLine; + +using GSS2.Core; +using GSS2.Core.Hardware; +using GSS2.Core.Logging; + +using Microsoft.Extensions.Logging.Console; + +using ConsoleFormatter = GSS2.Core.Logging.ConsoleFormatter; + +namespace GSS2.Test; + +public partial class Program +{ + private class CameraTuningWorker : BackgroundService + { + private readonly ILogger _logger; + private readonly IHostApplicationBuilder _applicationLifetime; + private readonly CameraService _cameraService; + private readonly IlluminatorService _illuminatorService; + + public CameraTuningWorker(ILogger logger, IHostApplicationBuilder applicationLifetime, CameraService cameraService, IlluminatorService illuminatorService) + { + _applicationLifetime = applicationLifetime; + _logger = logger; + _cameraService = cameraService; + _illuminatorService = illuminatorService; + _logger.LogInformation("Initialized"); + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + _logger.LogInformation("Running camera fine tuning"); + } + } + private static void CameraTuning(ParseResult parseResult) + { + var builder = Host.CreateApplicationBuilder(); + + builder.Logging.ClearProviders(); + builder.Logging.AddConsole(options => options.FormatterName = nameof(ConsoleFormatter)); + builder.Logging.AddConsoleFormatter(); + builder.Logging.AddProvider(new FileLoggerProvider("full.log", true)); + builder.Logging.AddProvider(new FileLoggerProvider("last_run.log", false)); + builder.Services.AddSingleton(); + + builder.Services.AddIlluminatorService("Hardware:Illuminator"); + builder.Services.AddCameraService("Hardware:Camera", true); + + builder.Services.AddHostedService(); + + var host = builder.Build(); + host.Services.GetRequiredService(); + + host.Run(); + } +} diff --git a/GSS2.Test/PropgramUi.cs b/GSS2.Test/PropgramUi.cs new file mode 100644 index 0000000..b765001 --- /dev/null +++ b/GSS2.Test/PropgramUi.cs @@ -0,0 +1,104 @@ +using System.CommandLine; + +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Console; + +using Avalonia; +using Avalonia.OpenGL.Egl; + +using GSS2.Core; +using GSS2.Core.Logging; +using GSS2.Core.Analysis.AmineContent.Database; + +using ConsoleFormatter = GSS2.Core.Logging.ConsoleFormatter; + +namespace GSS2.Test; + +public partial class Program +{ + public static IHost ApplicationHost { get; } = BuildHostApp(); + + private static void Ui(ParseResult parseResult) + { + var builder = BuildAvaloniaApp(); + var renderingMode = parseResult.GetValue(_renderingModeArgument); + var hideConsole = parseResult.GetValue(_hideConsoleArgument); + + if (hideConsole) + SilenceConsole(); + + switch (renderingMode) + { + case RenderingMode.X11: + builder.StartWithClassicDesktopLifetime(_args ?? []); + break; + case RenderingMode.DRM: + builder.StartLinuxDrm(_args ?? [], "/dev/dri/card1", 1.0); + break; + } + } + + private static void SilenceConsole() + { + new Thread(() => + { + Console.CursorVisible = false; + while (true) + Console.ReadKey(true); + }) + { IsBackground = true }.Start(); + } + + private static IHost BuildHostApp() + { + var builder = Host.CreateApplicationBuilder(); + + builder.Logging.ClearProviders(); + builder.Logging.AddConsole(options => options.FormatterName = nameof(ConsoleFormatter)); + builder.Logging.AddConsoleFormatter(); + builder.Logging.AddProvider(new FileLoggerProvider("full.log", true)); + builder.Logging.AddProvider(new FileLoggerProvider("last_run.log", false)); + builder.Services.AddSingleton(); + + builder.Services.AddAmineContentCalibrationContext(builder.Configuration); + builder.Services.AddAmineContentResultsContext(builder.Configuration); + + builder.Services.AddLightsService("Hardware:Lights"); + builder.Services.AddTemperatureHumidityService("Hardware:TemperatureHumidity"); + builder.Services.AddIlluminatorService("Hardware:Illuminator"); + builder.Services.AddCameraService("Hardware:Camera", true); + builder.Services.AddComputeResourcesService(); + + builder.Services.AddViewsAndModels(); + + var host = builder.Build(); + host.Services.GetRequiredService(); + + using (var scope = host.Services.CreateScope()) + { + var context = scope.ServiceProvider.GetRequiredService(); + context.Database.Migrate(); + } + using (var scope = host.Services.CreateScope()) + { + var context = scope.ServiceProvider.GetRequiredService(); + context.Database.Migrate(); + } + + return host; + } + + private static AppBuilder BuildAvaloniaApp() + => AppBuilder.Configure() + .UsePlatformDetect() + .UseSkia() + .LogToTrace() + .With(new X11PlatformOptions + { + RenderingMode = [X11RenderingMode.Egl] + }) + .With(new EglDisplayOptions + { + SupportsContextSharing = true + }); +}