Skip to content

Commit 6a01d6c

Browse files
committed
Resolve the null EntryAssembly Issues #389 #424
1 parent 371b61a commit 6a01d6c

File tree

5 files changed

+95
-6
lines changed

5 files changed

+95
-6
lines changed

Directory.Build.props

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project>
2+
<PropertyGroup>
3+
<NoWarn>CS1591;8002;NU5125</NoWarn>
4+
</PropertyGroup>
5+
<PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
6+
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
7+
</PropertyGroup>
8+
</Project>

src/CommandLine/CommandLine.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<AssemblyName>CommandLine</AssemblyName>
55
<OutputType>Library</OutputType>
6-
<TargetFrameworks>netstandard2.0;net40;net45;net461;netcoreapp2.0</TargetFrameworks>
6+
<TargetFrameworks>netstandard2.0;net40;net45</TargetFrameworks>
77
<DefineConstants>$(DefineConstants);CSX_EITHER_INTERNAL;CSX_REM_EITHER_BEYOND_2;CSX_ENUM_INTERNAL;ERRH_INTERNAL;ERRH_DISABLE_INLINE_METHODS;CSX_MAYBE_INTERNAL;CSX_REM_EITHER_FUNC</DefineConstants>
88
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
@@ -23,6 +23,7 @@
2323
<PackageIconUrl>https://raw.githubusercontent.com/commandlineparser/commandline/master/art/CommandLine20.png</PackageIconUrl>
2424
<PackageTags>command line;commandline;argument;option;parser;parsing;library;syntax;shell</PackageTags>
2525
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
26+
<LangVersion>7.3</LangVersion>
2627
</PropertyGroup>
2728

2829
<ItemGroup Condition="'$(BuildTarget)' != 'fsharp'">

src/CommandLine/Infrastructure/ReflectionHelper.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ public static object CreateDefaultImmutableInstance(Type type, Type[] constructo
101101

102102
private static Assembly GetExecutingOrEntryAssembly()
103103
{
104-
return Assembly.GetEntryAssembly();
104+
//resolve issues of null EntryAssembly in Xunit Test #392,424,389
105+
//return Assembly.GetEntryAssembly();
106+
return Assembly.GetEntryAssembly()??Assembly.GetCallingAssembly();
105107
}
106108
}
107109
}

tests/CommandLine.Tests/CommandLine.Tests.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Library</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
66
<DefineConstants>$(DefineConstants);PLATFORM_DOTNET</DefineConstants>
77
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
88
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
@@ -13,7 +13,6 @@
1313
<Version Condition="'$(VersionSuffix)' == ''">2.5.0</Version>
1414
<Copyright>Copyright (c) 2005 - 2018 Giacomo Stelluti Scala &amp; Contributors</Copyright>
1515
</PropertyGroup>
16-
1716
<ItemGroup Condition="'$(BuildTarget)' != 'fsharp'">
1817
<Compile Remove="Fakes\Options_With_FSharpOption.cs" />
1918
<Compile Remove="Unit\Infrastructure\FSharpOptionHelperTests.cs" />
@@ -29,8 +28,9 @@
2928
<PackageReference Include="FSharp.Core" Version="4.5.1" Condition="'$(BuildTarget)' == 'fsharp'" />
3029
<PackageReference Include="xunit" Version="2.4.0" />
3130
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
32-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
31+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
32+
3333
</ItemGroup>
3434

35-
35+
3636
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using CommandLine.Text;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using Xunit;
7+
8+
namespace CommandLine.Tests.Unit
9+
{
10+
//Reference: PR# 392
11+
public class Issue389Tests
12+
{
13+
14+
private const int ERROR_SUCCESS = 0;
15+
16+
// Test method (xUnit) which fails
17+
[Fact]
18+
public void CallMain_GiveHelpArgument_ExpectSuccess()
19+
{
20+
var result = Program.__Main(new[] { "--help" });
21+
22+
Assert.Equal(ERROR_SUCCESS, result);
23+
}
24+
25+
// main program
26+
internal class Program
27+
{
28+
29+
30+
internal static int __Main(string[] args)
31+
{
32+
bool hasError = false;
33+
bool helpOrVersionRequested = false;
34+
35+
ParserResult<Options> parsedOptions = Parser.Default.ParseArguments<Options>(args)
36+
.WithNotParsed(errors => {
37+
helpOrVersionRequested = errors.Any(
38+
x => x.Tag == ErrorType.HelpRequestedError
39+
|| x.Tag == ErrorType.VersionRequestedError);
40+
hasError = true;
41+
});
42+
43+
if(helpOrVersionRequested)
44+
{
45+
return ERROR_SUCCESS;
46+
}
47+
48+
// Execute as a normal call
49+
// ...
50+
return ERROR_SUCCESS;
51+
}
52+
53+
}
54+
55+
// Options
56+
internal class Options
57+
{
58+
59+
[Option('c', "connectionString", Required = true, HelpText = "Texts.ExplainConnection")]
60+
public string ConnectionString { get; set; }
61+
62+
[Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")]
63+
public int JobId { get; set; }
64+
65+
[Usage(ApplicationAlias = "Importer.exe")]
66+
public static IEnumerable<Example> Examples
67+
{
68+
get => new[] {
69+
new Example("Texts.ExplainExampleExecution", new Options() {
70+
ConnectionString="Server=MyServer;Database=MyDatabase",
71+
JobId = 5
72+
}),
73+
};
74+
}
75+
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)