-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switching PowArgs to Spectre.Console.Cli and fixing console apps trim…
…ming
- Loading branch information
Showing
31 changed files
with
505 additions
and
10,837 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.