Skip to content

Add support for default values #39

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 7 commits into from
Mar 3, 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
1 change: 1 addition & 0 deletions examples/Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ private static void Reset()
{
String = string.Empty;
Bool = false;
Help = false;
Int = 0;
Double = 0;
List = new List<int>();
Expand Down
22 changes: 14 additions & 8 deletions src/Utility.CommandLine.Arguments/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal static void ExclusiveAdd(this Dictionary<string, object> dictionary, st

/// <summary>
/// Indicates that the property is to be used as a target for automatic population of values from command line arguments
/// when invoking the <see cref="Arguments.Populate(string)"/> method.
/// when invoking the <see cref="Arguments.Populate(string, bool)"/> method.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ArgumentAttribute : Attribute
Expand Down Expand Up @@ -273,9 +273,10 @@ public static IEnumerable<ArgumentHelp> GetArgumentHelp(Type type = null)
/// arguments, if present.
/// </summary>
/// <param name="commandLineString">The command line arguments with which the application was started.</param>
public static void Populate(string commandLineString = default(string))
/// <param name="clearExistingValues">Whether to clear the properties before populating them. Defaults to true.</param>
public static void Populate(string commandLineString = default(string), bool clearExistingValues = true)
{
Populate(new StackFrame(1).GetMethod().DeclaringType, Parse(commandLineString));
Populate(new StackFrame(1).GetMethod().DeclaringType, Parse(commandLineString), clearExistingValues);
}

/// <summary>
Expand All @@ -287,9 +288,10 @@ public static IEnumerable<ArgumentHelp> GetArgumentHelp(Type type = null)
/// The Type for which the static properties matching the list of command line arguments are to be populated.
/// </param>
/// <param name="commandLineString">The command line arguments with which the application was started.</param>
public static void Populate(Type type, string commandLineString = default(string))
/// <param name="clearExistingValues">Whether to clear the properties before populating them. Defaults to true.</param>
public static void Populate(Type type, string commandLineString = default(string), bool clearExistingValues = true)
{
Populate(type, Parse(commandLineString));
Populate(type, Parse(commandLineString), clearExistingValues);
}

/// <summary>
Expand All @@ -304,12 +306,16 @@ public static IEnumerable<ArgumentHelp> GetArgumentHelp(Type type = null)
/// The Arguments object containing the dictionary containing the argument-value pairs with which the destination
/// properties should be populated and the list of operands.
/// </param>
public static void Populate(Type type, Arguments arguments)
/// <param name="clearExistingValues">Whether to clear the properties before populating them. Defaults to true.</param>
public static void Populate(Type type, Arguments arguments, bool clearExistingValues = true)
{
// fetch any properties in the specified type marked with the ArgumentAttribute attribute and clear them
Dictionary<string, PropertyInfo> properties = GetArgumentProperties(type);

ClearProperties(properties);
if (clearExistingValues)
{
ClearProperties(properties);
}

foreach (string propertyName in properties.Keys)
{
Expand Down Expand Up @@ -667,7 +673,7 @@ public class ArgumentHelp

/// <summary>
/// Indicates that the property is to be used as the target for automatic population of command line operands when invoking
/// the <see cref="Arguments.Populate(string)"/> method.
/// the <see cref="Arguments.Populate(string, bool)"/> method.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class OperandsAttribute : Attribute
Expand Down
60 changes: 39 additions & 21 deletions tests/Utility.CommandLine.Arguments.Tests/ArgumentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ public void ParseValueWithQuotedPeriod()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string)"/> method with the default values.
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with the default values.
/// </summary>
[Fact]
public void Populate()
Expand All @@ -528,7 +528,7 @@ public void Populate()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string)"/> method with an explicit command line string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with an explicit command line string
/// containing both upper and lower case arguments.
/// </summary>
[Fact]
Expand All @@ -546,7 +546,7 @@ public void PopulateCaseSensitive()
}

/// <summary>
/// Tests the <see cref="CommandLine.Arguments.Populate(string)"/> method with a decimal value.
/// Tests the <see cref="CommandLine.Arguments.Populate(string, bool)"/> method with a decimal value.
/// </summary>
[Fact]
public void PopulateDecimal()
Expand All @@ -557,7 +557,25 @@ public void PopulateDecimal()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit command line
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method to assure that properties are not
/// "cleared" when clearing is explicitly disabled.
/// </summary>
[Fact]
public void PopulateDisableClearing()
{
Bool = true;
Decimal = 3.5m;
Integer = 42;

CommandLine.Arguments.Populate(string.Empty, false);

Assert.True(Bool);
Assert.Equal(3.5m, Decimal);
Assert.Equal(42, Integer);
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit command line
/// string and with a class containing duplicate properties.
/// </summary>
[Fact]
Expand All @@ -572,7 +590,7 @@ public void PopulateDuplicateProperties()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with a Type external to the
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with a Type external to the
/// calling class and with an explicit string.
/// </summary>
[Fact]
Expand All @@ -586,7 +604,7 @@ public void PopulateExternalClass()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with a string containing
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with a string containing
/// multiple values for an argument which is not backed by a collection.
/// </summary>
[Fact]
Expand All @@ -599,7 +617,7 @@ public void PopulateMultipleValuesNotCollectionBacked()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit command line
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit command line
/// string containing two operands.
/// </summary>
[Fact]
Expand All @@ -612,7 +630,7 @@ public void PopulateOperands()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string)"/> method with an explicit command line string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with an explicit command line string
/// containing multiple short names.
/// </summary>
[Fact]
Expand All @@ -625,7 +643,7 @@ public void PopulateShortNames()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string)"/> method with an explicit command line string.
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with an explicit command line string.
/// </summary>
[Fact]
public void PopulateString()
Expand All @@ -638,7 +656,7 @@ public void PopulateString()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string)"/> method with an explicit type.
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with an explicit type.
/// </summary>
[Fact]
public void PopulateType()
Expand All @@ -649,7 +667,7 @@ public void PopulateType()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string)"/> method with an explicit type and command
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with an explicit type and command
/// line string, where the string contains a value which does not match the type of the destination property.
/// </summary>
[Fact]
Expand All @@ -662,7 +680,7 @@ public void PopulateTypeMismatch()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string)"/> method to assure that properties are "cleared"
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method to assure that properties are "cleared"
/// prior to populating values.
/// </summary>
[Fact]
Expand Down Expand Up @@ -766,7 +784,7 @@ public class TestClassWithArrayOperands
#region Public Methods

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing two operands, and with a Type containing a property of type <see langword="string[]"/> marked with the
/// <see cref="Operands"/> attribute.
/// </summary>
Expand Down Expand Up @@ -802,7 +820,7 @@ public class TestClassWithArrayProperty
#region Public Methods

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing multiple instances of the same argument.
/// </summary>
[Fact]
Expand All @@ -818,7 +836,7 @@ public void Populate()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing a single a single instance of an array-backed argument.
/// </summary>
[Fact]
Expand Down Expand Up @@ -856,7 +874,7 @@ public class TestClassWithBadOperands
#region Public Methods

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing two operands, and with a Type containing a property marked with the <see cref="Operands"/> attribute but
/// that is not of type <see cref="T:string[]"/> or <see cref="List{T}"/>}"/&gt; .
/// </summary>
Expand Down Expand Up @@ -892,7 +910,7 @@ public class TestClassWithListProperty
#region Public Methods

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing multiple instances of a list-backed argument.
/// </summary>
[Fact]
Expand All @@ -908,7 +926,7 @@ public void Populate()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing a single a single instance of a list-backed argument.
/// </summary>
[Fact]
Expand All @@ -934,7 +952,7 @@ public class TestClassWithNoProperties
#region Public Methods

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing a single argument/value pair, and with a Type containing no properties.
/// </summary>
[Fact]
Expand Down Expand Up @@ -988,7 +1006,7 @@ public class TestClassWithBoolProperty
private static List<string> Operands { get; set; }

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing a single boolean followed by a single operand.
/// </summary>
[Fact]
Expand All @@ -1003,7 +1021,7 @@ public void PopulateBoolFollowedByOperand()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string)"/> method with an explicit string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(Type, string, bool)"/> method with an explicit string
/// containing a single boolean followed by two operands.
/// </summary>
[Fact]
Expand Down