From 1d29354c2f958d506f479d4c36d7b3d6542b122e Mon Sep 17 00:00:00 2001 From: Alex Ghiondea Date: Wed, 10 Jun 2020 13:56:54 -0700 Subject: [PATCH] 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. --- src/Analysis/PropertyHelpers.cs | 3 +- src/CommandLine.csproj | 5 +- test/CommandLineTests.Functional.cs | 21 +++++++ test/TestObjects/Action.cs | 8 +++ test/TestObjects/OverridePositionGroup.cs | 57 ------------------- test/TestObjects/OverridePositionGroup2.cs | 21 +++++++ .../OverridePositionGroupWithMoreArgs.cs | 26 +++++++++ .../OverridePositionGroup_Conflict.cs | 20 +++++++ test/TestObjects/SimpleType3.cs | 25 ++++++++ 9 files changed, 126 insertions(+), 60 deletions(-) create mode 100644 test/TestObjects/Action.cs create mode 100644 test/TestObjects/OverridePositionGroup2.cs create mode 100644 test/TestObjects/OverridePositionGroupWithMoreArgs.cs create mode 100644 test/TestObjects/OverridePositionGroup_Conflict.cs create mode 100644 test/TestObjects/SimpleType3.cs diff --git a/src/Analysis/PropertyHelpers.cs b/src/Analysis/PropertyHelpers.cs index 9e151fa..c5ea1ad 100644 --- a/src/Analysis/PropertyHelpers.cs +++ b/src/Analysis/PropertyHelpers.cs @@ -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; diff --git a/src/CommandLine.csproj b/src/CommandLine.csproj index c1a0486..6b74540 100644 --- a/src/CommandLine.csproj +++ b/src/CommandLine.csproj @@ -7,7 +7,7 @@ - 2.2.0 + 2.2.1 $(AssemblyVersion) 2.2.0 @@ -20,12 +20,13 @@ Parse command line arguments into user defined objects 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. - Alex Ghiondea (c) 2019 + Alex Ghiondea (c) 2020 https://raw.githubusercontent.com/AlexGhiondea/CommandLine/master/LICENSE https://github.com/AlexGhiondea/CommandLine Git CommandLine, Command line, Command, Line, parser, objects, custom 7.1 + 2.2.1 diff --git a/test/CommandLineTests.Functional.cs b/test/CommandLineTests.Functional.cs index 02ffdd8..ae93440 100644 --- a/test/CommandLineTests.Functional.cs +++ b/test/CommandLineTests.Functional.cs @@ -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 GetParserOptions() { yield return new object[] { (ParserOptions)null }; diff --git a/test/TestObjects/Action.cs b/test/TestObjects/Action.cs new file mode 100644 index 0000000..421f6e2 --- /dev/null +++ b/test/TestObjects/Action.cs @@ -0,0 +1,8 @@ +namespace CommandLine.Tests +{ + public enum Action + { + Create, + List + } +} diff --git a/test/TestObjects/OverridePositionGroup.cs b/test/TestObjects/OverridePositionGroup.cs index 7510896..71a3e05 100644 --- a/test/TestObjects/OverridePositionGroup.cs +++ b/test/TestObjects/OverridePositionGroup.cs @@ -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 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 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 Repositories2 { get; set; } - } - - public enum Action - { - Create, - List - } } diff --git a/test/TestObjects/OverridePositionGroup2.cs b/test/TestObjects/OverridePositionGroup2.cs new file mode 100644 index 0000000..154acd5 --- /dev/null +++ b/test/TestObjects/OverridePositionGroup2.cs @@ -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; } + } +} diff --git a/test/TestObjects/OverridePositionGroupWithMoreArgs.cs b/test/TestObjects/OverridePositionGroupWithMoreArgs.cs new file mode 100644 index 0000000..2e3c33d --- /dev/null +++ b/test/TestObjects/OverridePositionGroupWithMoreArgs.cs @@ -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; } + } +} diff --git a/test/TestObjects/OverridePositionGroup_Conflict.cs b/test/TestObjects/OverridePositionGroup_Conflict.cs new file mode 100644 index 0000000..b86d51d --- /dev/null +++ b/test/TestObjects/OverridePositionGroup_Conflict.cs @@ -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 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 Repositories2 { get; set; } + } +} diff --git a/test/TestObjects/SimpleType3.cs b/test/TestObjects/SimpleType3.cs new file mode 100644 index 0000000..5faea0e --- /dev/null +++ b/test/TestObjects/SimpleType3.cs @@ -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 + { + /// + /// Type of action by files + /// + [RequiredArgument(0, "action", "what need do: convert or unconvert")] + public ActionType ActionType { get; private set; } + } + + /// + /// Action type + /// + public enum ActionType + { + Convert, + UnConvert + } +}