Skip to content

Commit

Permalink
Fix casing for enums (#64)
Browse files Browse the repository at this point in the history
* Refactor object types into separate files.

* Ensure that we do a case-insensitive parse of the enum.

* Bump package version in preparation of release.

* Add a couple more tests.
  • Loading branch information
AlexGhiondea authored Jun 10, 2020
1 parent ecd99b2 commit 1d29354
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/Analysis/PropertyHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static object GetValueForProperty(string value, PropertyInfo targetType)
{
if (TypeHelpers.IsEnum(targetType.PropertyType))
{
return Enum.Parse(targetType.PropertyType, value);
// We are going to parse the enum as case insensitive
return Enum.Parse(targetType.PropertyType, value, true);
}

return value;
Expand Down
5 changes: 3 additions & 2 deletions src/CommandLine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<PropertyGroup>
<AssemblyVersion>2.2.0</AssemblyVersion>
<AssemblyVersion>2.2.1</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<VersionPrefix>2.2.0</VersionPrefix>
</PropertyGroup>
Expand All @@ -20,12 +20,13 @@
<Description>Parse command line arguments into user defined objects</Description>
<releaseNotes>Fix an issue where TryParse returns true when parsing help which leads to a null reference later.
Also ensure that Parse throws a ParseException when the help was requested to ensure we don't end up with null options after parsing.</releaseNotes>
<Copyright>Alex Ghiondea (c) 2019</Copyright>
<Copyright>Alex Ghiondea (c) 2020</Copyright>
<PackageLicenseUrl>https://raw.githubusercontent.com/AlexGhiondea/CommandLine/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/AlexGhiondea/CommandLine</PackageProjectUrl>
<RepositoryType>Git</RepositoryType>
<PackageTags>CommandLine, Command line, Command, Line, parser, objects, custom</PackageTags>
<LangVersion>7.1</LangVersion>
<Version>2.2.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
21 changes: 21 additions & 0 deletions test/CommandLineTests.Functional.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,27 @@ public void ParseTwoOptionalCollections()
Helpers.CollectionEquals(options.List, "d", "e", "f");
}

[Trait("Category", "Basic")]
[Fact]
public void ParseEnumCaseSensitive()
{
SimpleType3 outputType;
var outcome = Parser.TryParse("convert", out outputType);

Assert.True(outcome);
Assert.Equal(ActionType.Convert, outputType.ActionType);

outputType = null;
outcome = Parser.TryParse("conVErt", out outputType);
Assert.True(outcome);
Assert.Equal(ActionType.Convert, outputType.ActionType);

outputType = null;
outcome = Parser.TryParse("Convert", out outputType);
Assert.True(outcome);
Assert.Equal(ActionType.Convert, outputType.ActionType);
}

public static IEnumerable<object[]> GetParserOptions()
{
yield return new object[] { (ParserOptions)null };
Expand Down
8 changes: 8 additions & 0 deletions test/TestObjects/Action.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace CommandLine.Tests
{
public enum Action
{
Create,
List
}
}
57 changes: 0 additions & 57 deletions test/TestObjects/OverridePositionGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,61 +20,4 @@ class OverridePositionGroup
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
public List<string> Repositories { get; set; }
}

class OverridePositionGroupWithMoreArgs
{
[ActionArgument]
public Action Action { get; set; }

[ArgumentGroup(nameof(Action.List), 1)]
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
public string One { get; set; }

[ArgumentGroup(nameof(Action.List), 0)]
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(1, "milestoneInputFile2", "The file containing the list of milestones to create.")]
public string Two { get; set; }

[ArgumentGroup(nameof(Action.List), 2)]
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(2, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
public string Three { get; set; }
}

class OverridePositionGroup2
{
[ActionArgument]
public Action Action { get; set; }

[ArgumentGroup(nameof(Action.List), 0)]
[ArgumentGroup(nameof(Action.Create), 1)]
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
public string MilestoneFile { get; set; }

[ArgumentGroup(nameof(Action.Create), 0)]
[ArgumentGroup(nameof(Action.List), 1)]
[RequiredArgument(1, "repo", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
public string Repository { get; set; }
}

class OverridePositionGroup_Conflict
{
[ActionArgument]
public Action Action { get; set; }

[ArgumentGroup(nameof(Action.List), 0)]
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
public List<string> Repositories { get; set; }

[ArgumentGroup(nameof(Action.List))]
[RequiredArgument(0, "test", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
public List<string> Repositories2 { get; set; }
}

public enum Action
{
Create,
List
}
}
21 changes: 21 additions & 0 deletions test/TestObjects/OverridePositionGroup2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using CommandLine.Attributes;
using CommandLine.Attributes.Advanced;

namespace CommandLine.Tests
{
class OverridePositionGroup2
{
[ActionArgument]
public Action Action { get; set; }

[ArgumentGroup(nameof(Action.List), 0)]
[ArgumentGroup(nameof(Action.Create), 1)]
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
public string MilestoneFile { get; set; }

[ArgumentGroup(nameof(Action.Create), 0)]
[ArgumentGroup(nameof(Action.List), 1)]
[RequiredArgument(1, "repo", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
public string Repository { get; set; }
}
}
26 changes: 26 additions & 0 deletions test/TestObjects/OverridePositionGroupWithMoreArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CommandLine.Attributes;
using CommandLine.Attributes.Advanced;

namespace CommandLine.Tests
{
class OverridePositionGroupWithMoreArgs
{
[ActionArgument]
public Action Action { get; set; }

[ArgumentGroup(nameof(Action.List), 1)]
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
public string One { get; set; }

[ArgumentGroup(nameof(Action.List), 0)]
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(1, "milestoneInputFile2", "The file containing the list of milestones to create.")]
public string Two { get; set; }

[ArgumentGroup(nameof(Action.List), 2)]
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(2, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
public string Three { get; set; }
}
}
20 changes: 20 additions & 0 deletions test/TestObjects/OverridePositionGroup_Conflict.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using CommandLine.Attributes;
using CommandLine.Attributes.Advanced;
using System.Collections.Generic;

namespace CommandLine.Tests
{
internal class OverridePositionGroup_Conflict
{
[ActionArgument]
public Action Action { get; set; }

[ArgumentGroup(nameof(Action.List), 0)]
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
public List<string> Repositories { get; set; }

[ArgumentGroup(nameof(Action.List))]
[RequiredArgument(0, "test", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
public List<string> Repositories2 { get; set; }
}
}
25 changes: 25 additions & 0 deletions test/TestObjects/SimpleType3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CommandLine.Attributes;
using CommandLine.Attributes.Advanced;
using System.Collections.Generic;

namespace CommandLine.Tests.TestObjects
{
// define required collection property as first one
class SimpleType3
{
/// <summary>
/// Type of action by files
/// </summary>
[RequiredArgument(0, "action", "what need do: convert or unconvert")]
public ActionType ActionType { get; private set; }
}

/// <summary>
/// Action type
/// </summary>
public enum ActionType
{
Convert,
UnConvert
}
}

0 comments on commit 1d29354

Please sign in to comment.