Skip to content

Commit d59d6fc

Browse files
authored
Merge pull request #430 from moh-hassan/net40-net45
add support for net40 and net45
2 parents 61a2c41 + 8d89898 commit d59d6fc

File tree

9 files changed

+203
-57
lines changed

9 files changed

+203
-57
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;CS0219;8002;NU5125</NoWarn>
4+
</PropertyGroup>
5+
<PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
6+
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
7+
</PropertyGroup>
8+
</Project>
+4-46
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,10 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProductVersion>12.0.0</ProductVersion>
7-
<SchemaVersion>2.0</SchemaVersion>
8-
<ProjectGuid>{F9D3B288-1A73-4C91-8ED7-11ED1704B817}</ProjectGuid>
93
<OutputType>Exe</OutputType>
10-
<RootNamespace>ReadText.Demo</RootNamespace>
11-
<AssemblyName>ReadText.Demo</AssemblyName>
4+
<TargetFrameworks>net40;net45;net461;netcoreapp2.1;netcoreapp2.0</TargetFrameworks>
5+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
126
</PropertyGroup>
13-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
14-
<DebugSymbols>true</DebugSymbols>
15-
<DebugType>full</DebugType>
16-
<Optimize>false</Optimize>
17-
<OutputPath>bin\Debug</OutputPath>
18-
<DefineConstants>DEBUG;</DefineConstants>
19-
<ErrorReport>prompt</ErrorReport>
20-
<WarningLevel>4</WarningLevel>
21-
<Externalconsole>true</Externalconsole>
22-
</PropertyGroup>
23-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
24-
<DebugType>full</DebugType>
25-
<Optimize>true</Optimize>
26-
<OutputPath>bin\Release</OutputPath>
27-
<ErrorReport>prompt</ErrorReport>
28-
<WarningLevel>4</WarningLevel>
29-
<Externalconsole>true</Externalconsole>
30-
</PropertyGroup>
31-
<ItemGroup>
32-
<Reference Include="CommandLine, Version=2.0.275.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
33-
<HintPath>packages\CommandLineParser.2.1.1-beta\lib\net40\CommandLine.dll</HintPath>
34-
</Reference>
35-
<Reference Include="System" />
36-
<Reference Include="System.Core" />
37-
</ItemGroup>
38-
<ItemGroup>
39-
<Compile Include="..\..\src\SharedAssemblyInfo.cs">
40-
<Link>Properties\SharedAssemblyInfo.cs</Link>
41-
</Compile>
42-
<Compile Include="Program.cs" />
43-
<Compile Include="Properties\AssemblyInfo.cs" />
44-
<Compile Include="Options.cs" />
45-
</ItemGroup>
467
<ItemGroup>
47-
<None Include="packages.config">
48-
<SubType>Designer</SubType>
49-
</None>
8+
<ProjectReference Include="..\..\src\CommandLine\CommandLine.csproj" />
509
</ItemGroup>
51-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
5210
</Project>

demo/ReadText.Demo/ReadText.Demo.sln

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.31101.0
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.106
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadText.Demo", "ReadText.Demo.csproj", "{F9D3B288-1A73-4C91-8ED7-11ED1704B817}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReadText.Demo", "ReadText.Demo.csproj", "{F9D3B288-1A73-4C91-8ED7-11ED1704B817}"
7+
EndProject
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandLine", "..\..\src\CommandLine\CommandLine.csproj", "{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}"
79
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,8 +17,15 @@ Global
1517
{F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE
2127
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {FF14CDF0-EF51-448B-918C-47CD369568DF}
30+
EndGlobalSection
2231
EndGlobal

src/CommandLine/CommandLine.csproj

+3-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; net461; netcoreapp2.0</TargetFrameworks>
6+
<TargetFrameworks>netstandard2.0;net40;net45;net461</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,8 @@
2323
<PackageProjectUrl>https://github.com/commandlineparser/commandline</PackageProjectUrl>
2424
<PackageIconUrl>https://raw.githubusercontent.com/commandlineparser/commandline/master/art/CommandLine20.png</PackageIconUrl>
2525
<PackageTags>command line;commandline;argument;option;parser;parsing;library;syntax;shell</PackageTags>
26+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
27+
<LangVersion>7.3</LangVersion>
2628
</PropertyGroup>
2729

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

src/CommandLine/Infrastructure/ReflectionHelper.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,12 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()
5252
}
5353

5454
var assembly = GetExecutingOrEntryAssembly();
55+
56+
#if NET40
57+
var attributes = assembly.GetCustomAttributes(typeof(TAttribute), false);
58+
#else
5559
var attributes = assembly.GetCustomAttributes<TAttribute>().ToArray();
60+
#endif
5661

5762
return attributes.Length > 0
5863
? Maybe.Just((TAttribute)attributes[0])
@@ -93,7 +98,9 @@ public static object CreateDefaultImmutableInstance(Type type, Type[] constructo
9398

9499
private static Assembly GetExecutingOrEntryAssembly()
95100
{
96-
return Assembly.GetEntryAssembly();
101+
//resolve issues of null EntryAssembly in Xunit Test #392,424,389
102+
//return Assembly.GetEntryAssembly();
103+
return Assembly.GetEntryAssembly()??Assembly.GetCallingAssembly();
97104
}
98105
}
99106
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CommandLine
8+
{
9+
#if NET40
10+
11+
internal static class IntrospectionExtensions
12+
{
13+
public static Type GetTypeInfo(this Type type)
14+
{
15+
return type;
16+
}
17+
}
18+
#endif
19+
}
20+

tests/CommandLine.Tests/CommandLine.Tests.csproj

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,24 @@
22

33
<PropertyGroup>
44
<OutputType>Library</OutputType>
5-
<TargetFramework>netcoreapp2.0</TargetFramework>
6-
<DefineConstants>$(DefineConstants);PLATFORM_DOTNET</DefineConstants>
5+
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
76
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
87
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
98
<SignAssembly>true</SignAssembly>
9+
<Authors>gsscoder;nemec;ericnewton76</Authors>
10+
<Title>Command Line Parser Library</Title>
11+
<Version Condition="'$(VersionSuffix)' != ''">$(VersionSuffix)</Version>
12+
<Version Condition="'$(VersionSuffix)' == ''">2.5.0</Version>
13+
<Copyright>Copyright (c) 2005 - 2018 Giacomo Stelluti Scala &amp; Contributors</Copyright>
14+
<IsTestProject>true</IsTestProject>
1015
</PropertyGroup>
11-
1216
<ItemGroup Condition="'$(BuildTarget)' != 'fsharp'">
1317
<Compile Remove="Fakes\Options_With_FSharpOption.cs" />
1418
<Compile Remove="Unit\Infrastructure\FSharpOptionHelperTests.cs" />
1519
</ItemGroup>
16-
20+
<PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp'))">
21+
<DefineConstants>$(DefineConstants);PLATFORM_DOTNET</DefineConstants>
22+
</PropertyGroup>
1723
<ItemGroup>
1824
<ProjectReference Include="..\..\src\CommandLine\CommandLine.csproj" />
1925
</ItemGroup>
@@ -22,8 +28,11 @@
2228
<PackageReference Include="FluentAssertions" Version="5.4.1" />
2329
<PackageReference Include="FsCheck" Version="2.11.0" />
2430
<PackageReference Include="FSharp.Core" Version="4.5.1" Condition="'$(BuildTarget)' == 'fsharp'" />
25-
<PackageReference Include="xunit" Version="2.4.0" />
26-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
31+
<PackageReference Include="xunit" Version="2.4.1" />
32+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
33+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
34+
2735
</ItemGroup>
2836

37+
2938
</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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
5+
namespace CommandLine.Tests.Unit
6+
{
7+
8+
//MailAndSmsWarningSenderTests
9+
public class Issue424Tests
10+
{
11+
private MailAndSmsWarningSender _sut;
12+
13+
public Issue424Tests()
14+
{
15+
_sut = new MailAndSmsWarningSender();
16+
}
17+
18+
[Fact]
19+
public void SendSmsOnWarning()
20+
{
21+
//Arrange
22+
void Action() => _sut.ParseArgumentsAndRun(
23+
new[] { "--task", "MailAndSmsWarningSender", "--test", "hejtest" });
24+
// Act & Assert
25+
Assert.Throws<NotImplementedException>((Action)Action);
26+
}
27+
}
28+
29+
public class MailAndSmsWarningSender
30+
{
31+
internal class Options
32+
{
33+
[Option("task")]
34+
public string Task { get; set; }
35+
}
36+
37+
public void ParseArgumentsAndRun(string[] args)
38+
{
39+
Parser.Default.ParseArguments<Options>(args)
40+
.WithParsed(ExecuteTaskWithOptions)
41+
.WithNotParsed(HandleParseError);
42+
}
43+
44+
private void HandleParseError(IEnumerable<Error> errs)
45+
{
46+
throw new NotImplementedException();
47+
}
48+
49+
private void ExecuteTaskWithOptions(Options opts)
50+
{
51+
Console.WriteLine("Executing");
52+
}
53+
54+
}
55+
}

0 commit comments

Comments
 (0)