Skip to content

Commit

Permalink
Switching PowArgs to Spectre.Console.Cli and fixing console apps trim…
Browse files Browse the repository at this point in the history
…ming
  • Loading branch information
geeooff committed Mar 18, 2024
1 parent a644146 commit 79338a2
Show file tree
Hide file tree
Showing 31 changed files with 505 additions and 10,837 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Console/bin/Debug/net8.0/ForzaData.Console.dll",
"args": [
"-ServerIpAddress", "${input:serverIpAddress}",
"-Port", "${input:clientPortNumber}"
"--server", "${input:serverIpAddress}",
"--port", "${input:clientPortNumber}"
],
"cwd": "${workspaceFolder}/Console",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
15 changes: 0 additions & 15 deletions Console/Arguments.cs

This file was deleted.

38 changes: 38 additions & 0 deletions Console/DefaultCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace ForzaData.Console;

public sealed class DefaultCommand : Command<DefaultCommandSettings>
{
public override int Execute([NotNull] CommandContext context, [NotNull] DefaultCommandSettings settings)
{
using var cancellationTokenSource = new CancellationTokenSource();
using var listener = new ForzaDataListener((int)settings.Port!, settings.Server!);

// cancellation provided by CTRL + C / CTRL + break
System.Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
cancellationTokenSource.Cancel();
};

// forza data observer
var console = new ForzaDataConsole();
console.Subscribe(listener);

try
{
System.Console.WriteLine($"Listening for data from {settings.Server} to local port {settings.Port}...");
System.Console.WriteLine($"Please press CTRL+C to stop listening");

// forza data observable
listener.Listen(cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
// user cancellation requested
}

console.Unsubscribe();

return 0;
}
}
28 changes: 28 additions & 0 deletions Console/DefaultCommandSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.ComponentModel;
using System.Net;

namespace ForzaData.Console;

public sealed class DefaultCommandSettings : CommandSettings
{
[Description("IP of server (your PC or console running the game) to listen to")]
[CommandOption("-s|--server <server>")]
[TypeConverter(typeof(IPAddressTypeConverter))]
public IPAddress? Server { get; set; }

[Description("Local network port to listen on")]
[CommandOption("-p|--port <port>")]
public ushort? Port { get; set; }

public override ValidationResult Validate()
{
if (Server is null)
return ValidationResult.Error("The server IP address is required");
if (Port is null)
return ValidationResult.Error("The local network port number is required");
if (Port is < 1024 or > 65535)
return ValidationResult.Error("The local network port number must be in 1024-65535 range");

return ValidationResult.Success();
}
}
4 changes: 3 additions & 1 deletion Console/ForzaData.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TrimMode>partial</TrimMode>
</PropertyGroup>

<ItemGroup>
<None Remove="packages.lock.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="PowArgs" Version="1.1.0" />
<PackageReference Include="Spectre.Console" Version="0.48.0" />
<PackageReference Include="Spectre.Console.Cli" Version="0.48.0" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions Console/ForzaDataConsole.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using ForzaData.Core;
using System.Diagnostics;

namespace ForzaData.Console;
namespace ForzaData.Console;

public class ForzaDataConsole : ForzaDataObserver
{
Expand Down
5 changes: 5 additions & 0 deletions Console/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global using ForzaData.Core;
global using Spectre.Console;
global using Spectre.Console.Cli;
global using System.Diagnostics;
global using System.Diagnostics.CodeAnalysis;
94 changes: 8 additions & 86 deletions Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,88 +1,10 @@
using ForzaData.Core;
using System.Net;
using System.Text;
namespace ForzaData.Console;

namespace ForzaData.Console;

class Program
static class Program
{
internal enum ExitCodes
{
ArgsError = -1,
OK = 0,
Help = 1
}

private static Arguments? _args;

static void Main(string[] args)
{
// forcing UTF-8 encoding
System.Console.OutputEncoding = Encoding.UTF8;

try
{
_args = PowArgs.Parser<Arguments>.Parse(args);
}
catch (Exception ex)
{
System.Console.Error.WriteLine(ex.Message);
ShowHelp();
Exit(ExitCodes.ArgsError);
return;
}

if (_args.Help)
{
ShowHelp();
Exit(ExitCodes.Help);
return;
}

var serverIpAddress = IPAddress.Parse(_args.ServerIpAddress!);

using var cancellationTokenSource = new CancellationTokenSource();
using var listener = new ForzaDataListener(_args.Port, serverIpAddress);

// cancellation provided by CTRL + C / CTRL + break
System.Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true;
cancellationTokenSource.Cancel();
};

// forza data observer
var console = new ForzaDataConsole();
console.Subscribe(listener);

try
{
System.Console.WriteLine($"Listening for data from {serverIpAddress} to local port {_args.Port}...");
System.Console.WriteLine($"Please press CTRL+C to stop listening");

// forza data observable
listener.Listen(cancellationTokenSource.Token);
}
catch (OperationCanceledException)
{
// user cancellation requested
}

console.Unsubscribe();

Exit(ExitCodes.OK);
}

private static void ShowHelp()
{
foreach (string helpTextLine in PowArgs.Helper<Arguments>.GetHelpText())
{
System.Console.Out.WriteLine(helpTextLine);
}
}

private static void Exit(ExitCodes exitCode)
{
Environment.Exit((int)exitCode);
}
}
[DynamicDependency(
DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicNestedTypes,
typeof(DefaultCommand)
)]
static int Main(string[] args) => new CommandApp<DefaultCommand>().Run(args);
}
2 changes: 1 addition & 1 deletion Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"ForzaData.Console": {
"commandName": "Project",
"commandLineArgs": "-ServerIpAddress 192.168.0.10 -Port 7777"
"commandLineArgs": "--server 192.168.0.100 --port 7777"
}
}
}
Loading

0 comments on commit 79338a2

Please sign in to comment.