Created
February 20, 2020 10:29
-
-
Save adamsitnik/bc9beb4dd045638b2302565b02239389 to your computer and use it in GitHub Desktop.
repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using CommandLine; | |
using System; | |
using System.IO; | |
using Xunit; | |
namespace XUnitTestProject5 | |
{ | |
public class CommandLineOptions | |
{ | |
[Option('a', "aio", Required = false, Default = true, HelpText = "Use Linux AIO")] | |
public bool UseAio { get; set; } | |
} | |
public static class ConsoleLineArgumentsParser | |
{ | |
public static (bool isSuccess, CommandLineOptions commandLineOptions) ParseArguments(string[] args) | |
{ | |
bool isSuccess = false; | |
CommandLineOptions commandLineOptions = null; | |
using (var parser = CreateParser()) | |
{ | |
parser | |
.ParseArguments<CommandLineOptions>(args) | |
.WithParsed(options => { commandLineOptions = options; isSuccess = true; }) | |
.WithNotParsed(_ => isSuccess = false); | |
} | |
return (isSuccess, commandLineOptions); | |
} | |
private static Parser CreateParser() | |
=> new Parser(settings => | |
{ | |
settings.CaseInsensitiveEnumValues = true; | |
settings.CaseSensitive = false; | |
settings.EnableDashDash = true; | |
settings.HelpWriter = Console.Out; | |
settings.IgnoreUnknownArguments = true; // for args that we pass to Host.CreateDefaultBuilder() | |
settings.MaximumDisplayWidth = GetMaximumDisplayWidth(); | |
}); | |
private static int GetMaximumDisplayWidth() | |
{ | |
const int MinimumDisplayWidth = 80; | |
try | |
{ | |
return Math.Max(MinimumDisplayWidth, Console.WindowWidth); | |
} | |
catch (IOException) | |
{ | |
return MinimumDisplayWidth; | |
} | |
} | |
} | |
public class ConsoleLineArgumentsParserTests | |
{ | |
[Theory] | |
[InlineData(true, "-a", "true")] | |
[InlineData(true, "-a=true")] | |
[InlineData(true, "--aio", "true")] | |
[InlineData(false, "-a", "false")] | |
[InlineData(false, "-a", "False")] | |
[InlineData(false, "-a=false")] | |
[InlineData(false, "--aio", "false")] | |
[InlineData(false, "--aio", "False")] | |
public void AioCanBeParsed(bool expectedAio, params string[] args) | |
{ | |
(bool isSuccess, CommandLineOptions commandLineOptions) = ConsoleLineArgumentsParser.ParseArguments(args); | |
Assert.True(isSuccess); | |
Assert.Equal(expectedAio, commandLineOptions.UseAio); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks> | |
<IsPackable>false</IsPackable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="CommandLineParser" Version="2.7.82" /> | |
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" /> | |
<PackageReference Include="xunit" Version="2.4.0" /> | |
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" /> | |
<PackageReference Include="coverlet.collector" Version="1.0.1" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment