Added .NET Framework 4 target for application

Update ErrorCodes.cs, NibblePoker.Application.ListComPort.csproj, and 2 more files...
This commit is contained in:
2024-05-24 16:49:02 +02:00
parent 6e4462c8fa
commit 60a7cb0f6b
4 changed files with 256 additions and 214 deletions
@@ -1,6 +1,5 @@
namespace NibblePoker.Application.ListComPort; namespace NibblePoker.Application.ListComPort {
public static class ErrorCodes {
public static class ErrorCodes {
public const int NoError = 0; public const int NoError = 0;
/* Fatal errors (1-9) */ /* Fatal errors (1-9) */
@@ -17,4 +16,5 @@ public static class ErrorCodes {
/* Application & System errors (30-39) */ /* Application & System errors (30-39) */
// NoFriendlyNames = 30 => Causes problems with broad-range exit code checks. // NoFriendlyNames = 30 => Causes problems with broad-range exit code checks.
public const int NoComPorts = 31; public const int NoComPorts = 31;
}
} }
@@ -2,9 +2,11 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFrameworks>net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks> <TargetFrameworks>net40;net6.0-windows;net7.0-windows;net8.0-windows</TargetFrameworks>
<ImplicitUsings>disable</ImplicitUsings> <ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>disable</Nullable>
<RootNamespace>NibblePoker.Application.ListComPort</RootNamespace> <RootNamespace>NibblePoker.Application.ListComPort</RootNamespace>
<AssemblyName>NibblePoker.Application.ListComPort</AssemblyName> <AssemblyName>NibblePoker.Application.ListComPort</AssemblyName>
<AssemblyVersion>4.0.0</AssemblyVersion> <AssemblyVersion>4.0.0</AssemblyVersion>
@@ -14,8 +16,6 @@
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
<Authors>Herwin Bozet (@NibblePoker)</Authors> <Authors>Herwin Bozet (@NibblePoker)</Authors>
<ApplicationIcon>..\NibblePoker.Packaging.ListComPort\Icons\lscom-v2-text-01.ico</ApplicationIcon> <ApplicationIcon>..\NibblePoker.Packaging.ListComPort\Icons\lscom-v2-text-01.ico</ApplicationIcon>
<Configurations>Release</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+80 -36
View File
@@ -4,30 +4,48 @@ using System.Linq;
using NibblePoker.Library.Arguments; using NibblePoker.Library.Arguments;
using NibblePoker.Library.ComPortWatcher; using NibblePoker.Library.ComPortWatcher;
namespace NibblePoker.Application.ListComPort; namespace NibblePoker.Application.ListComPort {
internal static class Program {
internal static class Program {
private const string Version = "4.0.0"; private const string Version = "4.0.0";
private static readonly Verb RootVerb = new Verb(""); // ReSharper disable once InconsistentNaming
private static readonly Verb RootVerb = new Verb(null);
private static readonly Option OptionShowAll = new('a', "show-all", "Display the complete port's name (Equal to '-dfn')"); private static readonly Option OptionShowAll = new Option('a', "show-all",
private static readonly Option OptionShowDevice = new('d', "show-device", "Displays the port's device name"); "Display the complete port's name (Equal to '-dfn')");
private static readonly Option OptionDivider = new('D', "divider", "Uses the given string or char as a separator (Can be an empty string !)", OptionFlags.HasValue); private static readonly Option OptionShowDevice = new Option('d', "show-device",
private static readonly Option OptionShowFriendly = new('f', "show-friendly", "Displays the port's friendly name"); "Displays the port's device name");
private static readonly Option OptionHelp = new('h', "help", "Display the help text", OptionFlags.StopsParsing); private static readonly Option OptionShowFriendly = new Option('f', "show-friendly",
private static readonly Option OptionHelpShort = new('H', "short-help", "Display the short help text", OptionFlags.StopsParsing); "Displays the port's friendly name");
private static readonly Option OptionShowNameRaw = new('n', "show-name-raw", "Displays the port's raw name (See remarks section)"); private static readonly Option OptionShowNameRaw = new Option('n', "show-name-raw",
private static readonly Option OptionNoPretty = new('P', "no-pretty", "Disables the pretty printing format (Equal to -D \" \""); "Displays the port's raw name (See remarks section)");
private static readonly Option OptionSort = new('s', "sort", "Sorts the port based on their raw names in an ascending order");
private static readonly Option OptionSortReverse = new('S', "sort-reverse", "Sorts the port based on their raw names in a descending order");
private static readonly Option OptionTabPadding = new('t', "tab-padding", "Use tabs for padding between the types of names (Overrides '-D')");
private static readonly Option OptionVersion = new('v', "version", "Shows the utility's version number and other info");
private static readonly Option OptionVersionOnly = new('V', "version-only", "Shows the utility's version number only (Overrides '-v')");
private static readonly Option OptionDivider = new Option('D', "divider",
"Uses the given string or char as a separator (Can be an empty string !)", OptionFlags.HasValue);
private static readonly Option OptionTabPadding = new Option('t', "tab-padding",
"Use tabs for padding between the types of names (Overrides '-D')");
private static readonly Option OptionNoPretty = new Option('P', "no-pretty",
"Disables the pretty printing format (Equal to -D \" \"");
private static readonly Option OptionSort = new Option('s', "sort",
"Sorts the port based on their raw names in an ascending order");
private static readonly Option OptionSortReverse = new Option('S', "sort-reverse",
"Sorts the port based on their raw names in a descending order");
private static readonly Option OptionHelp = new Option('h', "help",
"Display the help text", OptionFlags.StopsParsing);
private static readonly Option OptionHelpShort = new Option('H', "short-help",
"Display the short help text", OptionFlags.StopsParsing);
private static readonly Option OptionVersion = new Option('v', "version",
"Shows the utility's version number and other info");
private static readonly Option OptionVersionOnly = new Option('V', "version-only",
"Shows the utility's version number only (Overrides '-v')");
// ReSharper disable RedundantDefaultMemberInitializer
private static bool _shouldPrintRawNames = true; private static bool _shouldPrintRawNames = true;
private static bool _shouldPrintDeviceNames = false; private static bool _shouldPrintDeviceNames = false;
private static bool _shouldPrintFriendlyNames = false; private static bool _shouldPrintFriendlyNames = false;
// ReSharper enable RedundantDefaultMemberInitializer
private static string _paddingText = ""; private static string _paddingText = "";
@@ -45,6 +63,7 @@ internal static class Program {
} }
private static int Main(string[] args) { private static int Main(string[] args) {
// Preparing & parsing launch arguments
try { try {
ArgumentsParser.ParseArguments(RootVerb.RegisterOption(OptionShowAll).RegisterOption(OptionShowDevice) ArgumentsParser.ParseArguments(RootVerb.RegisterOption(OptionShowAll).RegisterOption(OptionShowDevice)
.RegisterOption(OptionDivider).RegisterOption(OptionShowFriendly).RegisterOption(OptionHelp) .RegisterOption(OptionDivider).RegisterOption(OptionShowFriendly).RegisterOption(OptionHelp)
@@ -57,20 +76,31 @@ internal static class Program {
RootVerb.Clear(); RootVerb.Clear();
} }
// Processing exiting options
if(OptionHelp.WasUsed() || OptionHelpShort.WasUsed()) { if(OptionHelp.WasUsed() || OptionHelpShort.WasUsed()) {
Console.WriteLine(HelpText.GetFullHelpText(RootVerb, "lscom.exe", (uint)Console.BufferWidth - 1, 1, 2, false)); Console.WriteLine(HelpText.GetFullHelpText(
RootVerb, "lscom.exe",
(uint)Console.BufferWidth - 1, 1, 2, false));
if(OptionHelp.WasUsed()) { if(OptionHelp.WasUsed()) {
Console.WriteLine("\nRemarks:"); Console.WriteLine("\nRemarks:");
Console.WriteLine(" * If '-d' or '-f' is used, the raw name will not be shown unless '-n' is used."); Console.WriteLine(
Console.WriteLine(" * If '-D', '-t' or '-p' are used, the special separator between the raw and friendly name and the square brackets are not shown."); " * If '-d' or '-f' is used, the raw name will not be shown unless '-n' is used.");
Console.WriteLine(" * If the program is raw without going through a console, the options '-n' and '-f' will be used."); Console.WriteLine(
Console.WriteLine(" * By default, the ports are sorted in the order they are provided by the registry, which is often chronological."); " * If '-D', '-t' or '-p' are used, the special separator between the raw and friendly name and the square brackets are not shown.");
Console.WriteLine(
" * If the program is raw without going through a console, the options '-n' and '-f' will be used.");
Console.WriteLine(
" * By default, the ports are sorted in the order they are provided by the registry, which is often chronological.");
Console.WriteLine(" * The 'raw name' refers to a port name. (e.g.: COM1, COM2, ...)"); Console.WriteLine(" * The 'raw name' refers to a port name. (e.g.: COM1, COM2, ...)");
Console.WriteLine(" * The 'device name' refers to a port device path. (e.g.: \\Device\\Serial1, ...)"); Console.WriteLine(
Console.WriteLine(" * The 'friendly name' refers to a port name as seen in the device manager. (e.g.: Communications Port, USB-SERIAL CH340, ...)"); " * The 'device name' refers to a port device path. (e.g.: \\Device\\Serial1, ...)");
Console.WriteLine(" * Any result returned with an error code between 1-9 and 30-39 should be considered as invalid."); Console.WriteLine(
Console.WriteLine(" * Any result returned with another error code is valid but probably not formatted properly."); " * The 'friendly name' refers to a port name as seen in the device manager. (e.g.: Communications Port, USB-SERIAL CH340, ...)");
Console.WriteLine(
" * Any result returned with an error code between 1-9 and 30-39 should be considered as invalid.");
Console.WriteLine(
" * Any result returned with another error code is valid but probably not formatted properly.");
} }
return ErrorCodes.NoError; return ErrorCodes.NoError;
@@ -87,17 +117,19 @@ internal static class Program {
Console.WriteLine("Dependencies:"); Console.WriteLine("Dependencies:");
Console.WriteLine("├─> NibblePoker.Library.Arguments v2.0.0"); Console.WriteLine("├─> NibblePoker.Library.Arguments v2.0.0");
Console.WriteLine("├─> NibblePoker.Library.ComPortHelpers v" + Version); Console.WriteLine("├─> NibblePoker.Library.ComPortHelpers v" + Version);
Console.WriteLine("└─> NibblePoker.Library.RegistryHelpers v0.0.1"); Console.WriteLine("└─> NibblePoker.Library.RegistryHelpers v1.0.0");
Console.WriteLine(""); Console.WriteLine("");
Console.WriteLine("GitHub repositories:"); Console.WriteLine("GitHub repositories:");
Console.WriteLine("├─> https://github.com/aziascreations/DotNet-Arguments"); Console.WriteLine("├─> https://github.com/aziascreations/DotNet-Arguments");
Console.WriteLine("├─> https://github.com/aziascreations/DotNet-ListComPort"); Console.WriteLine("├─> https://github.com/aziascreations/DotNet-ListComPort");
Console.WriteLine("└─> https://github.com/aziascreations/DotNet-RegistryHelpers"); Console.WriteLine("└─> https://github.com/aziascreations/DotNet-RegistryHelpers");
Console.WriteLine(""); Console.WriteLine("");
Console.WriteLine("This software and all its libraries are released in the public domain under the CC0-1.0 license."); Console.WriteLine(
"This software and all its libraries are released in the public domain under the CC0-1.0 license.");
return ErrorCodes.NoError; return ErrorCodes.NoError;
} }
// Processing parameter options
if(OptionShowDevice.WasUsed()) { if(OptionShowDevice.WasUsed()) {
_shouldPrintRawNames = false; _shouldPrintRawNames = false;
_shouldPrintDeviceNames = true; _shouldPrintDeviceNames = true;
@@ -139,7 +171,6 @@ internal static class Program {
} }
// Application's code // Application's code
string rawToFriendlySeparator = " - "; string rawToFriendlySeparator = " - ";
bool addDeviceBrackets = true; bool addDeviceBrackets = true;
@@ -157,11 +188,22 @@ internal static class Program {
List<ComPortInfo> comPortsInfo = ComPortHelper.GetComList(_shouldPrintFriendlyNames); List<ComPortInfo> comPortsInfo = ComPortHelper.GetComList(_shouldPrintFriendlyNames);
if(comPortsInfo.Count > 0) { if(comPortsInfo.Count > 0) {
comPortsInfo = _sortingOrder switch { //comPortsInfo = _sortingOrder switch {
SortingOrder.Ascending => comPortsInfo.OrderBy(o => o.RawName).ToList(), // SortingOrder.Ascending => comPortsInfo.OrderBy(o => o.RawName).ToList(),
SortingOrder.Descending => comPortsInfo.OrderByDescending(o => o.RawName).ToList(), // SortingOrder.Descending => comPortsInfo.OrderByDescending(o => o.RawName).ToList(),
_ => comPortsInfo // _ => comPortsInfo
}; //};
switch(_sortingOrder) {
case SortingOrder.Ascending:
comPortsInfo = comPortsInfo.OrderBy(o => o.RawName).ToList();
break;
case SortingOrder.Descending:
comPortsInfo = comPortsInfo.OrderByDescending(o => o.RawName).ToList();
break;
case SortingOrder.None:
default:
break;
}
foreach(ComPortInfo comPortInfo in comPortsInfo) { foreach(ComPortInfo comPortInfo in comPortsInfo) {
if(_shouldPrintRawNames) { if(_shouldPrintRawNames) {
@@ -181,7 +223,8 @@ internal static class Program {
} }
if(_shouldPrintDeviceNames) { if(_shouldPrintDeviceNames) {
Console.Write((addDeviceBrackets ? "[" : "") + comPortInfo.DeviceName + (addDeviceBrackets ? "]" : "")); Console.Write((addDeviceBrackets ? "[" : "") + comPortInfo.DeviceName +
(addDeviceBrackets ? "]" : ""));
} }
Console.WriteLine(); Console.WriteLine();
@@ -200,8 +243,9 @@ internal static class Program {
return _exitCode; return _exitCode;
} }
// Imports "GetConsoleProcessList(...)" in order to know if the program was run via a shortcut or CLI. // Imports "GetConsoleProcessList(...)" in order to know if the program was run via a shortcut or a CLI prompt.
// See: https://learn.microsoft.com/en-us/windows/console/getconsoleprocesslist // See: https://learn.microsoft.com/en-us/windows/console/getconsoleprocesslist
[System.Runtime.InteropServices.DllImport("kernel32.dll")] [System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern int GetConsoleProcessList(int[] buffer, int size); private static extern int GetConsoleProcessList(int[] buffer, int size);
}
} }
@@ -8,8 +8,6 @@
<RootNamespace>NibblePoker.Library.ComPortWatcher</RootNamespace> <RootNamespace>NibblePoker.Library.ComPortWatcher</RootNamespace>
<AssemblyName>NibblePoker.Library.ComPortHelpers</AssemblyName> <AssemblyName>NibblePoker.Library.ComPortHelpers</AssemblyName>
<Configurations>Release</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>