Skip to content

Commit e03ef8e

Browse files
committed
Remove dependency on imported ExactConvert. The amount of code copied over is actually quite small
1 parent ba70431 commit e03ef8e

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ jobs:
3232
core.exportVariable('VER_NUM', vs.formatVersion(`${majmin}.$(GitRevCount)`));
3333
core.exportVariable('VER_SUF', ver_suf);
3434
35-
- name: Clone RT.Util
36-
run: git clone https://github.com/RT-Projects/RT.Util.git ../RT.Util
37-
3835
- name: Install dotnet
3936
uses: actions/setup-dotnet@v4
4037
with:

RT.CommandLine.sln

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RT.CommandLine", "Src\RT.Co
66
EndProject
77
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RT.CommandLine.Tests", "Tests\RT.CommandLine.Tests.csproj", "{294D2B02-3FA6-4B9A-82BF-F80396943892}"
88
EndProject
9-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RT.Util.Core", "..\RT.Util\RT.Util.Core\RT.Util.Core.csproj", "{45112116-A549-97E2-5AB1-8CDC7AE4B250}"
10-
EndProject
119
Global
1210
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1311
Debug-locallibs|Any CPU = Debug-locallibs|Any CPU
@@ -27,12 +25,6 @@ Global
2725
{294D2B02-3FA6-4B9A-82BF-F80396943892}.Debug-Nuget|Any CPU.Build.0 = Debug|Any CPU
2826
{294D2B02-3FA6-4B9A-82BF-F80396943892}.Release|Any CPU.ActiveCfg = Release|Any CPU
2927
{294D2B02-3FA6-4B9A-82BF-F80396943892}.Release|Any CPU.Build.0 = Release|Any CPU
30-
{45112116-A549-97E2-5AB1-8CDC7AE4B250}.Debug-locallibs|Any CPU.ActiveCfg = Debug|Any CPU
31-
{45112116-A549-97E2-5AB1-8CDC7AE4B250}.Debug-locallibs|Any CPU.Build.0 = Debug|Any CPU
32-
{45112116-A549-97E2-5AB1-8CDC7AE4B250}.Debug-Nuget|Any CPU.ActiveCfg = Debug|Any CPU
33-
{45112116-A549-97E2-5AB1-8CDC7AE4B250}.Debug-Nuget|Any CPU.Build.0 = Debug|Any CPU
34-
{45112116-A549-97E2-5AB1-8CDC7AE4B250}.Release|Any CPU.ActiveCfg = Release|Any CPU
35-
{45112116-A549-97E2-5AB1-8CDC7AE4B250}.Release|Any CPU.Build.0 = Release|Any CPU
3628
EndGlobalSection
3729
GlobalSection(SolutionProperties) = preSolution
3830
HideSolutionNode = FALSE

Src/CommandLineParser.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics;
1+
using System.Diagnostics;
2+
using System.Numerics;
23
using System.Reflection;
34
using RT.Internal;
45
using RT.PostBuild;
@@ -203,6 +204,10 @@ public static ConsoleColoredString GenerateHelp<TArgs>(int? wrapWidth = null, Ty
203204
return getHelpGenerator(subType ?? typeof(TArgs), helpProcessor)(wrapWidth ?? ConsoleUtil.WrapToWidth());
204205
}
205206

207+
private static bool isIntegerType(Type type) => Type.GetTypeCode(type) is TypeCode.Byte or TypeCode.SByte or TypeCode.Int16 or TypeCode.Int32 or TypeCode.Int64 or TypeCode.UInt16 or TypeCode.UInt32 or TypeCode.UInt64 or TypeCode.Boolean or TypeCode.Char or TypeCode.DateTime;
208+
private static bool isTrueIntegerType(Type type) => Type.GetTypeCode(type) is TypeCode.Byte or TypeCode.SByte or TypeCode.Int16 or TypeCode.Int32 or TypeCode.Int64 or TypeCode.UInt16 or TypeCode.UInt32 or TypeCode.UInt64;
209+
private static bool isTrueIntegerNullableType(Type type) => type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) && isTrueIntegerType(type.GetGenericArguments()[0]);
210+
206211
private static object parseCommandLine(string[] args, Type type, int i, Func<ConsoleColoredString, ConsoleColoredString> helpProcessor)
207212
{
208213
if (i < args.Length)
@@ -346,7 +351,7 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
346351
options[o] = () => { field.SetValue(ret, true); i++; missingMandatories.Remove(field); };
347352
}
348353
// ### STRING and INTEGER fields (including nullable)
349-
else if (field.FieldType == typeof(string) || ExactConvert.IsTrueIntegerType(field.FieldType) || ExactConvert.IsTrueIntegerNullableType(field.FieldType) ||
354+
else if (field.FieldType == typeof(string) || isTrueIntegerType(field.FieldType) || isTrueIntegerNullableType(field.FieldType) ||
350355
field.FieldType == typeof(float) || field.FieldType == typeof(float?) || field.FieldType == typeof(double) || field.FieldType == typeof(double?))
351356
{
352357
if (positional is double order)
@@ -526,6 +531,41 @@ private static object parseCommandLine(string[] args, Type type, int i, Func<Con
526531

527532
private static IEnumerable<Type> allTypes() => AppDomain.CurrentDomain.GetAssemblies().SelectMany(asm => asm.GetTypes());
528533

534+
private static bool tryConvert(Type toType, string value, out object result)
535+
{
536+
if (toType.IsEnum)
537+
{
538+
object[] parameters = [value, null];
539+
var succeeded = (bool) typeof(Enum).GetMethods(BindingFlags.Static | BindingFlags.Public).First(m => m.Name == "TryParse" && m.GetParameters().Length == 2).MakeGenericMethod(toType).Invoke(null, parameters);
540+
result = parameters[1];
541+
return succeeded;
542+
}
543+
544+
bool success = false;
545+
object converted = null;
546+
switch (Type.GetTypeCode(toType))
547+
{
548+
case TypeCode.Boolean: { success = bool.TryParse(value, out bool temp); converted = temp; break; }
549+
case TypeCode.Byte: { success = byte.TryParse(value, out byte temp); converted = temp; break; }
550+
case TypeCode.SByte: { success = sbyte.TryParse(value, out sbyte temp); converted = temp; break; }
551+
case TypeCode.Int16: { success = short.TryParse(value, out short temp); converted = temp; break; }
552+
case TypeCode.UInt16: { success = ushort.TryParse(value, out ushort temp); converted = temp; break; }
553+
case TypeCode.Int32: { success = int.TryParse(value, out int temp); converted = temp; break; }
554+
case TypeCode.UInt32: { success = uint.TryParse(value, out uint temp); converted = temp; break; }
555+
case TypeCode.Int64: { success = long.TryParse(value, out long temp); converted = temp; break; }
556+
case TypeCode.UInt64: { success = ulong.TryParse(value, out ulong temp); converted = temp; break; }
557+
case TypeCode.Single: { success = float.TryParse(value, out float temp); converted = temp; break; }
558+
case TypeCode.Double: { success = double.TryParse(value, out double temp); converted = temp; break; }
559+
case TypeCode.Decimal: { success = decimal.TryParse(value, out decimal temp); converted = temp; break; }
560+
case TypeCode.DateTime: { success = DateTime.TryParse(value, out DateTime temp); converted = temp; break; }
561+
case TypeCode.Char: { success = char.TryParse(value, out char temp); converted = temp; break; }
562+
case TypeCode.String: { converted = value; success = true; break; }
563+
case TypeCode.Object when toType == typeof(BigInteger): { success = BigInteger.TryParse(value, out BigInteger temp); converted = temp; break; }
564+
}
565+
result = success ? converted : null;
566+
return success;
567+
}
568+
529569
private static bool convertStringAndSetField(string value, object cmdLineObject, FieldInfo field)
530570
{
531571
object result;
@@ -534,10 +574,10 @@ private static bool convertStringAndSetField(string value, object cmdLineObject,
534574
result = value;
535575
else
536576
{
537-
Type type = field.FieldType.IsGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(Nullable<>)
577+
Type type = field.FieldType.IsConstructedGenericType && field.FieldType.GetGenericTypeDefinition() == typeof(Nullable<>)
538578
? field.FieldType.GetGenericArguments()[0]
539579
: field.FieldType;
540-
if (!ExactConvert.Try(type, value, out result))
580+
if (!tryConvert(type, value, out result))
541581
return false;
542582
}
543583
field.SetValue(cmdLineObject, result);
@@ -908,8 +948,8 @@ private static void postBuildStep(IPostBuildReporter rep, Type commandLineType,
908948
}
909949
// ### STRING, STRING[], INTEGER and FLOATING fields (including nullable)
910950
else if (field.FieldType == typeof(string) || field.FieldType == typeof(string[]) ||
911-
(ExactConvert.IsTrueIntegerType(field.FieldType) && !field.FieldType.IsEnum) ||
912-
(ExactConvert.IsTrueIntegerNullableType(field.FieldType) && !field.FieldType.GetGenericArguments()[0].IsEnum) ||
951+
(isTrueIntegerType(field.FieldType) && !field.FieldType.IsEnum) ||
952+
(isTrueIntegerNullableType(field.FieldType) && !field.FieldType.GetGenericArguments()[0].IsEnum) ||
913953
field.FieldType == typeof(float) || field.FieldType == typeof(float?) || field.FieldType == typeof(double) || field.FieldType == typeof(double?))
914954
{
915955
// options is null if and only if this field is positional

Src/RT.CommandLine.csproj

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,4 @@
2525
<ItemGroup Condition="'$(Configuration)' != 'Debug-locallibs'">
2626
<PackageReference Include="RT.Util.Core" Version="2.0.1777" />
2727
</ItemGroup>
28-
29-
<ItemGroup>
30-
<Compile Include="..\External\RT.Util\RT.Serialization\ExactConvert.cs" Link="RT.Util\ExactConvert.cs" />
31-
<Compile Include="..\External\RT.Util\RT.Util.Core\ExtensionMethods\DateTimeExtensions.cs" Link="RT.Util\DateTimeExtensions.cs" />
32-
</ItemGroup>
3328
</Project>

0 commit comments

Comments
 (0)