Skip to content

Commit 4d69a38

Browse files
committed
Merge PR 314 (Wind010)
1 parent 92869e2 commit 4d69a38

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/CommandLine/Core/ReflectionExtensions.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,18 @@ private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specPro
100100
{
101101
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e.InnerException, value) };
102102
}
103+
catch (ArgumentException e)
104+
{
105+
var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e);
106+
107+
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), argEx, value) };
108+
}
109+
103110
catch (Exception e)
104111
{
105-
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) };
112+
return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) };
106113
}
114+
107115
}
108116

109117
public static object CreateEmptyArray(this Type type)
@@ -202,4 +210,4 @@ public static bool IsPrimitiveEx(this Type type)
202210
|| Convert.GetTypeCode(type) != TypeCode.Object;
203211
}
204212
}
205-
}
213+
}

src/CommandLine/Error.cs

+20-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ public enum ErrorType
6464
/// <summary>
6565
/// Value of <see cref="CommandLine.SetValueExceptionError"/> type.
6666
/// </summary>
67-
SetValueExceptionError
67+
68+
SetValueExceptionError,
69+
/// <summary>
70+
/// Value of <see cref="CommandLine.InvalidAttributeConfigurationError"/> type.
71+
/// </summary>
72+
InvalidAttributeConfigurationError
73+
6874
}
6975

7076
/// <summary>
@@ -506,6 +512,18 @@ public object Value
506512
{
507513
get { return value; }
508514
}
515+
}
516+
517+
/// <summary>
518+
/// Models an error generated when an invalid token is detected.
519+
/// </summary>
520+
public sealed class InvalidAttributeConfigurationError : Error
521+
{
522+
public const string ErrorMessage = "Check if Option or Value attribute values are set properly for the given type.";
509523

524+
internal InvalidAttributeConfigurationError()
525+
: base(ErrorType.InvalidAttributeConfigurationError, true)
526+
{
527+
}
510528
}
511-
}
529+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
namespace CommandLine.Tests.Fakes
3+
{
4+
class Options_With_InvalidDefaults
5+
{
6+
// Default of string and integer type property will also throw.
7+
8+
[Option(Default = false)]
9+
public string FileName { get; set; }
10+
}
11+
}

tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs

+17
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,22 @@ public void Parse_option_with_exception_thrown_from_setter_generates_SetValueExc
10071007
// Teardown
10081008
}
10091009

1010+
[Fact]
1011+
public void Parse_default_bool_type_string_SetValueExceptionError()
1012+
{
1013+
// Fixture setup
1014+
string name = nameof(Options_With_InvalidDefaults.FileName).ToLower();
1015+
var expectedResult = new[] { new SetValueExceptionError(new NameInfo("", name),
1016+
new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage), "bad") };
1017+
1018+
// Exercize system
1019+
var result = InvokeBuild<Options_With_InvalidDefaults>(
1020+
new[] { name, "bad" });
1021+
1022+
// Verify outcome
1023+
((NotParsed<Options_With_InvalidDefaults>)result).Errors.Should().BeEquivalentTo(expectedResult);
1024+
}
1025+
10101026

10111027
[Theory]
10121028
[InlineData(new[] { "--stringvalue", "x-" }, "x-")]
@@ -1133,6 +1149,7 @@ public void Parse_TimeSpan()
11331149
// Teardown
11341150
}
11351151

1152+
11361153
[Fact]
11371154
public void OptionClass_IsImmutable_HasNoCtor()
11381155
{

0 commit comments

Comments
 (0)