Skip to content

Commit

Permalink
Renamed: appsettings.json appsettings.default.json
Browse files Browse the repository at this point in the history
Change: default param are actual for works
Added: cmdline parser with options
  • Loading branch information
vertigra committed Sep 12, 2023
1 parent c149806 commit ad12633
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
1 change: 0 additions & 1 deletion App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
56 changes: 52 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

}
}
}
25 changes: 25 additions & 0 deletions ProgramArguments.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 6 additions & 1 deletion XpadControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<TargetFramework>net7.0-windows</TargetFramework>
<AssemblyVersion>1.0.0.1</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand All @@ -13,22 +14,26 @@
<Compile Remove="XpadControl.Linux.Services\**" />
<Compile Remove="XpadControl.Math.Tests\**" />
<Compile Remove="XpadControl.Services.Tests\**" />
<Compile Remove="XpadControl.Services\**" />
<Compile Remove="XpadControl.Windows.Services\**" />
<EmbeddedResource Remove="XpadControl.Common.Services\**" />
<EmbeddedResource Remove="XpadControl.Interfaces\**" />
<EmbeddedResource Remove="XpadControl.Linux.Services\**" />
<EmbeddedResource Remove="XpadControl.Math.Tests\**" />
<EmbeddedResource Remove="XpadControl.Services.Tests\**" />
<EmbeddedResource Remove="XpadControl.Services\**" />
<EmbeddedResource Remove="XpadControl.Windows.Services\**" />
<None Remove="XpadControl.Common.Services\**" />
<None Remove="XpadControl.Interfaces\**" />
<None Remove="XpadControl.Linux.Services\**" />
<None Remove="XpadControl.Math.Tests\**" />
<None Remove="XpadControl.Services.Tests\**" />
<None Remove="XpadControl.Services\**" />
<None Remove="XpadControl.Windows.Services\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommandLineArgumentsParser" Version="3.0.23" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
Expand All @@ -44,7 +49,7 @@
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<None Update="appsettings.default.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
11 changes: 5 additions & 6 deletions appsettings.json → appsettings.default.json
Original file line number Diff line number Diff line change
@@ -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" }
Expand All @@ -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"
}
}

0 comments on commit ad12633

Please sign in to comment.