Skip to content

Commit

Permalink
Make possible to explicitly define the assembly that will be used to …
Browse files Browse the repository at this point in the history
…read metadata (assembly attributes) to produce the help.
  • Loading branch information
mnivet committed Oct 19, 2021
1 parent 9a54d78 commit 853eff3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/CommandLine/Infrastructure/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ static class ReflectionHelper
/// </summary>
[ThreadStatic] private static IDictionary<Type, Attribute> _overrides;

private static Assembly _programAssembly;

public static Assembly ProgramAssembly
{
get => _programAssembly ?? GetExecutingOrEntryAssembly();
set => _programAssembly = value;
}

/// <summary>
/// Assembly attribute overrides for testing.
/// </summary>
Expand Down Expand Up @@ -51,12 +59,10 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()
Maybe.Nothing<TAttribute>();
}

var assembly = GetExecutingOrEntryAssembly();

#if NET40
var attributes = assembly.GetCustomAttributes(typeof(TAttribute), false);
var attributes = ProgramAssembly.GetCustomAttributes(typeof(TAttribute), false);
#else
var attributes = assembly.GetCustomAttributes<TAttribute>().ToArray();
var attributes = ProgramAssembly.GetCustomAttributes<TAttribute>().ToArray();
#endif

return attributes.Length > 0
Expand All @@ -66,14 +72,12 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()

public static string GetAssemblyName()
{
var assembly = GetExecutingOrEntryAssembly();
return assembly.GetName().Name;
return ProgramAssembly.GetName().Name;
}

public static string GetAssemblyVersion()
{
var assembly = GetExecutingOrEntryAssembly();
return assembly.GetName().Version.ToStringInvariant();
return ProgramAssembly.GetName().Version.ToStringInvariant();
}

public static bool IsFSharpOptionType(Type type)
Expand Down
11 changes: 11 additions & 0 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ public SentenceBuilder SentenceBuilder
get { return sentenceBuilder; }
}

/// <summary>
/// Gets or sets the <see cref="Assembly"/> that will be used to look for meta data (assembly attributes) to auto build the help.
/// By default the entry assembly is automatically selected, but this may not match what is really expected, in particular when
/// running unit tests.
/// </summary>
public static Assembly AutoBuildMetadataAssembly
{
get => ReflectionHelper.ProgramAssembly;
set => ReflectionHelper.ProgramAssembly = value;
}

/// <summary>
/// Creates a new instance of the <see cref="CommandLine.Text.HelpText"/> class using common defaults.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class HelpTextTests : IDisposable
public void Dispose()
{
ReflectionHelper.SetAttributeOverride(null);
HelpText.AutoBuildMetadataAssembly = null;
}

[Fact]
Expand Down Expand Up @@ -716,6 +717,21 @@ public void AutoBuild_with_assembly_company_attribute_only()
actualResult.Copyright.Should().Be(string.Format("Copyright (C) {0} {1}", DateTime.Now.Year, expectedCompany));
}

[Fact]
public void AutoBuild_with_AutoBuildMetadataAssembly_defined()
{
HelpText.AutoBuildMetadataAssembly = typeof(HelpText).Assembly;
string expectedHeading = "CommandLine 0.0.0";
string expectedCopyright = "Copyright (c) 2005 - 2020 Giacomo Stelluti Scala & Contributors";

ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof(Simple_Options)), new Error[0]);
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht => ht, ex => ex);

actualResult.Heading.Should().Be(expectedHeading);
actualResult.Copyright.Should().Be(expectedCopyright);
}

[Fact]
public void Add_line_with_two_empty_spaces_at_the_end()
{
Expand Down

0 comments on commit 853eff3

Please sign in to comment.