From ad12633c463ebc886424b3938045bc8c88562416 Mon Sep 17 00:00:00 2001 From: Alexey Nesterov Date: Tue, 12 Sep 2023 11:07:51 +1000 Subject: [PATCH] Renamed: appsettings.json appsettings.default.json Change: default param are actual for works Added: cmdline parser with options --- App.cs | 1 - Program.cs | 56 ++++++++++++++++++-- ProgramArguments.cs | 25 +++++++++ XpadControl.csproj | 7 ++- appsettings.json => appsettings.default.json | 11 ++-- 5 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 ProgramArguments.cs rename appsettings.json => appsettings.default.json (68%) diff --git a/App.cs b/App.cs index 504145b..49cd938 100644 --- a/App.cs +++ b/App.cs @@ -2,7 +2,6 @@ using Microsoft.Extensions.Hosting; using System; using System.Text.Json; -using System.Text.Json.Serialization; using System.Threading; using System.Threading.Tasks; using XpadControl.Interfaces.GamepadService; diff --git a/Program.cs b/Program.cs index cd3d0bf..b0c6ede 100644 --- a/Program.cs +++ b/Program.cs @@ -14,22 +14,39 @@ using WindowsGamepadService = XpadControl.Windows.Services.GamepadService.GamepadService; using WindowsGamepadHostedService = XpadControl.Windows.Services.GamepadService.GamepadHostedService; using System.Configuration; -using static System.Runtime.InteropServices.JavaScript.JSType; +using System.Reflection; namespace XpadControl { public class Program { + private static ProgramArguments mProgramArguments; + static void Main(string[] args) { + try + { + if (!ParseArguments(args)) + return; + } + catch(FileNotFoundException e) + { + Console.WriteLine(e.Message); + return; + } + catch(Exception e) + { + Console.WriteLine(e.Message); + return; + } + IConfigurationRoot configuration = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json") + .AddJsonFile(mProgramArguments.ConfigPathName) .Build(); var loogerService = new LoggerService(configuration); - HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); + HostApplicationBuilder builder = Host.CreateApplicationBuilder(); builder.Logging.ClearProviders(); builder.Configuration.AddConfiguration(configuration); @@ -86,5 +103,36 @@ private static Uri ReadUriFromSettings(IConfigurationRoot configuration) Uri uri = new(ws); return uri; } + + private static bool ParseArguments(string[] args) + { + mProgramArguments = new(); + CommandLineParser.CommandLineParser parser = new() + { + IgnoreCase = true, + }; + + parser.ExtractArgumentAttributes(mProgramArguments); + parser.ParseCommandLine(args); + + if (mProgramArguments.ShowVersion) + { + Version v = Assembly.GetExecutingAssembly().GetName().Version; + Console.WriteLine($"{v}"); + return false; + } + + if (mProgramArguments.ShowHelp) + { + parser.ShowUsage(); + return false; + } + + if (!Path.Exists(mProgramArguments.ConfigPathName)) + throw new FileNotFoundException($"Cannot find app settings file {mProgramArguments.ConfigPathName}"); + + return true; + + } } } \ No newline at end of file diff --git a/ProgramArguments.cs b/ProgramArguments.cs new file mode 100644 index 0000000..c24de7d --- /dev/null +++ b/ProgramArguments.cs @@ -0,0 +1,25 @@ +using CommandLineParser.Arguments; + +namespace XpadControl +{ + internal class ProgramArguments + { + internal const string DefaultConfigName = $"appsettings.default.json"; + + [ValueArgument(typeof(string), 'c', + LongName = "config", + Description = "Set the path to the settings file", + DefaultValue = DefaultConfigName, + ValueOptional = true)] + public string ConfigPathName; + + [SwitchArgument('v', "version", + defaultValue: false, + Description = "Show app version")] + public bool ShowVersion; + + [SwitchArgument('h', "help", defaultValue: false, + Description = "Show this help")] + public bool ShowHelp; + } +} diff --git a/XpadControl.csproj b/XpadControl.csproj index 581388a..af44bc9 100644 --- a/XpadControl.csproj +++ b/XpadControl.csproj @@ -5,6 +5,7 @@ disable disable net7.0-windows + 1.0.0.1 @@ -13,22 +14,26 @@ + + + + @@ -44,7 +49,7 @@ - + Always diff --git a/appsettings.json b/appsettings.default.json similarity index 68% rename from appsettings.json rename to appsettings.default.json index b8d1298..09987bd 100644 --- a/appsettings.json +++ b/appsettings.default.json @@ -1,9 +1,8 @@ { "Serilog": { - "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ], - "MinimumLevel": "Debug", + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Verbose", "WriteTo": [ - { "Name": "Console" }, { "Name": "File", "Args": { "path": "Logs/log.txt" } @@ -25,8 +24,8 @@ ] }, "AppSettings": { - "WebSocketHost": "10.254.254.230", - "WebSocketPort": "9001", - "WebSocketPath": "/adam-2.7/debug" + "WebSocketHost": "127.0.0.1", + "WebSocketPort": "8000", + "WebSocketPath": "/adam-2.7/movement" } } \ No newline at end of file