Skip to content

Fix Populate() handling of async calling methods #41

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 3 commits into from
Mar 13, 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
19 changes: 15 additions & 4 deletions src/Utility.CommandLine.Arguments/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

[assembly: SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleClass", Justification = "Reviewed.")]
Expand Down Expand Up @@ -80,7 +81,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, bool)"/> method.
/// when invoking the <see cref="Arguments.Populate(string, bool, string)"/> method.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class ArgumentAttribute : Attribute
Expand Down Expand Up @@ -274,9 +275,19 @@ public static IEnumerable<ArgumentHelp> GetArgumentHelp(Type type = null)
/// </summary>
/// <param name="commandLineString">The command line arguments with which the application was started.</param>
/// <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)
/// <param name="caller">Internal parameter used to identify the calling method.</param>
public static void Populate(string commandLineString = default(string), bool clearExistingValues = true, [CallerMemberName] string caller = default(string))
{
Populate(new StackFrame(1).GetMethod().DeclaringType, Parse(commandLineString), clearExistingValues);
var callingMethod = new StackTrace().GetFrames()
.Select(f => f.GetMethod())
.Where(m => m.Name == caller).FirstOrDefault();

if (callingMethod == default(MethodBase))
{
throw new InvalidOperationException("Error populating arguments; Unable to determine the containing type of Main(). Use Populate(typeof(<class containing main>))");
}

Populate(callingMethod.DeclaringType, Parse(commandLineString), clearExistingValues);
}

/// <summary>
Expand Down Expand Up @@ -673,7 +684,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, bool)"/> method.
/// the <see cref="Arguments.Populate(string, bool, string)"/> method.
/// </summary>
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class OperandsAttribute : Attribute
Expand Down
30 changes: 21 additions & 9 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, bool)"/> method with the default values.
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool, string)"/> method with the default values.
/// </summary>
[Fact]
public void Populate()
Expand All @@ -528,7 +528,19 @@ public void Populate()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with an explicit command line string
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool, string)"/> method with a bogus caller.
/// </summary>
[Fact]
public void PopulateBogusCaller()
{
Exception ex = Record.Exception(() => CommandLine.Arguments.Populate("-b", true, Guid.NewGuid().ToString()));

Assert.NotNull(ex);
Assert.IsType<InvalidOperationException>(ex);
}

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

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

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method to assure that properties are not
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool, string)"/> method to assure that properties are not
/// "cleared" when clearing is explicitly disabled.
/// </summary>
[Fact]
Expand Down Expand Up @@ -630,7 +642,7 @@ public void PopulateOperands()
}

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

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

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

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method with an explicit type and command
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool, string)"/> 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 @@ -680,7 +692,7 @@ public void PopulateTypeMismatch()
}

/// <summary>
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool)"/> method to assure that properties are "cleared"
/// Tests the <see cref="Utility.CommandLine.Arguments.Populate(string, bool, string)"/> method to assure that properties are "cleared"
/// prior to populating values.
/// </summary>
[Fact]
Expand Down