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

Proposed changes for enhancement: #314

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#version should be only changed with RELEASE eminent, see RELEASE.md
version: 2.4.{build}

image: Visual Studio 2017

clone_depth: 1
pull_requests:
do_not_increment_build_number: true
Expand Down
4 changes: 2 additions & 2 deletions src/CommandLine/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Authors>gsscoder;nemec;ericnewton76</Authors>
<Title>Command Line Parser Library</Title>
<Version Condition="'$(VersionSuffix)' != ''">$(VersionSuffix)</Version>
<Version Condition="'$(VersionSuffix)' == ''">2.4.0</Version>
<Version Condition="'$(VersionSuffix)' == ''">0.0.0</Version>
<Description Condition="'$(BuildTarget)' != 'fsharp'">Terse syntax C# command line parser for .NET. For FSharp support see CommandLineParser.FSharp. The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks.</Description>
<Description Condition="'$(BuildTarget)' == 'fsharp'">Terse syntax C# command line parser for .NET with F# support. The Command Line Parser Library offers to CLR applications a clean and concise API for manipulating command line arguments and related tasks.</Description>
<Copyright>Copyright (c) 2005 - 2018 Giacomo Stelluti Scala &amp; Contributors</Copyright>
Expand All @@ -35,7 +35,7 @@
</Content>
</ItemGroup>

<ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' != 'net40' and '$(BuildTarget)' == 'fsharp'">
<PackageReference Include="FSharp.Core" Version="4.5.1" Condition="'$(BuildTarget)' == 'fsharp'" />
</ItemGroup>

Expand Down
19 changes: 7 additions & 12 deletions src/CommandLine/Core/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ public static IEnumerable<Error> SetProperties<T>(

private static IEnumerable<Error> SetValue<T>(this SpecificationProperty specProp, T instance, object value)
{
Action<Exception> fail = inner => {
throw new InvalidOperationException(CannotSetValueToTargetInstance, inner);
};

try
{
specProp.Property.SetValue(instance, value, null);
Expand All @@ -106,17 +102,16 @@ private static IEnumerable<Error> SetValue<T>(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)
Expand Down Expand Up @@ -215,4 +210,4 @@ public static bool IsPrimitiveEx(this Type type)
|| Convert.GetTypeCode(type) != TypeCode.Object;
}
}
}
}
17 changes: 14 additions & 3 deletions src/CommandLine/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public enum ErrorType
/// <summary>
/// Value of <see cref="CommandLine.SetValueExceptionError"/> type.
/// </summary>
SetValueExceptionError
VersionRequestedError,
SetValueExceptionError,
/// <summary>
/// Value of <see cref="CommandLine.InvalidAttributeConfigurationError"/> type.
/// </summary>
Expand Down Expand Up @@ -511,6 +510,18 @@ public object Value
{
get { return value; }
}
}

/// <summary>
/// Models an error generated when an invalid token is detected.
/// </summary>
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)
{
}
}
}
}
32 changes: 17 additions & 15 deletions tests/CommandLine.Tests/Unit/Core/InstanceBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Options_With_InvalidDefaults>(
new[] { name, "bad" });

// Verify outcome
((NotParsed<Options_With_InvalidDefaults>)result).Errors.Should().BeEquivalentTo(expectedResult);
}


[Theory]
[InlineData(new[] { "--stringvalue", "x-" }, "x-")]
Expand Down Expand Up @@ -1133,20 +1149,6 @@ public void Parse_TimeSpan()
// Teardown
}

[Fact]
public void Build_DefaultBoolTypeString_ThrowsInvalidOperationException()
{
// Exercize system
Action test = () => InvokeBuild<Options_With_InvalidDefaults>(
new string[] { });

// Verify outcome
test.ShouldThrow<InvalidOperationException>()
.WithMessage(ReflectionExtensions.CannotSetValueToTargetInstance)
.WithInnerException<ArgumentException>()
.WithInnerMessage(InvalidAttributeConfigurationError.ErrorMessage);
}


[Fact]
public void OptionClass_IsImmutable_HasNoCtor()
Expand All @@ -1163,7 +1165,7 @@ private class ValueWithNoSetterOptions
}


public static IEnumerable<object> RequiredValueStringData
public static IEnumerable<object[]> RequiredValueStringData
{
get
{
Expand Down