Skip to content

Commit 53e19fc

Browse files
committed
cleanup: simplify implementation of TypeDescriptor support and make code formatting consistent
1 parent dc17772 commit 53e19fc

File tree

10 files changed

+74
-139
lines changed

10 files changed

+74
-139
lines changed

src/CommandLineUtils/Abstractions/DefaultValueParserFactory.cs

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/CommandLineUtils/Abstractions/ValueParserProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace McMaster.Extensions.CommandLineUtils.Abstractions
1515
public class ValueParserProvider
1616
{
1717
private readonly Dictionary<Type, IValueParser> _parsers = new Dictionary<Type, IValueParser>(10);
18-
private readonly DefaultValueParserFactory _defaultValueParserFactory = new DefaultValueParserFactory();
18+
private readonly TypeDescriptorValueParserFactory _defaultValueParserFactory = new TypeDescriptorValueParserFactory();
1919

2020
internal ValueParserProvider()
2121
{
@@ -102,7 +102,9 @@ public IValueParser GetParser(Type type)
102102
}
103103

104104
if (_defaultValueParserFactory.TryGetParser<T>(out parser))
105+
{
105106
return parser;
107+
}
106108

107109
if (ReflectionHelper.IsNullableType(type, out var wrappedType) && wrappedType != null)
108110
{

src/CommandLineUtils/Internal/ValueParsers/ArrayParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Globalization;
67

78
namespace McMaster.Extensions.CommandLineUtils.Abstractions
89
{
9-
using System.Globalization;
10-
1110
internal class ArrayParser : ICollectionParser
1211
{
1312
private readonly Type _elementType;

src/CommandLineUtils/Internal/ValueParsers/EnumParser.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Globalization;
65

76
namespace McMaster.Extensions.CommandLineUtils.Abstractions
87
{

src/CommandLineUtils/Internal/ValueParsers/HashSetParser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Globalization;
67
using System.Reflection;
78

89
namespace McMaster.Extensions.CommandLineUtils.Abstractions
910
{
10-
using System.Globalization;
11-
1211
internal class HashSetParser : ICollectionParser
1312
{
1413
private readonly IValueParser _elementParser;

src/CommandLineUtils/Internal/ValueParsers/NullableValueParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) Nate McMaster.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Globalization;
6+
47
namespace McMaster.Extensions.CommandLineUtils.Abstractions
58
{
6-
using System;
7-
using System.Globalization;
8-
99
internal class NullableValueParser : IValueParser
1010
{
1111
private readonly IValueParser _wrapped;

src/CommandLineUtils/Internal/ValueParsers/StockValueParsers.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) Nate McMaster.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Globalization;
6+
47
namespace McMaster.Extensions.CommandLineUtils.Abstractions
58
{
6-
using System;
7-
using System.Globalization;
8-
99
internal static class StockValueParsers
1010
{
1111
public static readonly IValueParser<bool> Boolean = ValueParser.Create(
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Nate McMaster.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.ComponentModel;
6+
using System.Globalization;
7+
8+
namespace McMaster.Extensions.CommandLineUtils.Abstractions
9+
{
10+
/// <summary>
11+
/// A factory creating generic implementations of <see cref="IValueParser{T}"/>. The implementations are based
12+
/// on automatically located <see cref="TypeConverter"/> classes that are suitable for parsing.
13+
/// </summary>
14+
internal class TypeDescriptorValueParserFactory
15+
{
16+
public bool TryGetParser<T>(out IValueParser parser)
17+
{
18+
var targetType = typeof(T);
19+
var converter = TypeDescriptor.GetConverter(targetType);
20+
if (converter.CanConvertFrom(typeof(string)))
21+
{
22+
parser = new TypeConverterValueParser<T>(targetType, converter);
23+
return true;
24+
}
25+
26+
parser = null;
27+
return false;
28+
}
29+
30+
private sealed class TypeConverterValueParser<T> : IValueParser<T>
31+
{
32+
public TypeConverterValueParser(Type targetType, TypeConverter typeConverter)
33+
{
34+
TargetType = targetType ?? throw new ArgumentNullException(nameof(targetType));
35+
TypeConverter = typeConverter ?? throw new ArgumentNullException(nameof(typeConverter));
36+
}
37+
38+
public Type TargetType { get; }
39+
40+
private TypeConverter TypeConverter { get; }
41+
42+
public T Parse(string argName, string value, CultureInfo culture)
43+
{
44+
try
45+
{
46+
culture ??= CultureInfo.InvariantCulture;
47+
return (T)TypeConverter.ConvertFromString(null, culture, value);
48+
}
49+
catch (ArgumentException e)
50+
{
51+
throw new FormatException(e.Message, e);
52+
}
53+
}
54+
55+
object IValueParser.Parse(string argName, string value, CultureInfo culture)
56+
=> Parse(argName, value, culture);
57+
}
58+
}
59+
}

src/CommandLineUtils/Internal/ValueParsers/ValueParser.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (c) Nate McMaster.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
using System.Globalization;
6+
47
namespace McMaster.Extensions.CommandLineUtils.Abstractions
58
{
6-
using System;
7-
using System.Globalization;
8-
99
/// <summary>
1010
/// Provides methods for creating <see cref="IValueParser{T}"/>
1111
/// boilerplate implementations.

test/CommandLineUtils.Tests/ValueParserProviderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ public void ParsesGuid(string arg)
508508
[InlineData("")]
509509
public void ParsesGuidNullable(string arg)
510510
{
511-
var expected = String.IsNullOrWhiteSpace(arg)
511+
var expected = string.IsNullOrWhiteSpace(arg)
512512
? (Guid?)null
513513
: Guid.Parse("ff23ef12-500a-48df-9a5d-151c2adc2a0a");
514514
var parsed = CommandLineParser.ParseArgs<Program>("--guid-opt", arg);

0 commit comments

Comments
 (0)