Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce boilerplate needed for IValueParser implementations #169

Merged
merged 11 commits into from
Oct 26, 2018
32 changes: 16 additions & 16 deletions src/CommandLineUtils/Abstractions/ValueParserProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ internal ValueParserProvider()
AddRange(
new IValueParser[]
{
StringValueParser.Singleton,
BooleanValueParser.Singleton,
ByteValueParser.Singleton,
Int16ValueParser.Singleton,
Int32ValueParser.Singleton,
Int64ValueParser.Singleton,
UInt16ValueParser.Singleton,
UInt32ValueParser.Singleton,
UInt64ValueParser.Singleton,
FloatValueParser.Singleton,
DoubleValueParser.Singleton,
UriValueParser.Singleton,
StockValueParsers.String,
StockValueParsers.Boolean,
StockValueParsers.Byte,
StockValueParsers.Int16,
StockValueParsers.Int32,
StockValueParsers.Int64,
StockValueParsers.UInt16,
StockValueParsers.UInt32,
StockValueParsers.UInt64,
StockValueParsers.Float,
StockValueParsers.Double,
StockValueParsers.Uri,
});
}

Expand Down Expand Up @@ -96,14 +96,14 @@ internal IValueParser GetParserImpl<T>()

if (typeInfo.IsEnum)
{
return new EnumParser(type);
return EnumParser.Create(type);
}

if (ReflectionHelper.IsNullableType(typeInfo, out var wrappedType))
{
if (wrappedType.GetTypeInfo().IsEnum)
{
return new NullableValueParser(new EnumParser(wrappedType));
return new NullableValueParser(EnumParser.Create(wrappedType));
}

if (_parsers.TryGetValue(wrappedType, out parser))
Expand All @@ -125,8 +125,8 @@ internal IValueParser GetParserImpl<T>()
{
return null;
}
var parserType = typeof(ValueTupleValueParser<>).MakeGenericType(typeInfo.GenericTypeArguments[1]);
return (IValueParser)Activator.CreateInstance(parserType, new object[] { innerParser });
var method = typeof(ValueTupleValueParser).GetTypeInfo().GetMethod(nameof(ValueTupleValueParser.Create)).MakeGenericMethod(typeInfo.GenericTypeArguments[1]);
return (IValueParser)method.Invoke(null, new object[] { innerParser });
}

return null;
Expand Down
42 changes: 0 additions & 42 deletions src/CommandLineUtils/Internal/ValueParsers/BooleanValueParser.cs

This file was deleted.

33 changes: 0 additions & 33 deletions src/CommandLineUtils/Internal/ValueParsers/ByteValueParser.cs

This file was deleted.

33 changes: 0 additions & 33 deletions src/CommandLineUtils/Internal/ValueParsers/DoubleValueParser.cs

This file was deleted.

35 changes: 15 additions & 20 deletions src/CommandLineUtils/Internal/ValueParsers/EnumParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@

namespace McMaster.Extensions.CommandLineUtils.Abstractions
{
internal class EnumParser : IValueParser
internal static class EnumParser
{
public EnumParser(Type enumType)
{
TargetType = enumType;
}

public Type TargetType { get; }

public object Parse(string argName, string value, CultureInfo culture)
{
if (value == null) return Enum.ToObject(TargetType, 0);

try
public static IValueParser Create(Type enumType) =>
ValueParser.Create(enumType, (argName, value, culture) =>
{
return Enum.Parse(TargetType, value, ignoreCase: true);
}
catch
{
throw new FormatException($"Invalid value specified for {argName}. Allowed values are: {string.Join(", ", Enum.GetNames(TargetType))}.");
}
}
if (value == null) return Enum.ToObject(enumType, 0);

try
{
return Enum.Parse(enumType, value, ignoreCase: true);
}
catch
{
throw new FormatException(
$"Invalid value specified for {argName}. Allowed values are: {string.Join(", ", Enum.GetNames(enumType))}.");
}
});
}
}
33 changes: 0 additions & 33 deletions src/CommandLineUtils/Internal/ValueParsers/FloatValueParser.cs

This file was deleted.

33 changes: 0 additions & 33 deletions src/CommandLineUtils/Internal/ValueParsers/Int16ValueParser.cs

This file was deleted.

33 changes: 0 additions & 33 deletions src/CommandLineUtils/Internal/ValueParsers/Int32ValueParser.cs

This file was deleted.

33 changes: 0 additions & 33 deletions src/CommandLineUtils/Internal/ValueParsers/Int64ValueParser.cs

This file was deleted.

Loading