Skip to content

Commit aa20943

Browse files
committed
PowerArgs enum support.
1 parent 0db32fb commit aa20943

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

DataBoss.Specs/PowerArgsSpec.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
44
using System.ComponentModel.DataAnnotations;
@@ -149,5 +149,30 @@ public void numbers_are_not_parameter_names() {
149149
() => args.Count == 1,
150150
() => args["Value"] == "-1");
151151
}
152+
153+
enum MyEnum
154+
{
155+
Nothing,
156+
Something
157+
}
158+
159+
class MyArgsWithEnum
160+
{
161+
public MyEnum A;
162+
public MyEnum B;
163+
[DefaultValue(MyEnum.Something)]
164+
public MyEnum C;
165+
public MyEnum FromInt;
166+
}
167+
168+
public void enums() => Check
169+
.With(() => PowerArgs.Parse("-A", "Nothing", "-B", "Something", "-FromInt", $"{(int)MyEnum.Something}"))
170+
.That(
171+
args => args.Count == 3,
172+
args => args.Into<MyArgsWithEnum>().A == MyEnum.Nothing,
173+
args => args.Into<MyArgsWithEnum>().B == MyEnum.Something,
174+
args => args.Into<MyArgsWithEnum>().C == MyEnum.Something,
175+
args => args.Into<MyArgsWithEnum>().FromInt == MyEnum.Something
176+
);
152177
}
153178
}

DataBoss/PowerArgs.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Concurrent;
44
using System.Collections.Generic;
@@ -161,6 +161,15 @@ bool TryParse(string input, Type targetType, out object result) {
161161
result = input;
162162
return true;
163163
}
164+
if(targetType.IsEnum) {
165+
try {
166+
result = Enum.Parse(targetType, input);
167+
return true;
168+
} catch {
169+
result = null;
170+
return false;
171+
}
172+
}
164173
var hasParse = targetType.GetMethod("Parse", new []{ typeof(string) } );
165174
if(hasParse != null) {
166175
try {

DataBoss/Properties/Version.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Reflection;
1+
using System.Reflection;
22

33
[assembly: AssemblyVersion("0.0.0.30")]
4-
[assembly: AssemblyInformationalVersion("0.0.30")]
4+
[assembly: AssemblyInformationalVersion("0.0.31")]

0 commit comments

Comments
 (0)