Skip to content

Commit b82ab38

Browse files
committed
cherry pick 853eff3 from PR785
Make possible to explicitly define the assembly that will be used to read metadata (assembly attributes) to produce the help. commandlineparser#785
1 parent f4032de commit b82ab38

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

src/CommandLine/Infrastructure/ReflectionHelper.cs

+12-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ static class ReflectionHelper
1616
/// </summary>
1717
[ThreadStatic] private static IDictionary<Type, Attribute> _overrides;
1818

19+
private static Assembly _programAssembly;
20+
21+
public static Assembly ProgramAssembly
22+
{
23+
get => _programAssembly ?? GetExecutingOrEntryAssembly();
24+
set => _programAssembly = value;
25+
}
26+
1927
/// <summary>
2028
/// Assembly attribute overrides for testing.
2129
/// </summary>
@@ -51,12 +59,10 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()
5159
Maybe.Nothing<TAttribute>();
5260
}
5361

54-
var assembly = GetExecutingOrEntryAssembly();
55-
5662
#if NET40
57-
var attributes = assembly.GetCustomAttributes(typeof(TAttribute), false);
63+
var attributes = ProgramAssembly.GetCustomAttributes(typeof(TAttribute), false);
5864
#else
59-
var attributes = assembly.GetCustomAttributes<TAttribute>().ToArray();
65+
var attributes = ProgramAssembly.GetCustomAttributes<TAttribute>().ToArray();
6066
#endif
6167

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

6773
public static string GetAssemblyName()
6874
{
69-
var assembly = GetExecutingOrEntryAssembly();
70-
return assembly.GetName().Name;
75+
return ProgramAssembly.GetName().Name;
7176
}
7277

7378
public static string GetAssemblyVersion()
7479
{
75-
var assembly = GetExecutingOrEntryAssembly();
76-
return assembly.GetName().Version.ToStringInvariant();
80+
return ProgramAssembly.GetName().Version.ToStringInvariant();
7781
}
7882

7983
public static bool IsFSharpOptionType(Type type)

src/CommandLine/Text/HelpText.cs

+11
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,17 @@ public SentenceBuilder SentenceBuilder
305305
get { return sentenceBuilder; }
306306
}
307307

308+
/// <summary>
309+
/// Gets or sets the <see cref="Assembly"/> that will be used to look for meta data (assembly attributes) to auto build the help.
310+
/// By default the entry assembly is automatically selected, but this may not match what is really expected, in particular when
311+
/// running unit tests.
312+
/// </summary>
313+
public static Assembly AutoBuildMetadataAssembly
314+
{
315+
get => ReflectionHelper.ProgramAssembly;
316+
set => ReflectionHelper.ProgramAssembly = value;
317+
}
318+
308319
/// <summary>
309320
/// Creates a new instance of the <see cref="CommandLine.Text.HelpText"/> class using common defaults.
310321
/// </summary>

tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class HelpTextTests : IDisposable
2222
public void Dispose()
2323
{
2424
ReflectionHelper.SetAttributeOverride(null);
25+
HelpText.AutoBuildMetadataAssembly = null;
2526
}
2627

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

720+
[Fact]
721+
public void AutoBuild_with_AutoBuildMetadataAssembly_defined()
722+
{
723+
HelpText.AutoBuildMetadataAssembly = typeof(HelpText).Assembly;
724+
string expectedHeading = "CommandLine 0.0.0";
725+
string expectedCopyright = "Copyright (c) 2005 - 2020 Giacomo Stelluti Scala & Contributors";
726+
727+
ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
728+
TypeInfo.Create(typeof(Simple_Options)), new Error[0]);
729+
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht => ht, ex => ex);
730+
731+
actualResult.Heading.Should().Be(expectedHeading);
732+
actualResult.Copyright.Should().Be(expectedCopyright);
733+
}
734+
719735
[Fact]
720736
public void Add_line_with_two_empty_spaces_at_the_end()
721737
{

0 commit comments

Comments
 (0)