Skip to content

Commit 57d0ed6

Browse files
committed
Added the ability to use Public properties as the target for Populate(). Added tests for this functionality.
1 parent d0fe87d commit 57d0ed6

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

Utility.CommandLine.Arguments.Tests/Arguments.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,20 @@ public void PopulateTypeMismatch()
494494
Assert.IsType<ArgumentException>(ex);
495495
}
496496

497+
/// <summary>
498+
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with a Type external to the
499+
/// calling class and with an explicit string.
500+
/// </summary>
501+
[Fact]
502+
public void PopulateExternalClass()
503+
{
504+
CommandLine.Arguments.Populate(typeof(TestClassPublicProperties), "--test test! operand1 operand2");
505+
506+
Assert.Equal("test!", TestClassPublicProperties.Test);
507+
Assert.Equal("operand1", TestClassPublicProperties.Operands[0]);
508+
Assert.Equal("operand2", TestClassPublicProperties.Operands[1]);
509+
}
510+
497511
#endregion Public Methods
498512
}
499513

@@ -532,7 +546,7 @@ public class TestClassWithArrayOperands
532546
/// Gets or sets a test property.
533547
/// </summary>
534548
[CommandLine.Operands]
535-
private static string[] Operands { get; set; }
549+
public static string[] Operands { get; set; }
536550

537551
#endregion Private Properties
538552

@@ -616,4 +630,23 @@ public void Populate()
616630

617631
#endregion Public Methods
618632
}
633+
634+
/// <summary>
635+
/// Unit tests for the <see cref="CommandLine.Arguments"/> class.
636+
/// </summary>
637+
/// <remarks>Used to facilitate testing of a class with public properties.</remarks>
638+
public class TestClassPublicProperties
639+
{
640+
/// <summary>
641+
/// Gets or sets a test property.
642+
/// </summary>
643+
[CommandLine.Argument('t', "test")]
644+
public static string Test { get; set; }
645+
646+
/// <summary>
647+
/// Gets or sets a test property.
648+
/// </summary>
649+
[CommandLine.Operands]
650+
public static string[] Operands { get; set; }
651+
}
619652
}

Utility.CommandLine.Arguments/Arguments.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ private static Dictionary<string, PropertyInfo> GetArgumentProperties(Type type)
434434
{
435435
Dictionary<string, PropertyInfo> properties = new Dictionary<string, PropertyInfo>();
436436

437-
foreach (PropertyInfo property in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Static))
437+
foreach (PropertyInfo property in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static))
438438
{
439439
// attempt to fetch the ArgumentAttribute of the property
440440
CustomAttributeData attribute = property.CustomAttributes.Where(a => a.AttributeType.Name == typeof(ArgumentAttribute).Name).FirstOrDefault();
@@ -521,7 +521,7 @@ private static List<string> GetOperandListStrict(string operandListString)
521521
/// </exception>
522522
private static PropertyInfo GetOperandsProperty(Type type)
523523
{
524-
PropertyInfo property = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Static)
524+
PropertyInfo property = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)
525525
.Where(p => p.CustomAttributes
526526
.Any(a => a.AttributeType.Name == typeof(OperandsAttribute).Name))
527527
.FirstOrDefault();

0 commit comments

Comments
 (0)