Skip to content

Add enum handling #42

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

Merged
merged 2 commits into from
Mar 14, 2019
Merged
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
5 changes: 5 additions & 0 deletions src/Utility.CommandLine.Arguments/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,11 @@ private static object ChangeType(object value, string argument, Type toType)
{
try
{
if (toType.IsEnum)
{
return Enum.Parse(toType, (string)value, true);
}

return Convert.ChangeType(value, toType);
}
catch (Exception ex)
Expand Down
37 changes: 35 additions & 2 deletions tests/Utility.CommandLine.Arguments.Tests/ArgumentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ public void Constructor()
[Collection("Arguments")]
public class Arguments
{
/// <summary>
/// Enums
/// </summary>
public enum Enums
{
/// <summary>
/// Foo
/// </summary>
Foo = 1,

/// <summary>
/// Bar
/// </summary>
Bar = 2,
}

#region Private Properties

/// <summary>
Expand Down Expand Up @@ -152,6 +168,12 @@ public class Arguments
[CommandLine.Argument('C', "CASE-SENSITIVE")]
private static string UpperCase { get; set; }

/// <summary>
/// Gets or sets an enum property.
/// </summary>
[CommandLine.Argument('e', "enum")]
private static Enums Enum { get; set; }

#endregion Private Properties

#region Public Methods
Expand All @@ -164,7 +186,7 @@ public void GetArgumentHelp()
{
var help = CommandLine.Arguments.GetArgumentHelp(typeof(Arguments)).ToList();

Assert.Equal(6, help.Count);
Assert.Equal(7, help.Count);
Assert.Single(help.Where(h => h.ShortName == 'b'));
Assert.Equal("help", help.Where(h => h.ShortName == 'b').FirstOrDefault().HelpText);
}
Expand All @@ -177,7 +199,7 @@ public void GetArgumentHelpNull()
{
var help = CommandLine.Arguments.GetArgumentHelp().ToList();

Assert.Equal(6, help.Count);
Assert.Equal(7, help.Count);
Assert.Single(help.Where(h => h.ShortName == 'b'));
Assert.Equal("help", help.Where(h => h.ShortName == 'b').FirstOrDefault().HelpText);
}
Expand Down Expand Up @@ -568,6 +590,17 @@ public void PopulateDecimal()
Assert.Equal(1.1M, Decimal);
}

/// <summary>
/// Tests the <see cref="CommandLine.Arguments.Populate(string, bool, string)"/> method with an enum value.
/// </summary>
[Fact]
public void PopulateEnum()
{
CommandLine.Arguments.Populate("--enum bar");

Assert.Equal(Enums.Bar, Enum);
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool, string)"/> method to assure that properties are not
/// "cleared" when clearing is explicitly disabled.
Expand Down