6 Commits
0.0.7 ... 0.0.8

Author SHA1 Message Date
Alexey Golub
70bfe0bf91 Update version 2019-11-13 20:34:11 +02:00
Alexey Golub
9690c380d3 Use C#8 features and cleanup 2019-11-13 20:31:48 +02:00
Alexey Golub
85caa275ae Add source link 2019-11-12 22:26:29 +02:00
Federico Paolillo
32026e59c0 Use Path.Separator in environment variables tests (#31) 2019-11-09 13:06:00 +02:00
Alexey Golub
486ccb9685 Update csproj 2019-11-08 13:21:53 +02:00
Alexey Golub
7b766f70f3 Use GitHub actions 2019-11-06 15:08:51 +02:00
71 changed files with 452 additions and 569 deletions

27
.github/workflows/CD.yml vendored Normal file
View File

@@ -0,0 +1,27 @@
name: CD
on:
push:
tags:
- '*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.0.100
- name: Pack
run: dotnet pack CliFx --configuration Release
- name: Deploy
run: |
dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{secrets.NUGET_TOKEN}}
dotnet nuget push CliFx/bin/Release/*.snupkg -s nuget.org -k ${{secrets.NUGET_TOKEN}}

22
.github/workflows/CI.yml vendored Normal file
View File

@@ -0,0 +1,22 @@
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Install .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.0.100
- name: Build & test
run: dotnet test --configuration Release
- name: Coverage
run: curl -s https://codecov.io/bash | bash -s -- -f CliFx.Tests/bin/Release/Coverage.xml -t ${{secrets.CODECOV_TOKEN}} -Z

View File

@@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -9,7 +8,7 @@ namespace CliFx.Benchmarks.Commands
public class CliFxCommand : ICommand
{
[CommandOption("str", 's')]
public string StrOption { get; set; }
public string? StrOption { get; set; }
[CommandOption("int", 'i')]
public int IntOption { get; set; }

View File

@@ -5,7 +5,7 @@ namespace CliFx.Benchmarks.Commands
public class CliprCommand
{
[NamedArgument('s', "str")]
public string StrOption { get; set; }
public string? StrOption { get; set; }
[NamedArgument('i', "int")]
public int IntOption { get; set; }

View File

@@ -5,7 +5,7 @@ namespace CliFx.Benchmarks.Commands
public class CommandLineParserCommand
{
[Option('s', "str")]
public string StrOption { get; set; }
public string? StrOption { get; set; }
[Option('i', "int")]
public int IntOption { get; set; }

View File

@@ -5,7 +5,7 @@ namespace CliFx.Benchmarks.Commands
public class McMasterCommand
{
[Option("--str|-s")]
public string StrOption { get; set; }
public string? StrOption { get; set; }
[Option("--int|-i")]
public int IntOption { get; set; }

View File

@@ -5,7 +5,7 @@ namespace CliFx.Benchmarks.Commands
public class PowerArgsCommand
{
[ArgShortcut("--str"), ArgShortcut("-s")]
public string StrOption { get; set; }
public string? StrOption { get; set; }
[ArgShortcut("--int"), ArgShortcut("-i")]
public int IntOption { get; set; }

View File

@@ -14,7 +14,7 @@ namespace CliFx.Benchmarks.Commands
{
new Option(new[] {"--str", "-s"})
{
Argument = new Argument<string>()
Argument = new Argument<string?>()
},
new Option(new[] {"--int", "-i"})
{

View File

@@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>

View File

@@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Demo.Internal;
@@ -25,7 +24,7 @@ namespace CliFx.Demo.Commands
public DateTimeOffset Published { get; set; }
[CommandOption("isbn", 'n', Description = "Book ISBN.")]
public Isbn Isbn { get; set; }
public Isbn? Isbn { get; set; }
public BookAddCommand(LibraryService libraryService)
{

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Demo.Internal;
using CliFx.Demo.Services;

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Demo.Internal;
using CliFx.Demo.Services;

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Demo.Services;
using CliFx.Exceptions;

View File

@@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using CliFx.Demo.Commands;
using CliFx.Demo.Services;
using Microsoft.Extensions.DependencyInjection;
@@ -7,7 +8,7 @@ namespace CliFx.Demo
{
public static class Program
{
public static Task<int> Main(string[] args)
private static IServiceProvider ConfigureServices()
{
// We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands
var services = new ServiceCollection();
@@ -21,7 +22,12 @@ namespace CliFx.Demo
services.AddTransient<BookRemoveCommand>();
services.AddTransient<BookListCommand>();
var serviceProvider = services.BuildServiceProvider();
return services.BuildServiceProvider();
}
public static Task<int> Main(string[] args)
{
var serviceProvider = ConfigureServices();
return new CliApplicationBuilder()
.AddCommandsFromThisAssembly()

View File

@@ -21,8 +21,8 @@ namespace CliFx.Tests
builder
.AddCommand(typeof(HelloWorldDefaultCommand))
.AddCommandsFrom(typeof(HelloWorldDefaultCommand).Assembly)
.AddCommands(new[] { typeof(HelloWorldDefaultCommand) })
.AddCommandsFrom(new[] { typeof(HelloWorldDefaultCommand).Assembly })
.AddCommands(new[] {typeof(HelloWorldDefaultCommand)})
.AddCommandsFrom(new[] {typeof(HelloWorldDefaultCommand).Assembly})
.AddCommandsFromThisAssembly()
.AllowDebugMode()
.AllowPreviewMode()
@@ -31,7 +31,7 @@ namespace CliFx.Tests
.UseVersionText("test")
.UseDescription("test")
.UseConsole(new VirtualConsole(TextWriter.Null))
.UseCommandFactory(schema => (ICommand)Activator.CreateInstance(schema.Type))
.UseCommandFactory(schema => (ICommand) Activator.CreateInstance(schema.Type!)!)
.UseCommandOptionInputConverter(new CommandOptionInputConverter())
.UseEnvironmentVariablesProvider(new EnvironmentVariablesProviderStub())
.Build();

View File

@@ -19,104 +19,104 @@ namespace CliFx.Tests
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
{
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] {typeof(HelloWorldDefaultCommand)},
new string[0],
"Hello world."
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "-i", "foo", "-i", "bar", "-s", " " },
new[] {typeof(ConcatCommand)},
new[] {"concat", "-i", "foo", "-i", "bar", "-s", " "},
"foo bar"
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "-i", "one", "two", "three", "-s", ", " },
new[] {typeof(ConcatCommand)},
new[] {"concat", "-i", "one", "two", "three", "-s", ", "},
"one, two, three"
);
yield return new TestCaseData(
new[] { typeof(DivideCommand) },
new[] { "div", "-D", "24", "-d", "8" },
new[] {typeof(DivideCommand)},
new[] {"div", "-D", "24", "-d", "8"},
"3"
);
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] { "--version" },
new[] {typeof(HelloWorldDefaultCommand)},
new[] {"--version"},
TestVersionText
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "--version" },
new[] {typeof(ConcatCommand)},
new[] {"--version"},
TestVersionText
);
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] { "-h" },
new[] {typeof(HelloWorldDefaultCommand)},
new[] {"-h"},
null
);
yield return new TestCaseData(
new[] { typeof(HelloWorldDefaultCommand) },
new[] { "--help" },
new[] {typeof(HelloWorldDefaultCommand)},
new[] {"--help"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] {typeof(ConcatCommand)},
new string[0],
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "-h" },
new[] {typeof(ConcatCommand)},
new[] {"-h"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "--help" },
new[] {typeof(ConcatCommand)},
new[] {"--help"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "-h" },
new[] {typeof(ConcatCommand)},
new[] {"concat", "-h"},
null
);
yield return new TestCaseData(
new[] { typeof(ExceptionCommand) },
new[] { "exc", "-h" },
new[] {typeof(ExceptionCommand)},
new[] {"exc", "-h"},
null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc", "-h" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-h"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "[preview]" },
new[] {typeof(ConcatCommand)},
new[] {"[preview]"},
null
);
yield return new TestCaseData(
new[] { typeof(ExceptionCommand) },
new[] { "exc", "[preview]" },
new[] {typeof(ExceptionCommand)},
new[] {"exc", "[preview]"},
null
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "concat", "[preview]", "-o", "value" },
new[] {typeof(ConcatCommand)},
new[] {"concat", "[preview]", "-o", "value"},
null
);
}
@@ -130,38 +130,38 @@ namespace CliFx.Tests
);
yield return new TestCaseData(
new[] { typeof(ConcatCommand) },
new[] { "non-existing" },
new[] {typeof(ConcatCommand)},
new[] {"non-existing"},
null, null
);
yield return new TestCaseData(
new[] { typeof(ExceptionCommand) },
new[] { "exc" },
new[] {typeof(ExceptionCommand)},
new[] {"exc"},
null, null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc"},
null, null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc"},
null, null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc", "-m", "foo bar" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-m", "foo bar"},
"foo bar", null
);
yield return new TestCaseData(
new[] { typeof(CommandExceptionCommand) },
new[] { "exc", "-m", "foo bar", "-c", "666" },
new[] {typeof(CommandExceptionCommand)},
new[] {"exc", "-m", "foo bar", "-c", "666"},
"foo bar", 666
);
}
@@ -169,95 +169,92 @@ namespace CliFx.Tests
[Test]
[TestCaseSource(nameof(GetTestCases_RunAsync))]
public async Task RunAsync_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
string expectedStdOut = null)
string? expectedStdOut = null)
{
// Arrange
using (var stdoutStream = new StringWriter())
{
var console = new VirtualConsole(stdoutStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
await using var stdoutStream = new StringWriter();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.Build();
var console = new VirtualConsole(stdoutStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stdOut = stdoutStream.ToString().Trim();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseConsole(console)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.Build();
// Assert
exitCode.Should().Be(0);
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stdOut = stdoutStream.ToString().Trim();
if (expectedStdOut != null)
stdOut.Should().Be(expectedStdOut);
else
stdOut.Should().NotBeNullOrWhiteSpace();
}
// Assert
exitCode.Should().Be(0);
if (expectedStdOut != null)
stdOut.Should().Be(expectedStdOut);
else
stdOut.Should().NotBeNullOrWhiteSpace();
}
[Test]
[TestCaseSource(nameof(GetTestCases_RunAsync_Negative))]
public async Task RunAsync_Negative_Test(IReadOnlyList<Type> commandTypes, IReadOnlyList<string> commandLineArguments,
string expectedStdErr = null, int? expectedExitCode = null)
string? expectedStdErr = null, int? expectedExitCode = null)
{
// Arrange
using (var stderrStream = new StringWriter())
{
var console = new VirtualConsole(TextWriter.Null, stderrStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
await using var stderrStream = new StringWriter();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.UseConsole(console)
.Build();
var console = new VirtualConsole(TextWriter.Null, stderrStream);
var environmentVariablesProvider = new EnvironmentVariablesProviderStub();
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stderr = stderrStream.ToString().Trim();
var application = new CliApplicationBuilder()
.AddCommands(commandTypes)
.UseVersionText(TestVersionText)
.UseEnvironmentVariablesProvider(environmentVariablesProvider)
.UseConsole(console)
.Build();
// Assert
if (expectedExitCode != null)
exitCode.Should().Be(expectedExitCode);
else
exitCode.Should().NotBe(0);
// Act
var exitCode = await application.RunAsync(commandLineArguments);
var stderr = stderrStream.ToString().Trim();
if (expectedStdErr != null)
stderr.Should().Be(expectedStdErr);
else
stderr.Should().NotBeNullOrWhiteSpace();
}
// Assert
if (expectedExitCode != null)
exitCode.Should().Be(expectedExitCode);
else
exitCode.Should().NotBe(0);
if (expectedStdErr != null)
stderr.Should().Be(expectedStdErr);
else
stderr.Should().NotBeNullOrWhiteSpace();
}
[Test]
public async Task RunAsync_Cancellation_Test()
{
// Arrange
using (var stdoutStream = new StringWriter())
using (var cancellationTokenSource = new CancellationTokenSource())
{
var console = new VirtualConsole(stdoutStream, cancellationTokenSource.Token);
var application = new CliApplicationBuilder()
.AddCommand(typeof(CancellableCommand))
.UseConsole(console)
.Build();
var args = new[] { "cancel" };
using var cancellationTokenSource = new CancellationTokenSource();
await using var stdoutStream = new StringWriter();
// Act
var runTask = application.RunAsync(args);
cancellationTokenSource.Cancel();
var exitCode = await runTask.ConfigureAwait(false);
var stdOut = stdoutStream.ToString().Trim();
var console = new VirtualConsole(stdoutStream, cancellationTokenSource.Token);
// Assert
exitCode.Should().Be(-2146233029);
stdOut.Should().Be("Printed");
}
var application = new CliApplicationBuilder()
.AddCommand(typeof(CancellableCommand))
.UseConsole(console)
.Build();
var args = new[] {"cancel"};
// Act
var runTask = application.RunAsync(args);
cancellationTokenSource.Cancel();
var exitCode = await runTask.ConfigureAwait(false);
var stdOut = stdoutStream.ToString().Trim();
// Assert
exitCode.Should().Be(-2146233029);
stdOut.Should().Be("Printed");
}
}
}

View File

@@ -1,23 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<CollectCoverage>true</CollectCoverage>
<CoverletOutputFormat>opencover</CoverletOutputFormat>
<CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="FluentAssertions" Version="5.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.14.0" />
<PackageReference Include="coverlet.msbuild" Version="2.6.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="coverlet.msbuild" Version="2.7.0" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>

View File

@@ -8,6 +8,7 @@ using CliFx.Models;
using CliFx.Services;
using CliFx.Tests.TestCommands;
using CliFx.Tests.Stubs;
using System.IO;
namespace CliFx.Tests.Services
{
@@ -106,7 +107,7 @@ namespace CliFx.Tests.Services
new EnvironmentVariableWithoutCollectionPropertyCommand(),
GetCommandSchema(typeof(EnvironmentVariableWithoutCollectionPropertyCommand)),
new CommandInput(null, new CommandOptionInput[0], EnvironmentVariablesProviderStub.EnvironmentVariables),
new EnvironmentVariableWithoutCollectionPropertyCommand { Option = "A;B;C;" }
new EnvironmentVariableWithoutCollectionPropertyCommand { Option = $"A{Path.PathSeparator}B{Path.PathSeparator}C{Path.PathSeparator}" }
);
}

View File

@@ -18,7 +18,7 @@ namespace CliFx.Tests.Services
private static IEnumerable<TestCaseData> GetTestCases_CreateCommand()
{
yield return new TestCaseData(
new Func<CommandSchema, ICommand>(schema => (ICommand) Activator.CreateInstance(schema.Type)),
new Func<CommandSchema, ICommand>(schema => (ICommand) Activator.CreateInstance(schema.Type!)!),
GetCommandSchema(typeof(HelloWorldDefaultCommand))
);
}

View File

@@ -93,17 +93,16 @@ namespace CliFx.Tests.Services
IReadOnlyList<string> expectedSubstrings)
{
// Arrange
using (var stdout = new StringWriter())
{
var console = new VirtualConsole(stdout);
var renderer = new HelpTextRenderer();
using var stdout = new StringWriter();
// Act
renderer.RenderHelpText(console, source);
var console = new VirtualConsole(stdout);
var renderer = new HelpTextRenderer();
// Assert
stdout.ToString().Should().ContainAll(expectedSubstrings);
}
// Act
renderer.RenderHelpText(console, source);
// Assert
stdout.ToString().Should().ContainAll(expectedSubstrings);
}
}
}

View File

@@ -14,30 +14,29 @@ namespace CliFx.Tests.Services
public void All_Smoke_Test()
{
// Arrange
using (var stdin = new StringReader("hello world"))
using (var stdout = new StringWriter())
using (var stderr = new StringWriter())
{
var console = new VirtualConsole(stdin, stdout, stderr);
using var stdin = new StringReader("hello world");
using var stdout = new StringWriter();
using var stderr = new StringWriter();
// Act
console.ResetColor();
console.ForegroundColor = ConsoleColor.DarkMagenta;
console.BackgroundColor = ConsoleColor.DarkMagenta;
var console = new VirtualConsole(stdin, stdout, stderr);
// Assert
console.Input.Should().BeSameAs(stdin);
console.Input.Should().NotBeSameAs(Console.In);
console.IsInputRedirected.Should().BeTrue();
console.Output.Should().BeSameAs(stdout);
console.Output.Should().NotBeSameAs(Console.Out);
console.IsOutputRedirected.Should().BeTrue();
console.Error.Should().BeSameAs(stderr);
console.Error.Should().NotBeSameAs(Console.Error);
console.IsErrorRedirected.Should().BeTrue();
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
}
// Act
console.ResetColor();
console.ForegroundColor = ConsoleColor.DarkMagenta;
console.BackgroundColor = ConsoleColor.DarkMagenta;
// Assert
console.Input.Should().BeSameAs(stdin);
console.Input.Should().NotBeSameAs(Console.In);
console.IsInputRedirected.Should().BeTrue();
console.Output.Should().BeSameAs(stdout);
console.Output.Should().NotBeSameAs(Console.Out);
console.IsOutputRedirected.Should().BeTrue();
console.Error.Should().BeSameAs(stderr);
console.Error.Should().NotBeSameAs(Console.Error);
console.IsErrorRedirected.Should().BeTrue();
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
}
}
}

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Services;
@@ -13,7 +12,7 @@ namespace CliFx.Tests.TestCommands
public int ExitCode { get; set; } = 1337;
[CommandOption("msg", 'm')]
public string Message { get; set; }
public string? Message { get; set; }
public Task ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
}

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -9,10 +8,10 @@ namespace CliFx.Tests.TestCommands
public class DuplicateOptionNamesCommand : ICommand
{
[CommandOption("fruits")]
public string Apples { get; set; }
public string? Apples { get; set; }
[CommandOption("fruits")]
public string Oranges { get; set; }
public string? Oranges { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -9,10 +8,10 @@ namespace CliFx.Tests.TestCommands
public class DuplicateOptionShortNamesCommand : ICommand
{
[CommandOption('f')]
public string Apples { get; set; }
public string? Apples { get; set; }
[CommandOption('f')]
public string Oranges { get; set; }
public string? Oranges { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -9,7 +8,7 @@ namespace CliFx.Tests.TestCommands
public class EnvironmentVariableCommand : ICommand
{
[CommandOption("opt", EnvironmentVariableName = "ENV_SINGLE_VALUE")]
public string Option { get; set; }
public string? Option { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -10,7 +9,7 @@ namespace CliFx.Tests.TestCommands
public class EnvironmentVariableWithMultipleValuesCommand : ICommand
{
[CommandOption("opt", EnvironmentVariableName = "ENV_MULTIPLE_VALUES")]
public IEnumerable<string> Option { get; set; }
public IEnumerable<string>? Option { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -10,7 +8,7 @@ namespace CliFx.Tests.TestCommands
public class EnvironmentVariableWithoutCollectionPropertyCommand : ICommand
{
[CommandOption("opt", EnvironmentVariableName = "ENV_MULTIPLE_VALUES")]
public string Option { get; set; }
public string? Option { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,5 +1,4 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -10,7 +9,7 @@ namespace CliFx.Tests.TestCommands
public class ExceptionCommand : ICommand
{
[CommandOption("msg", 'm')]
public string Message { get; set; }
public string? Message { get; set; }
public Task ExecuteAsync(IConsole console) => throw new Exception(Message);
}

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -9,10 +8,10 @@ namespace CliFx.Tests.TestCommands
public class HelpDefaultCommand : ICommand
{
[CommandOption("option-a", 'a', Description = "OptionA description.")]
public string OptionA { get; set; }
public string? OptionA { get; set; }
[CommandOption("option-b", 'b', Description = "OptionB description.")]
public string OptionB { get; set; }
public string? OptionB { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -9,10 +8,10 @@ namespace CliFx.Tests.TestCommands
public class HelpNamedCommand : ICommand
{
[CommandOption("option-c", 'c', Description = "OptionC description.")]
public string OptionC { get; set; }
public string? OptionC { get; set; }
[CommandOption("option-d", 'd', Description = "OptionD description.")]
public string OptionD { get; set; }
public string? OptionD { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Services;
@@ -9,7 +8,7 @@ namespace CliFx.Tests.TestCommands
public class HelpSubCommand : ICommand
{
[CommandOption("option-e", 'e', Description = "OptionE description.")]
public string OptionE { get; set; }
public string? OptionE { get; set; }
public Task ExecuteAsync(IConsole console) => Task.CompletedTask;
}

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Services;
namespace CliFx.Tests.TestCommands

View File

@@ -17,41 +17,39 @@ namespace CliFx.Tests.Utilities
// Arrange
var formatProvider = CultureInfo.InvariantCulture;
using (var stdout = new StringWriter(formatProvider))
{
var console = new VirtualConsole(TextReader.Null, false, stdout, false, TextWriter.Null, false);
var ticker = console.CreateProgressTicker();
using var stdout = new StringWriter(formatProvider);
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
var progressStringValues = progressValues.Select(p => p.ToString("P2", formatProvider)).ToArray();
var console = new VirtualConsole(TextReader.Null, false, stdout, false, TextWriter.Null, false);
var ticker = console.CreateProgressTicker();
// Act
foreach (var progress in progressValues)
ticker.Report(progress);
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
var progressStringValues = progressValues.Select(p => p.ToString("P2", formatProvider)).ToArray();
// Assert
stdout.ToString().Should().ContainAll(progressStringValues);
}
// Act
foreach (var progress in progressValues)
ticker.Report(progress);
// Assert
stdout.ToString().Should().ContainAll(progressStringValues);
}
[Test]
public void Report_Redirected_Test()
{
// Arrange
using (var stdout = new StringWriter())
{
var console = new VirtualConsole(stdout);
var ticker = console.CreateProgressTicker();
using var stdout = new StringWriter();
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
var console = new VirtualConsole(stdout);
var ticker = console.CreateProgressTicker();
// Act
foreach (var progress in progressValues)
ticker.Report(progress);
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
// Assert
stdout.ToString().Should().BeEmpty();
}
// Act
foreach (var progress in progressValues)
ticker.Report(progress);
// Assert
stdout.ToString().Should().BeEmpty();
}
}
}

View File

@@ -10,27 +10,27 @@ namespace CliFx.Attributes
{
/// <summary>
/// Command name.
/// This can be null if this is the default command.
/// </summary>
public string Name { get; }
public string? Name { get; }
/// <summary>
/// Command description, which is used in help text.
/// </summary>
public string Description { get; set; }
public string? Description { get; set; }
/// <summary>
/// Initializes an instance of <see cref="CommandAttribute"/>.
/// </summary>
public CommandAttribute(string name)
{
Name = name; // can be null
Name = name;
}
/// <summary>
/// Initializes an instance of <see cref="CommandAttribute"/>.
/// </summary>
public CommandAttribute()
: this(null)
{
}
}

View File

@@ -10,11 +10,13 @@ namespace CliFx.Attributes
{
/// <summary>
/// Option name.
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
/// </summary>
public string Name { get; }
public string? Name { get; }
/// <summary>
/// Option short name.
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
/// </summary>
public char? ShortName { get; }
@@ -26,27 +28,27 @@ namespace CliFx.Attributes
/// <summary>
/// Option description, which is used in help text.
/// </summary>
public string Description { get; set; }
public string? Description { get; set; }
/// <summary>
/// Optional environment variable name that will be used as fallback value if no option value is specified.
/// </summary>
public string EnvironmentVariableName { get; set; }
public string? EnvironmentVariableName { get; set; }
/// <summary>
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
/// </summary>
public CommandOptionAttribute(string name, char? shortName)
private CommandOptionAttribute(string? name, char? shortName)
{
Name = name; // can be null
ShortName = shortName; // can be null
Name = name;
ShortName = shortName;
}
/// <summary>
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
/// </summary>
public CommandOptionAttribute(string name, char shortName)
: this(name, (char?)shortName)
: this(name, (char?) shortName)
{
}
@@ -62,7 +64,7 @@ namespace CliFx.Attributes
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
/// </summary>
public CommandOptionAttribute(char shortName)
: this(null, shortName)
: this(null, (char?) shortName)
{
}
}

View File

@@ -32,15 +32,15 @@ namespace CliFx
IConsole console, ICommandInputParser commandInputParser, ICommandSchemaResolver commandSchemaResolver,
ICommandFactory commandFactory, ICommandInitializer commandInitializer, IHelpTextRenderer helpTextRenderer)
{
_metadata = metadata.GuardNotNull(nameof(metadata));
_configuration = configuration.GuardNotNull(nameof(configuration));
_metadata = metadata;
_configuration = configuration;
_console = console.GuardNotNull(nameof(console));
_commandInputParser = commandInputParser.GuardNotNull(nameof(commandInputParser));
_commandSchemaResolver = commandSchemaResolver.GuardNotNull(nameof(commandSchemaResolver));
_commandFactory = commandFactory.GuardNotNull(nameof(commandFactory));
_commandInitializer = commandInitializer.GuardNotNull(nameof(commandInitializer));
_helpTextRenderer = helpTextRenderer.GuardNotNull(nameof(helpTextRenderer));
_console = console;
_commandInputParser = commandInputParser;
_commandSchemaResolver = commandSchemaResolver;
_commandFactory = commandFactory;
_commandInitializer = commandInitializer;
_helpTextRenderer = helpTextRenderer;
}
private async Task<int?> HandleDebugDirectiveAsync(CommandInput commandInput)
@@ -117,7 +117,7 @@ namespace CliFx
}
private int? HandleHelpOption(CommandInput commandInput,
IReadOnlyList<CommandSchema> availableCommandSchemas, CommandSchema targetCommandSchema)
IReadOnlyList<CommandSchema> availableCommandSchemas, CommandSchema? targetCommandSchema)
{
// Help should be rendered if it was requested, or when executing a command which isn't defined
var shouldRenderHelp = commandInput.IsHelpOptionSpecified() || targetCommandSchema == null;
@@ -180,8 +180,6 @@ namespace CliFx
/// <inheritdoc />
public async Task<int> RunAsync(IReadOnlyList<string> commandLineArguments)
{
commandLineArguments.GuardNotNull(nameof(commandLineArguments));
try
{
// Parse command input from arguments
@@ -199,7 +197,7 @@ namespace CliFx
HandlePreviewDirective(commandInput) ??
HandleVersionOption(commandInput) ??
HandleHelpOption(commandInput, availableCommandSchemas, targetCommandSchema) ??
await HandleCommandExecutionAsync(commandInput, targetCommandSchema);
await HandleCommandExecutionAsync(commandInput, targetCommandSchema!);
}
catch (Exception ex)
{
@@ -207,7 +205,7 @@ namespace CliFx
// Doing this also gets rid of the annoying Windows troubleshooting dialog that shows up on unhandled exceptions.
// Prefer showing message without stack trace on exceptions coming from CliFx or on CommandException
if (!ex.Message.IsNullOrWhiteSpace() && (ex is CliFxException || ex is CommandException))
if (!string.IsNullOrWhiteSpace(ex.Message) && (ex is CliFxException || ex is CommandException))
{
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(ex.Message));
}

View File

@@ -19,20 +19,18 @@ namespace CliFx
private bool _isDebugModeAllowed = true;
private bool _isPreviewModeAllowed = true;
private string _title;
private string _executableName;
private string _versionText;
private string _description;
private IConsole _console;
private ICommandFactory _commandFactory;
private ICommandOptionInputConverter _commandOptionInputConverter;
private IEnvironmentVariablesProvider _environmentVariablesProvider;
private string? _title;
private string? _executableName;
private string? _versionText;
private string? _description;
private IConsole? _console;
private ICommandFactory? _commandFactory;
private ICommandOptionInputConverter? _commandOptionInputConverter;
private IEnvironmentVariablesProvider? _environmentVariablesProvider;
/// <inheritdoc />
public ICliApplicationBuilder AddCommand(Type commandType)
{
commandType.GuardNotNull(nameof(commandType));
_commandTypes.Add(commandType);
return this;
@@ -41,8 +39,6 @@ namespace CliFx
/// <inheritdoc />
public ICliApplicationBuilder AddCommandsFrom(Assembly commandAssembly)
{
commandAssembly.GuardNotNull(nameof(commandAssembly));
var commandTypes = commandAssembly.ExportedTypes
.Where(t => t.Implements(typeof(ICommand)))
.Where(t => t.IsDefined(typeof(CommandAttribute)))
@@ -71,56 +67,56 @@ namespace CliFx
/// <inheritdoc />
public ICliApplicationBuilder UseTitle(string title)
{
_title = title.GuardNotNull(nameof(title));
_title = title;
return this;
}
/// <inheritdoc />
public ICliApplicationBuilder UseExecutableName(string executableName)
{
_executableName = executableName.GuardNotNull(nameof(executableName));
_executableName = executableName;
return this;
}
/// <inheritdoc />
public ICliApplicationBuilder UseVersionText(string versionText)
{
_versionText = versionText.GuardNotNull(nameof(versionText));
_versionText = versionText;
return this;
}
/// <inheritdoc />
public ICliApplicationBuilder UseDescription(string description)
public ICliApplicationBuilder UseDescription(string? description)
{
_description = description; // can be null
_description = description;
return this;
}
/// <inheritdoc />
public ICliApplicationBuilder UseConsole(IConsole console)
{
_console = console.GuardNotNull(nameof(console));
_console = console;
return this;
}
/// <inheritdoc />
public ICliApplicationBuilder UseCommandFactory(ICommandFactory factory)
{
_commandFactory = factory.GuardNotNull(nameof(factory));
_commandFactory = factory;
return this;
}
/// <inheritdoc />
public ICliApplicationBuilder UseCommandOptionInputConverter(ICommandOptionInputConverter converter)
{
_commandOptionInputConverter = converter.GuardNotNull(nameof(converter));
_commandOptionInputConverter = converter;
return this;
}
/// <inheritdoc />
public ICliApplicationBuilder UseEnvironmentVariablesProvider(IEnvironmentVariablesProvider environmentVariablesProvider)
{
_environmentVariablesProvider = environmentVariablesProvider.GuardNotNull(nameof(environmentVariablesProvider));
_environmentVariablesProvider = environmentVariablesProvider;
return this;
}
@@ -128,13 +124,13 @@ namespace CliFx
public ICliApplication Build()
{
// Use defaults for required parameters that were not configured
_title = _title ?? GetDefaultTitle() ?? "App";
_executableName = _executableName ?? GetDefaultExecutableName() ?? "app";
_versionText = _versionText ?? GetDefaultVersionText() ?? "v1.0";
_console = _console ?? new SystemConsole();
_commandFactory = _commandFactory ?? new CommandFactory();
_commandOptionInputConverter = _commandOptionInputConverter ?? new CommandOptionInputConverter();
_environmentVariablesProvider = _environmentVariablesProvider ?? new EnvironmentVariablesProvider();
_title ??= GetDefaultTitle() ?? "App";
_executableName ??= GetDefaultExecutableName() ?? "app";
_versionText ??= GetDefaultVersionText() ?? "v1.0";
_console ??= new SystemConsole();
_commandFactory ??= new CommandFactory();
_commandOptionInputConverter ??= new CommandOptionInputConverter();
_environmentVariablesProvider ??= new EnvironmentVariablesProvider();
// Project parameters to expected types
var metadata = new ApplicationMetadata(_title, _executableName, _versionText, _description);
@@ -153,7 +149,7 @@ namespace CliFx
// Entry assembly is null in tests
private static Assembly EntryAssembly => LazyEntryAssembly.Value;
private static string GetDefaultTitle() => EntryAssembly?.GetName().Name;
private static string GetDefaultTitle() => EntryAssembly?.GetName().Name ?? "";
private static string GetDefaultExecutableName()
{
@@ -169,6 +165,6 @@ namespace CliFx
return Path.GetFileNameWithoutExtension(entryAssemblyLocation);
}
private static string GetDefaultVersionText() => EntryAssembly != null ? $"v{EntryAssembly.GetName().Version}" : null;
private static string GetDefaultVersionText() => EntryAssembly != null ? $"v{EntryAssembly.GetName().Version}" : "";
}
}

View File

@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
<Version>0.0.7</Version>
<TargetFrameworks>net45;netstandard2.0;netstandard2.1</TargetFrameworks>
<Version>0.0.8</Version>
<Company>Tyrrrz</Company>
<Authors>$(Company)</Authors>
<Copyright>Copyright (C) Alexey Golub</Copyright>
@@ -12,13 +12,23 @@
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
<PackageIcon>favicon.png</PackageIcon>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<RepositoryUrl>https://github.com/Tyrrrz/CliFx</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<DocumentationFile>bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).xml</DocumentationFile>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<PublishRepositoryUrl>True</PublishRepositoryUrl>
<EmbedUntrackedSources>True</EmbedUntrackedSources>
<IncludeSymbols>True</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0-preview.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta2-19554-01" PrivateAssets="all" />
<PackageReference Include="Nullable" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<None Include="../favicon.png" Pack="True" PackagePath="" />
</ItemGroup>

View File

@@ -10,7 +10,7 @@ namespace CliFx.Exceptions
/// <summary>
/// Initializes an instance of <see cref="CliFxException"/>.
/// </summary>
public CliFxException(string message)
public CliFxException(string? message)
: base(message)
{
}
@@ -18,7 +18,7 @@ namespace CliFx.Exceptions
/// <summary>
/// Initializes an instance of <see cref="CliFxException"/>.
/// </summary>
public CliFxException(string message, Exception innerException)
public CliFxException(string? message, Exception? innerException)
: base(message, innerException)
{
}

View File

@@ -1,5 +1,4 @@
using System;
using CliFx.Internal;
namespace CliFx.Exceptions
{
@@ -20,16 +19,16 @@ namespace CliFx.Exceptions
/// <summary>
/// Initializes an instance of <see cref="CommandException"/>.
/// </summary>
public CommandException(string message, Exception innerException, int exitCode = DefaultExitCode)
public CommandException(string? message, Exception? innerException, int exitCode = DefaultExitCode)
: base(message, innerException)
{
ExitCode = exitCode.GuardNotZero(nameof(exitCode));
ExitCode = exitCode != 0 ? exitCode : throw new ArgumentException("Exit code cannot be zero because that signifies success.");
}
/// <summary>
/// Initializes an instance of <see cref="CommandException"/>.
/// </summary>
public CommandException(string message, int exitCode = DefaultExitCode)
public CommandException(string? message, int exitCode = DefaultExitCode)
: this(message, null, exitCode)
{
}

View File

@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using CliFx.Internal;
using CliFx.Models;
using CliFx.Services;
@@ -17,9 +16,6 @@ namespace CliFx
/// </summary>
public static ICliApplicationBuilder AddCommands(this ICliApplicationBuilder builder, IReadOnlyList<Type> commandTypes)
{
builder.GuardNotNull(nameof(builder));
commandTypes.GuardNotNull(nameof(commandTypes));
foreach (var commandType in commandTypes)
builder.AddCommand(commandType);
@@ -31,9 +27,6 @@ namespace CliFx
/// </summary>
public static ICliApplicationBuilder AddCommandsFrom(this ICliApplicationBuilder builder, IReadOnlyList<Assembly> commandAssemblies)
{
builder.GuardNotNull(nameof(builder));
commandAssemblies.GuardNotNull(nameof(commandAssemblies));
foreach (var commandAssembly in commandAssemblies)
builder.AddCommandsFrom(commandAssembly);
@@ -43,21 +36,13 @@ namespace CliFx
/// <summary>
/// Adds commands from calling assembly to the application.
/// </summary>
public static ICliApplicationBuilder AddCommandsFromThisAssembly(this ICliApplicationBuilder builder)
{
builder.GuardNotNull(nameof(builder));
return builder.AddCommandsFrom(Assembly.GetCallingAssembly());
}
public static ICliApplicationBuilder AddCommandsFromThisAssembly(this ICliApplicationBuilder builder) =>
builder.AddCommandsFrom(Assembly.GetCallingAssembly());
/// <summary>
/// Configures application to use specified factory method for creating new instances of <see cref="ICommand"/>.
/// </summary>
public static ICliApplicationBuilder UseCommandFactory(this ICliApplicationBuilder builder, Func<CommandSchema, ICommand> factoryMethod)
{
builder.GuardNotNull(nameof(builder));
factoryMethod.GuardNotNull(nameof(factoryMethod));
return builder.UseCommandFactory(new DelegateCommandFactory(factoryMethod));
}
public static ICliApplicationBuilder UseCommandFactory(this ICliApplicationBuilder builder, Func<CommandSchema, ICommand> factoryMethod) =>
builder.UseCommandFactory(new DelegateCommandFactory(factoryMethod));
}
}

View File

@@ -47,7 +47,7 @@ namespace CliFx
/// <summary>
/// Sets application description, which appears in the help text.
/// </summary>
ICliApplicationBuilder UseDescription(string description);
ICliApplicationBuilder UseDescription(string? description);
/// <summary>
/// Configures application to use specified implementation of <see cref="IConsole"/>.

View File

@@ -1,5 +1,4 @@
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks;
using CliFx.Services;
namespace CliFx

View File

@@ -8,8 +8,6 @@ namespace CliFx.Internal
{
internal static class Extensions
{
public static bool IsNullOrWhiteSpace(this string s) => string.IsNullOrWhiteSpace(s);
public static string Repeat(this char c, int count) => new string(c, count);
public static string AsString(this char c) => c.Repeat(1);
@@ -36,9 +34,9 @@ namespace CliFx.Internal
public static bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType);
public static Type GetNullableUnderlyingType(this Type type) => Nullable.GetUnderlyingType(type);
public static Type? GetNullableUnderlyingType(this Type type) => Nullable.GetUnderlyingType(type);
public static Type GetEnumerableUnderlyingType(this Type type)
public static Type? GetEnumerableUnderlyingType(this Type type)
{
if (type.IsPrimitive)
return null;
@@ -65,5 +63,8 @@ namespace CliFx.Internal
return array;
}
public static bool IsCollection(this Type type) =>
type != typeof(string) && type.GetEnumerableUnderlyingType() != null;
}
}

View File

@@ -1,13 +0,0 @@
using System;
namespace CliFx.Internal
{
internal static class Guards
{
public static T GuardNotNull<T>(this T o, string argName = null) where T : class =>
o ?? throw new ArgumentNullException(argName);
public static int GuardNotZero(this int i, string argName = null) =>
i != 0 ? i : throw new ArgumentException("Cannot be zero.", argName);
}
}

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using CliFx.Internal;
namespace CliFx.Models
{
@@ -30,7 +29,7 @@ namespace CliFx.Models
public ApplicationConfiguration(IReadOnlyList<Type> commandTypes,
bool isDebugModeAllowed, bool isPreviewModeAllowed)
{
CommandTypes = commandTypes.GuardNotNull(nameof(commandTypes));
CommandTypes = commandTypes;
IsDebugModeAllowed = isDebugModeAllowed;
IsPreviewModeAllowed = isPreviewModeAllowed;
}

View File

@@ -1,6 +1,4 @@
using CliFx.Internal;
namespace CliFx.Models
namespace CliFx.Models
{
/// <summary>
/// Metadata associated with an application.
@@ -25,17 +23,17 @@ namespace CliFx.Models
/// <summary>
/// Application description.
/// </summary>
public string Description { get; }
public string? Description { get; }
/// <summary>
/// Initializes an instance of <see cref="ApplicationMetadata"/>.
/// </summary>
public ApplicationMetadata(string title, string executableName, string versionText, string description)
public ApplicationMetadata(string title, string executableName, string versionText, string? description)
{
Title = title.GuardNotNull(nameof(title));
ExecutableName = executableName.GuardNotNull(nameof(executableName));
VersionText = versionText.GuardNotNull(nameof(versionText));
Description = description; // can be null
Title = title;
ExecutableName = executableName;
VersionText = versionText;
Description = description;
}
}
}

View File

@@ -13,7 +13,7 @@ namespace CliFx.Models
/// Specified command name.
/// Can be null if command was not specified.
/// </summary>
public string CommandName { get; }
public string? CommandName { get; }
/// <summary>
/// Specified directives.
@@ -33,18 +33,19 @@ namespace CliFx.Models
/// <summary>
/// Initializes an instance of <see cref="CommandInput"/>.
/// </summary>
public CommandInput(string commandName, IReadOnlyList<string> directives, IReadOnlyList<CommandOptionInput> options, IReadOnlyDictionary<string, string> environmentVariables)
public CommandInput(string? commandName, IReadOnlyList<string> directives, IReadOnlyList<CommandOptionInput> options,
IReadOnlyDictionary<string, string> environmentVariables)
{
CommandName = commandName; // can be null
Directives = directives.GuardNotNull(nameof(directives));
Options = options.GuardNotNull(nameof(options));
EnvironmentVariables = environmentVariables.GuardNotNull(nameof(environmentVariables));
CommandName = commandName;
Directives = directives;
Options = options;
EnvironmentVariables = environmentVariables;
}
/// <summary>
/// Initializes an instance of <see cref="CommandInput"/>.
/// </summary>
public CommandInput(string commandName, IReadOnlyList<string> directives, IReadOnlyList<CommandOptionInput> options)
public CommandInput(string? commandName, IReadOnlyList<string> directives, IReadOnlyList<CommandOptionInput> options)
: this(commandName, directives, options, EmptyEnvironmentVariables)
{
}
@@ -52,7 +53,7 @@ namespace CliFx.Models
/// <summary>
/// Initializes an instance of <see cref="CommandInput"/>.
/// </summary>
public CommandInput(string commandName, IReadOnlyList<CommandOptionInput> options, IReadOnlyDictionary<string, string> environmentVariables)
public CommandInput(string? commandName, IReadOnlyList<CommandOptionInput> options, IReadOnlyDictionary<string, string> environmentVariables)
: this(commandName, EmptyDirectives, options, environmentVariables)
{
}
@@ -60,7 +61,7 @@ namespace CliFx.Models
/// <summary>
/// Initializes an instance of <see cref="CommandInput"/>.
/// </summary>
public CommandInput(string commandName, IReadOnlyList<CommandOptionInput> options)
public CommandInput(string? commandName, IReadOnlyList<CommandOptionInput> options)
: this(commandName, EmptyDirectives, options)
{
}
@@ -76,7 +77,7 @@ namespace CliFx.Models
/// <summary>
/// Initializes an instance of <see cref="CommandInput"/>.
/// </summary>
public CommandInput(string commandName)
public CommandInput(string? commandName)
: this(commandName, EmptyOptions)
{
}
@@ -86,7 +87,7 @@ namespace CliFx.Models
{
var buffer = new StringBuilder();
if (!CommandName.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(CommandName))
buffer.Append(CommandName);
foreach (var directive in Directives)

View File

@@ -24,8 +24,8 @@ namespace CliFx.Models
/// </summary>
public CommandOptionInput(string alias, IReadOnlyList<string> values)
{
Alias = alias.GuardNotNull(nameof(alias));
Values = values.GuardNotNull(nameof(values));
Alias = alias;
Values = values;
}
/// <summary>

View File

@@ -1,6 +1,5 @@
using System.Reflection;
using System.Text;
using CliFx.Internal;
namespace CliFx.Models
{
@@ -12,12 +11,12 @@ namespace CliFx.Models
/// <summary>
/// Underlying property.
/// </summary>
public PropertyInfo Property { get; }
public PropertyInfo? Property { get; }
/// <summary>
/// Option name.
/// </summary>
public string Name { get; }
public string? Name { get; }
/// <summary>
/// Option short name.
@@ -32,24 +31,24 @@ namespace CliFx.Models
/// <summary>
/// Option description.
/// </summary>
public string Description { get; }
public string? Description { get; }
/// <summary>
/// Optional environment variable name that will be used as fallback value if no option value is specified.
/// </summary>
public string EnvironmentVariableName { get; }
public string? EnvironmentVariableName { get; }
/// <summary>
/// Initializes an instance of <see cref="CommandOptionSchema"/>.
/// </summary>
public CommandOptionSchema(PropertyInfo property, string name, char? shortName, bool isRequired, string description, string environmentVariableName)
public CommandOptionSchema(PropertyInfo? property, string? name, char? shortName, bool isRequired, string? description, string? environmentVariableName)
{
Property = property; // can be null
Name = name; // can be null
ShortName = shortName; // can be null
Property = property;
Name = name;
ShortName = shortName;
IsRequired = isRequired;
Description = description; // can be null
EnvironmentVariableName = environmentVariableName; //can be null
Description = description;
EnvironmentVariableName = environmentVariableName;
}
/// <inheritdoc />
@@ -60,10 +59,10 @@ namespace CliFx.Models
if (IsRequired)
buffer.Append('*');
if (!Name.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(Name))
buffer.Append(Name);
if (!Name.IsNullOrWhiteSpace() && ShortName != null)
if (!string.IsNullOrWhiteSpace(Name) && ShortName != null)
buffer.Append('|');
if (ShortName != null)

View File

@@ -13,17 +13,17 @@ namespace CliFx.Models
/// <summary>
/// Underlying type.
/// </summary>
public Type Type { get; }
public Type? Type { get; }
/// <summary>
/// Command name.
/// </summary>
public string Name { get; }
public string? Name { get; }
/// <summary>
/// Command description.
/// </summary>
public string Description { get; }
public string? Description { get; }
/// <summary>
/// Command options.
@@ -33,12 +33,12 @@ namespace CliFx.Models
/// <summary>
/// Initializes an instance of <see cref="CommandSchema"/>.
/// </summary>
public CommandSchema(Type type, string name, string description, IReadOnlyList<CommandOptionSchema> options)
public CommandSchema(Type? type, string? name, string? description, IReadOnlyList<CommandOptionSchema> options)
{
Type = type; // can be null
Name = name; // can be null
Description = description; // can be null
Options = options.GuardNotNull(nameof(options));
Type = type;
Name = name;
Description = description;
Options = options;
}
/// <inheritdoc />
@@ -46,7 +46,7 @@ namespace CliFx.Models
{
var buffer = new StringBuilder();
if (!Name.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(Name))
buffer.Append(Name);
foreach (var option in Options)

View File

@@ -13,13 +13,11 @@ namespace CliFx.Models
/// <summary>
/// Finds a command that has specified name, or null if not found.
/// </summary>
public static CommandSchema FindByName(this IReadOnlyList<CommandSchema> commandSchemas, string commandName)
public static CommandSchema? FindByName(this IReadOnlyList<CommandSchema> commandSchemas, string? commandName)
{
commandSchemas.GuardNotNull(nameof(commandSchemas));
// If looking for default command, don't compare names directly
// ...because null and empty are both valid names for default command
if (commandName.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(commandName))
return commandSchemas.FirstOrDefault(c => c.IsDefault());
return commandSchemas.FirstOrDefault(c => string.Equals(c.Name, commandName, StringComparison.OrdinalIgnoreCase));
@@ -28,12 +26,10 @@ namespace CliFx.Models
/// <summary>
/// Finds parent command to the command that has specified name, or null if not found.
/// </summary>
public static CommandSchema FindParent(this IReadOnlyList<CommandSchema> commandSchemas, string commandName)
public static CommandSchema? FindParent(this IReadOnlyList<CommandSchema> commandSchemas, string? commandName)
{
commandSchemas.GuardNotNull(nameof(commandSchemas));
// If command has no name, it's the default command so it doesn't have a parent
if (commandName.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(commandName))
return null;
// Repeatedly cut off individual words from the name until we find a command with that name
@@ -56,12 +52,9 @@ namespace CliFx.Models
/// </summary>
public static bool MatchesAlias(this CommandOptionSchema optionSchema, string alias)
{
optionSchema.GuardNotNull(nameof(optionSchema));
alias.GuardNotNull(nameof(alias));
// Compare against name. Case is ignored.
var matchesByName =
!optionSchema.Name.IsNullOrWhiteSpace() &&
!string.IsNullOrWhiteSpace(optionSchema.Name) &&
string.Equals(optionSchema.Name, alias, StringComparison.OrdinalIgnoreCase);
// Compare against short name. Case is NOT ignored.
@@ -75,13 +68,8 @@ namespace CliFx.Models
/// <summary>
/// Finds an option input that matches the option schema specified, or null if not found.
/// </summary>
public static CommandOptionInput FindByOptionSchema(this IReadOnlyList<CommandOptionInput> optionInputs, CommandOptionSchema optionSchema)
{
optionInputs.GuardNotNull(nameof(optionInputs));
optionSchema.GuardNotNull(nameof(optionSchema));
return optionInputs.FirstOrDefault(o => optionSchema.MatchesAlias(o.Alias));
}
public static CommandOptionInput? FindByOptionSchema(this IReadOnlyList<CommandOptionInput> optionInputs, CommandOptionSchema optionSchema) =>
optionInputs.FirstOrDefault(o => optionSchema.MatchesAlias(o.Alias));
/// <summary>
/// Gets valid aliases for the option.
@@ -90,8 +78,8 @@ namespace CliFx.Models
{
var result = new List<string>(2);
if (!optionSchema.Name.IsNullOrWhiteSpace())
result.Add(optionSchema.Name);
if (!string.IsNullOrWhiteSpace(optionSchema.Name))
result.Add(optionSchema.Name!);
if (optionSchema.ShortName != null)
result.Add(optionSchema.ShortName.Value.AsString());
@@ -102,37 +90,25 @@ namespace CliFx.Models
/// <summary>
/// Gets whether a command was specified in the input.
/// </summary>
public static bool IsCommandSpecified(this CommandInput commandInput)
{
commandInput.GuardNotNull(nameof(commandInput));
return !commandInput.CommandName.IsNullOrWhiteSpace();
}
public static bool IsCommandSpecified(this CommandInput commandInput) => !string.IsNullOrWhiteSpace(commandInput.CommandName);
/// <summary>
/// Gets whether debug directive was specified in the input.
/// </summary>
public static bool IsDebugDirectiveSpecified(this CommandInput commandInput)
{
commandInput.GuardNotNull(nameof(commandInput));
return commandInput.Directives.Contains("debug", StringComparer.OrdinalIgnoreCase);
}
public static bool IsDebugDirectiveSpecified(this CommandInput commandInput) =>
commandInput.Directives.Contains("debug", StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets whether preview directive was specified in the input.
/// </summary>
public static bool IsPreviewDirectiveSpecified(this CommandInput commandInput)
{
commandInput.GuardNotNull(nameof(commandInput));
return commandInput.Directives.Contains("preview", StringComparer.OrdinalIgnoreCase);
}
public static bool IsPreviewDirectiveSpecified(this CommandInput commandInput) =>
commandInput.Directives.Contains("preview", StringComparer.OrdinalIgnoreCase);
/// <summary>
/// Gets whether help option was specified in the input.
/// </summary>
public static bool IsHelpOptionSpecified(this CommandInput commandInput)
{
commandInput.GuardNotNull(nameof(commandInput));
var firstOption = commandInput.Options.FirstOrDefault();
return firstOption != null && CommandOptionSchema.HelpOption.MatchesAlias(firstOption.Alias);
}
@@ -142,8 +118,6 @@ namespace CliFx.Models
/// </summary>
public static bool IsVersionOptionSpecified(this CommandInput commandInput)
{
commandInput.GuardNotNull(nameof(commandInput));
var firstOption = commandInput.Options.FirstOrDefault();
return firstOption != null && CommandOptionSchema.VersionOption.MatchesAlias(firstOption.Alias);
}
@@ -151,10 +125,6 @@ namespace CliFx.Models
/// <summary>
/// Gets whether this command is the default command, i.e. without a name.
/// </summary>
public static bool IsDefault(this CommandSchema commandSchema)
{
commandSchema.GuardNotNull(nameof(commandSchema));
return commandSchema.Name.IsNullOrWhiteSpace();
}
public static bool IsDefault(this CommandSchema commandSchema) => string.IsNullOrWhiteSpace(commandSchema.Name);
}
}

View File

@@ -1,5 +1,4 @@
using System.Collections.Generic;
using CliFx.Internal;
namespace CliFx.Models
{
@@ -30,9 +29,9 @@ namespace CliFx.Models
IReadOnlyList<CommandSchema> availableCommandSchemas,
CommandSchema targetCommandSchema)
{
ApplicationMetadata = applicationMetadata.GuardNotNull(nameof(applicationMetadata));
AvailableCommandSchemas = availableCommandSchemas.GuardNotNull(nameof(availableCommandSchemas));
TargetCommandSchema = targetCommandSchema.GuardNotNull(nameof(targetCommandSchema));
ApplicationMetadata = applicationMetadata;
AvailableCommandSchemas = availableCommandSchemas;
TargetCommandSchema = targetCommandSchema;
}
}
}

View File

@@ -1,5 +1,4 @@
using System;
using CliFx.Internal;
using CliFx.Models;
namespace CliFx.Services
@@ -10,10 +9,6 @@ namespace CliFx.Services
public class CommandFactory : ICommandFactory
{
/// <inheritdoc />
public ICommand CreateCommand(CommandSchema commandSchema)
{
commandSchema.GuardNotNull(nameof(commandSchema));
return (ICommand) Activator.CreateInstance(commandSchema.Type);
}
public ICommand CreateCommand(CommandSchema commandSchema) => (ICommand) Activator.CreateInstance(commandSchema.Type);
}
}

View File

@@ -18,8 +18,8 @@ namespace CliFx.Services
/// </summary>
public CommandInitializer(ICommandOptionInputConverter commandOptionInputConverter, IEnvironmentVariablesParser environmentVariablesParser)
{
_commandOptionInputConverter = commandOptionInputConverter.GuardNotNull(nameof(commandOptionInputConverter));
_environmentVariablesParser = environmentVariablesParser.GuardNotNull(nameof(environmentVariablesParser));
_commandOptionInputConverter = commandOptionInputConverter;
_environmentVariablesParser = environmentVariablesParser;
}
/// <summary>
@@ -41,29 +41,29 @@ namespace CliFx.Services
/// <inheritdoc />
public void InitializeCommand(ICommand command, CommandSchema commandSchema, CommandInput commandInput)
{
command.GuardNotNull(nameof(command));
commandSchema.GuardNotNull(nameof(commandSchema));
commandInput.GuardNotNull(nameof(commandInput));
// Keep track of unset required options to report an error at a later stage
var unsetRequiredOptions = commandSchema.Options.Where(o => o.IsRequired).ToList();
//Set command options
foreach (var optionSchema in commandSchema.Options)
{
// Ignore special options that are not backed by a property
if (optionSchema.Property == null)
continue;
//Find matching option input
var optionInput = commandInput.Options.FindByOptionSchema(optionSchema);
//If no option input is available fall back to environment variable values
if (optionInput == null && !optionSchema.EnvironmentVariableName.IsNullOrWhiteSpace())
if (optionInput == null && !string.IsNullOrWhiteSpace(optionSchema.EnvironmentVariableName))
{
var fallbackEnvironmentVariableExists = commandInput.EnvironmentVariables.ContainsKey(optionSchema.EnvironmentVariableName);
var fallbackEnvironmentVariableExists = commandInput.EnvironmentVariables.ContainsKey(optionSchema.EnvironmentVariableName!);
//If no environment variable is found or there is no valid value for this option skip it
if (!fallbackEnvironmentVariableExists || commandInput.EnvironmentVariables[optionSchema.EnvironmentVariableName].IsNullOrWhiteSpace())
if (!fallbackEnvironmentVariableExists || string.IsNullOrWhiteSpace(commandInput.EnvironmentVariables[optionSchema.EnvironmentVariableName!]))
continue;
optionInput = _environmentVariablesParser.GetCommandOptionInputFromEnvironmentVariable(commandInput.EnvironmentVariables[optionSchema.EnvironmentVariableName], optionSchema);
optionInput = _environmentVariablesParser.GetCommandOptionInputFromEnvironmentVariable(commandInput.EnvironmentVariables[optionSchema.EnvironmentVariableName!], optionSchema);
}
//No fallback available and no option input was specified, skip option

View File

@@ -19,8 +19,6 @@ namespace CliFx.Services
/// </summary>
public CommandInputParser(IEnvironmentVariablesProvider environmentVariablesProvider)
{
environmentVariablesProvider.GuardNotNull(nameof(environmentVariablesProvider));
_environmentVariablesProvider = environmentVariablesProvider;
}
@@ -35,8 +33,6 @@ namespace CliFx.Services
/// <inheritdoc />
public CommandInput ParseCommandInput(IReadOnlyList<string> commandLineArguments)
{
commandLineArguments.GuardNotNull(nameof(commandLineArguments));
var commandNameBuilder = new StringBuilder();
var directives = new List<string>();
var optionsDic = new Dictionary<string, List<string>>();
@@ -71,7 +67,7 @@ namespace CliFx.Services
}
// Encountered directive or (part of) command name
else if (lastOptionAlias.IsNullOrWhiteSpace())
else if (string.IsNullOrWhiteSpace(lastOptionAlias))
{
if (commandLineArgument.StartsWith("[", StringComparison.OrdinalIgnoreCase) &&
commandLineArgument.EndsWith("]", StringComparison.OrdinalIgnoreCase))
@@ -89,7 +85,7 @@ namespace CliFx.Services
}
// Encountered option value
else if (!lastOptionAlias.IsNullOrWhiteSpace())
else if (!string.IsNullOrWhiteSpace(lastOptionAlias))
{
optionsDic[lastOptionAlias].Add(commandLineArgument);
}

View File

@@ -20,7 +20,7 @@ namespace CliFx.Services
/// </summary>
public CommandOptionInputConverter(IFormatProvider formatProvider)
{
_formatProvider = formatProvider.GuardNotNull(nameof(formatProvider));
_formatProvider = formatProvider;
}
/// <summary>
@@ -34,10 +34,8 @@ namespace CliFx.Services
/// <summary>
/// Converts a single string value to specified target type.
/// </summary>
protected virtual object ConvertValue(string value, Type targetType)
protected virtual object? ConvertValue(string value, Type targetType)
{
targetType.GuardNotNull(nameof(targetType));
try
{
// String or object
@@ -46,7 +44,7 @@ namespace CliFx.Services
// Bool
if (targetType == typeof(bool))
return value.IsNullOrWhiteSpace() || bool.Parse(value);
return string.IsNullOrWhiteSpace(value) || bool.Parse(value);
// Char
if (targetType == typeof(char))
@@ -115,7 +113,7 @@ namespace CliFx.Services
// Nullable
var nullableUnderlyingType = targetType.GetNullableUnderlyingType();
if (nullableUnderlyingType != null)
return !value.IsNullOrWhiteSpace() ? ConvertValue(value, nullableUnderlyingType) : null;
return !string.IsNullOrWhiteSpace(value) ? ConvertValue(value, nullableUnderlyingType) : null;
// Has a constructor that accepts a single string
var stringConstructor = GetStringConstructor(targetType);
@@ -143,11 +141,8 @@ namespace CliFx.Services
}
/// <inheritdoc />
public virtual object ConvertOptionInput(CommandOptionInput optionInput, Type targetType)
public virtual object? ConvertOptionInput(CommandOptionInput optionInput, Type targetType)
{
optionInput.GuardNotNull(nameof(optionInput));
targetType.GuardNotNull(nameof(targetType));
// Get the underlying type of IEnumerable<T> if it's implemented by the target type.
// Ignore string type because it's IEnumerable<T> but we don't treat it as such.
var enumerableUnderlyingType = targetType != typeof(string) ? targetType.GetEnumerableUnderlyingType() : null;

View File

@@ -36,7 +36,7 @@ namespace CliFx.Services
// Make sure there are no other options with the same name
var existingOptionWithSameName = result
.Where(o => !o.Name.IsNullOrWhiteSpace())
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
.FirstOrDefault(o => string.Equals(o.Name, optionSchema.Name, StringComparison.OrdinalIgnoreCase));
if (existingOptionWithSameName != null)
@@ -68,8 +68,6 @@ namespace CliFx.Services
/// <inheritdoc />
public IReadOnlyList<CommandSchema> GetCommandSchemas(IReadOnlyList<Type> commandTypes)
{
commandTypes.GuardNotNull(nameof(commandTypes));
// Make sure there's at least one command defined
if (!commandTypes.Any())
{

View File

@@ -1,5 +1,4 @@
using System;
using CliFx.Internal;
using CliFx.Models;
namespace CliFx.Services
@@ -16,14 +15,10 @@ namespace CliFx.Services
/// </summary>
public DelegateCommandFactory(Func<CommandSchema, ICommand> factoryMethod)
{
_factoryMethod = factoryMethod.GuardNotNull(nameof(factoryMethod));
_factoryMethod = factoryMethod;
}
/// <inheritdoc />
public ICommand CreateCommand(CommandSchema commandSchema)
{
commandSchema.GuardNotNull(nameof(commandSchema));
return _factoryMethod(commandSchema);
}
public ICommand CreateCommand(CommandSchema commandSchema) => _factoryMethod(commandSchema);
}
}

View File

@@ -11,17 +11,14 @@ namespace CliFx.Services
/// <inheritdoct />
public CommandOptionInput GetCommandOptionInputFromEnvironmentVariable(string environmentVariableValue, CommandOptionSchema targetOptionSchema)
{
environmentVariableValue.GuardNotNull(nameof(environmentVariableValue));
targetOptionSchema.GuardNotNull(nameof(targetOptionSchema));
//If the option is not a collection do not split environment variable values
var optionIsCollection = targetOptionSchema.Property.PropertyType.IsCollection();
var optionIsCollection = targetOptionSchema.Property != null && targetOptionSchema.Property.PropertyType.IsCollection();
if (!optionIsCollection) return new CommandOptionInput(targetOptionSchema.EnvironmentVariableName, environmentVariableValue);
//If the option is a collection split the values using System separator, empty values are discarded
var environmentVariableValues = environmentVariableValue.Split(Path.PathSeparator)
.Where(v => !v.IsNullOrWhiteSpace())
.Where(v => !string.IsNullOrWhiteSpace(v))
.ToList();
return new CommandOptionInput(targetOptionSchema.EnvironmentVariableName, environmentVariableValues);

View File

@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using CliFx.Internal;
namespace CliFx.Services
{
@@ -14,9 +12,6 @@ namespace CliFx.Services
/// </summary>
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
{
console.GuardNotNull(nameof(console));
action.GuardNotNull(nameof(action));
var lastColor = console.ForegroundColor;
console.ForegroundColor = foregroundColor;
@@ -30,9 +25,6 @@ namespace CliFx.Services
/// </summary>
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
{
console.GuardNotNull(nameof(console));
action.GuardNotNull(nameof(action));
var lastColor = console.BackgroundColor;
console.BackgroundColor = backgroundColor;
@@ -44,32 +36,7 @@ namespace CliFx.Services
/// <summary>
/// Sets console foreground and background colors, executes specified action, and sets the colors back to the original values.
/// </summary>
public static void WithColors(this IConsole console, ConsoleColor foregroundColor, ConsoleColor backgroundColor, Action action)
{
console.GuardNotNull(nameof(console));
action.GuardNotNull(nameof(action));
public static void WithColors(this IConsole console, ConsoleColor foregroundColor, ConsoleColor backgroundColor, Action action) =>
console.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
}
/// <summary>
/// Gets wether a string representing an environment variable value is escaped (i.e.: surrounded by double quotation marks)
/// </summary>
public static bool IsEnvironmentVariableEscaped(this string environmentVariableValue)
{
environmentVariableValue.GuardNotNull(nameof(environmentVariableValue));
return environmentVariableValue.StartsWith("\"") && environmentVariableValue.EndsWith("\"");
}
/// <summary>
/// Gets wether the <see cref="Type"/> supplied is a collection implementing <see cref="IEnumerable{T}"/>
/// </summary>
public static bool IsCollection(this Type type)
{
type.GuardNotNull(nameof(type));
return type != typeof(string) && type.GetEnumerableUnderlyingType() != null;
}
}
}

View File

@@ -14,9 +14,6 @@ namespace CliFx.Services
/// <inheritdoc />
public void RenderHelpText(IConsole console, HelpTextSource source)
{
console.GuardNotNull(nameof(console));
source.GuardNotNull(nameof(source));
// Track position
var column = 0;
var row = 0;
@@ -105,7 +102,7 @@ namespace CliFx.Services
RenderNewLine();
// Description
if (!source.ApplicationMetadata.Description.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(source.ApplicationMetadata.Description))
{
Render(source.ApplicationMetadata.Description);
RenderNewLine();
@@ -114,7 +111,7 @@ namespace CliFx.Services
void RenderDescription()
{
if (source.TargetCommandSchema.Description.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(source.TargetCommandSchema.Description))
return;
// Margin
@@ -142,7 +139,7 @@ namespace CliFx.Services
Render(source.ApplicationMetadata.ExecutableName);
// Command name
if (!source.TargetCommandSchema.IsDefault())
if (!string.IsNullOrWhiteSpace(source.TargetCommandSchema.Name))
{
Render(" ");
RenderWithColor(source.TargetCommandSchema.Name, ConsoleColor.Cyan);
@@ -195,19 +192,19 @@ namespace CliFx.Services
}
// Delimiter
if (!optionSchema.Name.IsNullOrWhiteSpace() && optionSchema.ShortName != null)
if (!string.IsNullOrWhiteSpace(optionSchema.Name) && optionSchema.ShortName != null)
{
Render("|");
}
// Name
if (!optionSchema.Name.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(optionSchema.Name))
{
RenderWithColor($"--{optionSchema.Name}", ConsoleColor.White);
}
// Description
if (!optionSchema.Description.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(optionSchema.Description))
{
RenderColumnIndent();
Render(optionSchema.Description);
@@ -231,14 +228,14 @@ namespace CliFx.Services
// Child commands
foreach (var childCommandSchema in childCommandSchemas)
{
var relativeCommandName = GetRelativeCommandName(childCommandSchema, source.TargetCommandSchema);
var relativeCommandName = GetRelativeCommandName(childCommandSchema, source.TargetCommandSchema)!;
// Name
RenderIndent();
RenderWithColor(relativeCommandName, ConsoleColor.Cyan);
// Description
if (!childCommandSchema.Description.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(childCommandSchema.Description))
{
RenderColumnIndent();
Render(childCommandSchema.Description);
@@ -254,7 +251,7 @@ namespace CliFx.Services
Render("You can run `");
Render(source.ApplicationMetadata.ExecutableName);
if (!source.TargetCommandSchema.IsDefault())
if (!string.IsNullOrWhiteSpace(source.TargetCommandSchema.Name))
{
Render(" ");
RenderWithColor(source.TargetCommandSchema.Name, ConsoleColor.Cyan);
@@ -285,8 +282,8 @@ namespace CliFx.Services
public partial class HelpTextRenderer
{
private static string GetRelativeCommandName(CommandSchema commandSchema, CommandSchema parentCommandSchema) =>
parentCommandSchema.Name.IsNullOrWhiteSpace()
private static string? GetRelativeCommandName(CommandSchema commandSchema, CommandSchema parentCommandSchema) =>
string.IsNullOrWhiteSpace(parentCommandSchema.Name) || string.IsNullOrWhiteSpace(commandSchema.Name)
? commandSchema.Name
: commandSchema.Name.Substring(parentCommandSchema.Name.Length + 1);
}

View File

@@ -11,6 +11,6 @@ namespace CliFx.Services
/// <summary>
/// Converts an option to specified target type.
/// </summary>
object ConvertOptionInput(CommandOptionInput optionInput, Type targetType);
object? ConvertOptionInput(CommandOptionInput optionInput, Type targetType);
}
}

View File

@@ -9,7 +9,7 @@ namespace CliFx.Services
/// </summary>
public class SystemConsole : IConsole
{
private CancellationTokenSource _cancellationTokenSource;
private CancellationTokenSource? _cancellationTokenSource;
/// <inheritdoc />
public TextReader Input => Console.In;

View File

@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Threading;
using CliFx.Internal;
namespace CliFx.Services
{
@@ -46,11 +45,11 @@ namespace CliFx.Services
TextWriter error, bool isErrorRedirected,
CancellationToken cancellationToken = default)
{
Input = input.GuardNotNull(nameof(input));
Input = input;
IsInputRedirected = isInputRedirected;
Output = output.GuardNotNull(nameof(output));
Output = output;
IsOutputRedirected = isOutputRedirected;
Error = error.GuardNotNull(nameof(error));
Error = error;
IsErrorRedirected = isErrorRedirected;
_cancellationToken = cancellationToken;
}

View File

@@ -1,10 +1,9 @@
# CliFx
[![Build](https://img.shields.io/appveyor/ci/Tyrrrz/CliFx/master.svg)](https://ci.appveyor.com/project/Tyrrrz/CliFx/branch/master)
[![Tests](https://img.shields.io/appveyor/tests/Tyrrrz/CliFx/master.svg)](https://ci.appveyor.com/project/Tyrrrz/CliFx/branch/master/tests)
[![Coverage](https://img.shields.io/codecov/c/gh/Tyrrrz/CliFx/master.svg)](https://codecov.io/gh/Tyrrrz/CliFx)
[![NuGet](https://img.shields.io/nuget/v/CliFx.svg)](https://nuget.org/packages/CliFx)
[![NuGet](https://img.shields.io/nuget/dt/CliFx.svg)](https://nuget.org/packages/CliFx)
[![Build](https://github.com/Tyrrrz/CliFx/workflows/CI/badge.svg?branch=master)](https://github.com/Tyrrrz/CliFx/actions)
[![Coverage](https://codecov.io/gh/Tyrrrz/CliFx/branch/master/graph/badge.svg)](https://codecov.io/gh/Tyrrrz/CliFx)
[![Version](https://img.shields.io/nuget/v/CliFx.svg)](https://nuget.org/packages/CliFx)
[![Downloads](https://img.shields.io/nuget/dt/CliFx.svg)](https://nuget.org/packages/CliFx)
[![Donate](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://patreon.com/tyrrrz)
[![Donate](https://img.shields.io/badge/buymeacoffee-donate-yellow.svg)](https://buymeacoffee.com/tyrrrz)
@@ -15,7 +14,6 @@ _CliFx is to command line interfaces what ASP.NET Core is to web applications._
## Download
- [NuGet](https://nuget.org/packages/CliFx): `dotnet add package CliFx`
- [Continuous integration](https://ci.appveyor.com/project/Tyrrrz/CliFx)
## Features
@@ -220,7 +218,7 @@ public class SecondSubCommand : ICommand
It is possible to gracefully cancel execution of a command and preform any necessary cleanup. By default an app gets forcefully killed when it receives an interrupt signal (Ctrl+C or Ctrl+Break). You can call `console.GetCancellationToken()` to override the default behavior and get `CancellationToken` that represents the first interrupt signal. Second interrupt signal terminates an app immediately. Note that the code that executes before the first call to `GetCancellationToken` will not be cancellation aware.
You can pass `CancellationToken` around and check its state.
You can pass `CancellationToken` around and check its state.
Cancelled or terminated app returns non-zero exit code.

View File

@@ -1,26 +0,0 @@
version: '{build}'
image: Visual Studio 2019
configuration: Release
before_build:
- dotnet restore
build:
verbosity: minimal
after_test:
- choco install codecov && codecov -f "CliFx.Tests/bin/%CONFIGURATION%/Coverage.xml" --required
artifacts:
- path: CliFx/bin/$(configuration)/CliFx*.nupkg
name: CliFx.nupkg
deploy:
- provider: NuGet
api_key:
secure: 5VyEaGo5gRLr9HdkRFqS1enRq+K8Qarg1dzU33CE1dOmVXp43JaS2PQTNgsRHXkc
artifact: CliFx.nupkg
on:
branch: master
appveyor_repo_tag: true