using System; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace PCL.Core.App.Cli; public class DecimalArgument : CommandArgument { public override ArgumentValueKind ValueKind => ArgumentValueKind.Decimal; protected override decimal ParseValueText() => decimal.Parse(ValueText); public new decimal Value { get => base.Value; init => base.Value = value; } public override bool TryCastValue([NotNullWhen(true)] out T value) { if (base.TryCastValue(out value)) return true; var type = typeof(T); try { if (type == typeof(int)) Unsafe.As(ref value) = Convert.ToInt32(Value); else if (type == typeof(long)) Unsafe.As(ref value) = Convert.ToInt64(Value); else if (type == typeof(double)) Unsafe.As(ref value) = Convert.ToDouble(Value); else if (type == typeof(float)) Unsafe.As(ref value) = Convert.ToSingle(Value); else if (type == typeof(short)) Unsafe.As(ref value) = Convert.ToInt16(Value); else if (type == typeof(sbyte)) Unsafe.As(ref value) = Convert.ToSByte(Value); else if (type == typeof(ulong)) Unsafe.As(ref value) = Convert.ToUInt64(Value); else if (type == typeof(uint)) Unsafe.As(ref value) = Convert.ToUInt32(Value); else if (type == typeof(ushort)) Unsafe.As(ref value) = Convert.ToUInt16(Value); else if (type == typeof(byte)) Unsafe.As(ref value) = Convert.ToByte(Value); else if (type == typeof(nint)) Unsafe.As(ref value) = checked((nint)Convert.ToInt64(Value)); else if (type == typeof(nuint)) Unsafe.As(ref value) = checked((nuint)Convert.ToUInt64(Value)); else return false; #pragma warning disable CS8762 // The analyzer sucks. return true; #pragma warning restore CS8762 } catch (Exception ex) when (ex is OverflowException or InvalidCastException or FormatException) { return false; } } }