diff --git a/src/CommandLine/Core/ReflectionExtensions.cs b/src/CommandLine/Core/ReflectionExtensions.cs index 2f4faf0f..70f1afc5 100644 --- a/src/CommandLine/Core/ReflectionExtensions.cs +++ b/src/CommandLine/Core/ReflectionExtensions.cs @@ -93,10 +93,6 @@ public static IEnumerable SetProperties( private static IEnumerable SetValue(this SpecificationProperty specProp, T instance, object value) { - Action fail = inner => { - throw new InvalidOperationException(CannotSetValueToTargetInstance, inner); - }; - try { specProp.Property.SetValue(instance, value, null); @@ -106,17 +102,16 @@ private static IEnumerable SetValue(this SpecificationProperty specPro { return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e.InnerException, value) }; } - catch (Exception e) + catch (ArgumentException e) { - return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) }; + var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e); + + return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), argEx, value) }; } - catch(ArgumentException e) + catch (Exception e) { - var argEx = new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage, e); - fail(argEx); + return new[] { new SetValueExceptionError(specProp.Specification.FromSpecification(), e, value) }; } - - return instance; } public static object CreateEmptyArray(this Type type) @@ -215,4 +210,4 @@ public static bool IsPrimitiveEx(this Type type) || Convert.GetTypeCode(type) != TypeCode.Object; } } -} \ No newline at end of file +} diff --git a/src/CommandLine/Error.cs b/src/CommandLine/Error.cs index 90ee26d8..5a562689 100644 --- a/src/CommandLine/Error.cs +++ b/src/CommandLine/Error.cs @@ -65,8 +65,6 @@ public enum ErrorType /// Value of type. /// SetValueExceptionError, - - VersionRequestedError, /// /// Value of type. /// @@ -512,6 +510,18 @@ public object Value { get { return value; } } + } + /// + /// Models an error generated when an invalid token is detected. + /// + public sealed class InvalidAttributeConfigurationError : Error + { + public const string ErrorMessage = "Check if Option or Value attribute values are set properly for the given type."; + + internal InvalidAttributeConfigurationError() + : base(ErrorType.InvalidAttributeConfigurationError, true) + { + } } -} \ No newline at end of file +} diff --git a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs index eef9a674..c6234089 100644 --- a/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs +++ b/tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs @@ -1007,6 +1007,22 @@ public void Parse_option_with_exception_thrown_from_setter_generates_SetValueExc // Teardown } + [Fact] + public void Parse_default_bool_type_string_SetValueExceptionError() + { + // Fixture setup + string name = nameof(Options_With_InvalidDefaults.FileName).ToLower(); + var expectedResult = new[] { new SetValueExceptionError(new NameInfo("", name), + new ArgumentException(InvalidAttributeConfigurationError.ErrorMessage), "bad") }; + + // Exercize system + var result = InvokeBuild( + new[] { name, "bad" }); + + // Verify outcome + ((NotParsed)result).Errors.Should().BeEquivalentTo(expectedResult); + } + [Theory] [InlineData(new[] { "--stringvalue", "x-" }, "x-")] @@ -1133,20 +1149,6 @@ public void Parse_TimeSpan() // Teardown } - [Fact] - public void Build_DefaultBoolTypeString_ThrowsInvalidOperationException() - { - // Exercize system - Action test = () => InvokeBuild( - new string[] { }); - - // Verify outcome - test.ShouldThrow() - .WithMessage(ReflectionExtensions.CannotSetValueToTargetInstance) - .WithInnerException() - .WithInnerMessage(InvalidAttributeConfigurationError.ErrorMessage); - } - [Fact] public void OptionClass_IsImmutable_HasNoCtor() @@ -1163,7 +1165,7 @@ private class ValueWithNoSetterOptions } - public static IEnumerable RequiredValueStringData + public static IEnumerable RequiredValueStringData { get {