Skip to content

Commit 1d29354

Browse files
authored
Fix casing for enums (#64)
* 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.
1 parent ecd99b2 commit 1d29354

9 files changed

Lines changed: 126 additions & 60 deletions

src/Analysis/PropertyHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public static object GetValueForProperty(string value, PropertyInfo targetType)
1414
{
1515
if (TypeHelpers.IsEnum(targetType.PropertyType))
1616
{
17-
return Enum.Parse(targetType.PropertyType, value);
17+
// We are going to parse the enum as case insensitive
18+
return Enum.Parse(targetType.PropertyType, value, true);
1819
}
1920

2021
return value;

src/CommandLine.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<PropertyGroup>
10-
<AssemblyVersion>2.2.0</AssemblyVersion>
10+
<AssemblyVersion>2.2.1</AssemblyVersion>
1111
<FileVersion>$(AssemblyVersion)</FileVersion>
1212
<VersionPrefix>2.2.0</VersionPrefix>
1313
</PropertyGroup>
@@ -20,12 +20,13 @@
2020
<Description>Parse command line arguments into user defined objects</Description>
2121
<releaseNotes>Fix an issue where TryParse returns true when parsing help which leads to a null reference later.
2222
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>
23-
<Copyright>Alex Ghiondea (c) 2019</Copyright>
23+
<Copyright>Alex Ghiondea (c) 2020</Copyright>
2424
<PackageLicenseUrl>https://raw.githubusercontent.com/AlexGhiondea/CommandLine/master/LICENSE</PackageLicenseUrl>
2525
<PackageProjectUrl>https://github.com/AlexGhiondea/CommandLine</PackageProjectUrl>
2626
<RepositoryType>Git</RepositoryType>
2727
<PackageTags>CommandLine, Command line, Command, Line, parser, objects, custom</PackageTags>
2828
<LangVersion>7.1</LangVersion>
29+
<Version>2.2.1</Version>
2930
</PropertyGroup>
3031

3132
<ItemGroup>

test/CommandLineTests.Functional.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,27 @@ public void ParseTwoOptionalCollections()
274274
Helpers.CollectionEquals(options.List, "d", "e", "f");
275275
}
276276

277+
[Trait("Category", "Basic")]
278+
[Fact]
279+
public void ParseEnumCaseSensitive()
280+
{
281+
SimpleType3 outputType;
282+
var outcome = Parser.TryParse("convert", out outputType);
283+
284+
Assert.True(outcome);
285+
Assert.Equal(ActionType.Convert, outputType.ActionType);
286+
287+
outputType = null;
288+
outcome = Parser.TryParse("conVErt", out outputType);
289+
Assert.True(outcome);
290+
Assert.Equal(ActionType.Convert, outputType.ActionType);
291+
292+
outputType = null;
293+
outcome = Parser.TryParse("Convert", out outputType);
294+
Assert.True(outcome);
295+
Assert.Equal(ActionType.Convert, outputType.ActionType);
296+
}
297+
277298
public static IEnumerable<object[]> GetParserOptions()
278299
{
279300
yield return new object[] { (ParserOptions)null };

test/TestObjects/Action.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace CommandLine.Tests
2+
{
3+
public enum Action
4+
{
5+
Create,
6+
List
7+
}
8+
}

test/TestObjects/OverridePositionGroup.cs

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,61 +20,4 @@ class OverridePositionGroup
2020
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
2121
public List<string> Repositories { get; set; }
2222
}
23-
24-
class OverridePositionGroupWithMoreArgs
25-
{
26-
[ActionArgument]
27-
public Action Action { get; set; }
28-
29-
[ArgumentGroup(nameof(Action.List), 1)]
30-
[ArgumentGroup(nameof(Action.Create))]
31-
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
32-
public string One { get; set; }
33-
34-
[ArgumentGroup(nameof(Action.List), 0)]
35-
[ArgumentGroup(nameof(Action.Create))]
36-
[RequiredArgument(1, "milestoneInputFile2", "The file containing the list of milestones to create.")]
37-
public string Two { get; set; }
38-
39-
[ArgumentGroup(nameof(Action.List), 2)]
40-
[ArgumentGroup(nameof(Action.Create))]
41-
[RequiredArgument(2, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
42-
public string Three { get; set; }
43-
}
44-
45-
class OverridePositionGroup2
46-
{
47-
[ActionArgument]
48-
public Action Action { get; set; }
49-
50-
[ArgumentGroup(nameof(Action.List), 0)]
51-
[ArgumentGroup(nameof(Action.Create), 1)]
52-
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
53-
public string MilestoneFile { get; set; }
54-
55-
[ArgumentGroup(nameof(Action.Create), 0)]
56-
[ArgumentGroup(nameof(Action.List), 1)]
57-
[RequiredArgument(1, "repo", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
58-
public string Repository { get; set; }
59-
}
60-
61-
class OverridePositionGroup_Conflict
62-
{
63-
[ActionArgument]
64-
public Action Action { get; set; }
65-
66-
[ArgumentGroup(nameof(Action.List), 0)]
67-
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
68-
public List<string> Repositories { get; set; }
69-
70-
[ArgumentGroup(nameof(Action.List))]
71-
[RequiredArgument(0, "test", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
72-
public List<string> Repositories2 { get; set; }
73-
}
74-
75-
public enum Action
76-
{
77-
Create,
78-
List
79-
}
8023
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using CommandLine.Attributes;
2+
using CommandLine.Attributes.Advanced;
3+
4+
namespace CommandLine.Tests
5+
{
6+
class OverridePositionGroup2
7+
{
8+
[ActionArgument]
9+
public Action Action { get; set; }
10+
11+
[ArgumentGroup(nameof(Action.List), 0)]
12+
[ArgumentGroup(nameof(Action.Create), 1)]
13+
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
14+
public string MilestoneFile { get; set; }
15+
16+
[ArgumentGroup(nameof(Action.Create), 0)]
17+
[ArgumentGroup(nameof(Action.List), 1)]
18+
[RequiredArgument(1, "repo", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
19+
public string Repository { get; set; }
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using CommandLine.Attributes;
2+
using CommandLine.Attributes.Advanced;
3+
4+
namespace CommandLine.Tests
5+
{
6+
class OverridePositionGroupWithMoreArgs
7+
{
8+
[ActionArgument]
9+
public Action Action { get; set; }
10+
11+
[ArgumentGroup(nameof(Action.List), 1)]
12+
[ArgumentGroup(nameof(Action.Create))]
13+
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
14+
public string One { get; set; }
15+
16+
[ArgumentGroup(nameof(Action.List), 0)]
17+
[ArgumentGroup(nameof(Action.Create))]
18+
[RequiredArgument(1, "milestoneInputFile2", "The file containing the list of milestones to create.")]
19+
public string Two { get; set; }
20+
21+
[ArgumentGroup(nameof(Action.List), 2)]
22+
[ArgumentGroup(nameof(Action.Create))]
23+
[RequiredArgument(2, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.")]
24+
public string Three { get; set; }
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using CommandLine.Attributes;
2+
using CommandLine.Attributes.Advanced;
3+
using System.Collections.Generic;
4+
5+
namespace CommandLine.Tests
6+
{
7+
internal class OverridePositionGroup_Conflict
8+
{
9+
[ActionArgument]
10+
public Action Action { get; set; }
11+
12+
[ArgumentGroup(nameof(Action.List), 0)]
13+
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
14+
public List<string> Repositories { get; set; }
15+
16+
[ArgumentGroup(nameof(Action.List))]
17+
[RequiredArgument(0, "test", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
18+
public List<string> Repositories2 { get; set; }
19+
}
20+
}

test/TestObjects/SimpleType3.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using CommandLine.Attributes;
2+
using CommandLine.Attributes.Advanced;
3+
using System.Collections.Generic;
4+
5+
namespace CommandLine.Tests.TestObjects
6+
{
7+
// define required collection property as first one
8+
class SimpleType3
9+
{
10+
/// <summary>
11+
/// Type of action by files
12+
/// </summary>
13+
[RequiredArgument(0, "action", "what need do: convert or unconvert")]
14+
public ActionType ActionType { get; private set; }
15+
}
16+
17+
/// <summary>
18+
/// Action type
19+
/// </summary>
20+
public enum ActionType
21+
{
22+
Convert,
23+
UnConvert
24+
}
25+
}

0 commit comments

Comments
 (0)