Skip to content

Commit

Permalink
Merge pull request #430 from moh-hassan/net40-net45
Browse files Browse the repository at this point in the history
add support for net40 and net45
  • Loading branch information
moh-hassan committed Apr 27, 2019
2 parents 61a2c41 + 8d89898 commit d59d6fc
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 57 deletions.
8 changes: 8 additions & 0 deletions Directory.Build.props
@@ -0,0 +1,8 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0219;8002;NU5125</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith('net4'))">
<DefineConstants>$(DefineConstants);NETFRAMEWORK</DefineConstants>
</PropertyGroup>
</Project>
50 changes: 4 additions & 46 deletions demo/ReadText.Demo/ReadText.Demo.csproj
@@ -1,52 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>12.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{F9D3B288-1A73-4C91-8ED7-11ED1704B817}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ReadText.Demo</RootNamespace>
<AssemblyName>ReadText.Demo</AssemblyName>
<TargetFrameworks>net40;net45;net461;netcoreapp2.1;netcoreapp2.0</TargetFrameworks>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommandLine, Version=2.0.275.0, Culture=neutral, PublicKeyToken=de6f01bd326f8c32, processorArchitecture=MSIL">
<HintPath>packages\CommandLineParser.2.1.1-beta\lib\net40\CommandLine.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\SharedAssemblyInfo.cs">
<Link>Properties\SharedAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Options.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config">
<SubType>Designer</SubType>
</None>
<ProjectReference Include="..\..\src\CommandLine\CommandLine.csproj" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
15 changes: 12 additions & 3 deletions demo/ReadText.Demo/ReadText.Demo.sln
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
# Visual Studio 15
VisualStudioVersion = 15.0.28307.106
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReadText.Demo", "ReadText.Demo.csproj", "{F9D3B288-1A73-4C91-8ED7-11ED1704B817}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReadText.Demo", "ReadText.Demo.csproj", "{F9D3B288-1A73-4C91-8ED7-11ED1704B817}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandLine", "..\..\src\CommandLine\CommandLine.csproj", "{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,8 +17,15 @@ Global
{F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9D3B288-1A73-4C91-8ED7-11ED1704B817}.Release|Any CPU.Build.0 = Release|Any CPU
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A03AADAC-F7E5-44A6-8BCC-492B1697CCC9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FF14CDF0-EF51-448B-918C-47CD369568DF}
EndGlobalSection
EndGlobal
4 changes: 3 additions & 1 deletion src/CommandLine/CommandLine.csproj
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<AssemblyName>CommandLine</AssemblyName>
<OutputType>Library</OutputType>
<TargetFrameworks>netstandard2.0; net461; netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net40;net45;net461</TargetFrameworks>
<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>
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand All @@ -23,6 +23,8 @@
<PackageProjectUrl>https://github.com/commandlineparser/commandline</PackageProjectUrl>
<PackageIconUrl>https://raw.githubusercontent.com/commandlineparser/commandline/master/art/CommandLine20.png</PackageIconUrl>
<PackageTags>command line;commandline;argument;option;parser;parsing;library;syntax;shell</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>7.3</LangVersion>
</PropertyGroup>

<ItemGroup Condition="'$(BuildTarget)' != 'fsharp'">
Expand Down
9 changes: 8 additions & 1 deletion src/CommandLine/Infrastructure/ReflectionHelper.cs
Expand Up @@ -52,7 +52,12 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()
}

var assembly = GetExecutingOrEntryAssembly();

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

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

private static Assembly GetExecutingOrEntryAssembly()
{
return Assembly.GetEntryAssembly();
//resolve issues of null EntryAssembly in Xunit Test #392,424,389
//return Assembly.GetEntryAssembly();
return Assembly.GetEntryAssembly()??Assembly.GetCallingAssembly();
}
}
}
20 changes: 20 additions & 0 deletions src/CommandLine/IntrospectionExtensions.cs
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandLine
{
#if NET40

internal static class IntrospectionExtensions
{
public static Type GetTypeInfo(this Type type)
{
return type;
}
}
#endif
}

21 changes: 15 additions & 6 deletions tests/CommandLine.Tests/CommandLine.Tests.csproj
Expand Up @@ -2,18 +2,24 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<DefineConstants>$(DefineConstants);PLATFORM_DOTNET</DefineConstants>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
<DefineConstants Condition="'$(BuildTarget)' != 'fsharp'">$(DefineConstants);SKIP_FSHARP</DefineConstants>
<AssemblyOriginatorKeyFile>..\..\CommandLine.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<Authors>gsscoder;nemec;ericnewton76</Authors>
<Title>Command Line Parser Library</Title>
<Version Condition="'$(VersionSuffix)' != ''">$(VersionSuffix)</Version>
<Version Condition="'$(VersionSuffix)' == ''">2.5.0</Version>
<Copyright>Copyright (c) 2005 - 2018 Giacomo Stelluti Scala &amp; Contributors</Copyright>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup Condition="'$(BuildTarget)' != 'fsharp'">
<Compile Remove="Fakes\Options_With_FSharpOption.cs" />
<Compile Remove="Unit\Infrastructure\FSharpOptionHelperTests.cs" />
</ItemGroup>

<PropertyGroup Condition="$(TargetFramework.StartsWith('netcoreapp'))">
<DefineConstants>$(DefineConstants);PLATFORM_DOTNET</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\CommandLine\CommandLine.csproj" />
</ItemGroup>
Expand All @@ -22,8 +28,11 @@
<PackageReference Include="FluentAssertions" Version="5.4.1" />
<PackageReference Include="FsCheck" Version="2.11.0" />
<PackageReference Include="FSharp.Core" Version="4.5.1" Condition="'$(BuildTarget)' == 'fsharp'" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />

</ItemGroup>


</Project>
78 changes: 78 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue389Tests.cs
@@ -0,0 +1,78 @@
using CommandLine.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace CommandLine.Tests.Unit
{
//Reference: PR# 392
public class Issue389Tests
{

private const int ERROR_SUCCESS = 0;

// Test method (xUnit) which fails
[Fact]
public void CallMain_GiveHelpArgument_ExpectSuccess()
{
var result = Program.__Main(new[] { "--help" });

Assert.Equal(ERROR_SUCCESS, result);
}

// main program
internal class Program
{


internal static int __Main(string[] args)
{
bool hasError = false;
bool helpOrVersionRequested = false;

ParserResult<Options> parsedOptions = Parser.Default.ParseArguments<Options>(args)
.WithNotParsed(errors => {
helpOrVersionRequested = errors.Any(
x => x.Tag == ErrorType.HelpRequestedError
|| x.Tag == ErrorType.VersionRequestedError);
hasError = true;
});

if(helpOrVersionRequested)
{
return ERROR_SUCCESS;
}

// Execute as a normal call
// ...
return ERROR_SUCCESS;
}

}

// Options
internal class Options
{

[Option('c', "connectionString", Required = true, HelpText = "Texts.ExplainConnection")]
public string ConnectionString { get; set; }

[Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")]
public int JobId { get; set; }

[Usage(ApplicationAlias = "Importer.exe")]
public static IEnumerable<Example> Examples
{
get => new[] {
new Example("Texts.ExplainExampleExecution", new Options() {
ConnectionString="Server=MyServer;Database=MyDatabase",
JobId = 5
}),
};
}

}
}
}
55 changes: 55 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue424Tests.cs
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using Xunit;

namespace CommandLine.Tests.Unit
{

//MailAndSmsWarningSenderTests
public class Issue424Tests
{
private MailAndSmsWarningSender _sut;

public Issue424Tests()
{
_sut = new MailAndSmsWarningSender();
}

[Fact]
public void SendSmsOnWarning()
{
//Arrange
void Action() => _sut.ParseArgumentsAndRun(
new[] { "--task", "MailAndSmsWarningSender", "--test", "hejtest" });
// Act & Assert
Assert.Throws<NotImplementedException>((Action)Action);
}
}

public class MailAndSmsWarningSender
{
internal class Options
{
[Option("task")]
public string Task { get; set; }
}

public void ParseArgumentsAndRun(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(ExecuteTaskWithOptions)
.WithNotParsed(HandleParseError);
}

private void HandleParseError(IEnumerable<Error> errs)
{
throw new NotImplementedException();
}

private void ExecuteTaskWithOptions(Options opts)
{
Console.WriteLine("Executing");
}

}
}

0 comments on commit d59d6fc

Please sign in to comment.