From 6aa72e45e8fc315d769a27b749f5c994fed85ff4 Mon Sep 17 00:00:00 2001 From: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com> Date: Fri, 12 Jan 2024 00:10:27 +0200 Subject: [PATCH] Use latest C# features --- .../CliFx.Analyzers.Tests.csproj | 2 +- ...ionMustHaveValidValidatorsAnalyzerSpecs.cs | 6 +- ...terMustHaveValidValidatorsAnalyzerSpecs.cs | 6 +- .../Utils/AnalyzerAssertions.cs | 6 +- CliFx.Benchmarks/Benchmarks.Cocona.cs | 6 +- .../Benchmarks.SystemCommandLine.cs | 6 +- CliFx.Benchmarks/Benchmarks.cs | 2 +- CliFx.Tests/ApplicationSpecs.cs | 4 +- CliFx.Tests/CliFx.Tests.csproj | 2 +- CliFx.Tests/ConversionSpecs.cs | 76 +++++--------- CliFx.Tests/DirectivesSpecs.cs | 2 +- CliFx.Tests/EnvironmentSpecs.cs | 2 +- CliFx.Tests/HelpTextSpecs.cs | 98 +++++-------------- CliFx.Tests/OptionBindingSpecs.cs | 43 ++++---- CliFx.Tests/ParameterBindingSpecs.cs | 24 ++--- CliFx.Tests/RoutingSpecs.cs | 7 +- CliFx.Tests/Utils/DynamicCommandBuilder.cs | 6 +- CliFx/ApplicationConfiguration.cs | 26 ++--- CliFx/ApplicationMetadata.cs | 31 ++---- CliFx/Attributes/CommandParameterAttribute.cs | 12 +-- CliFx/CliApplication.cs | 60 +++++------- CliFx/CliApplicationBuilder.cs | 2 +- CliFx/CommandBinder.cs | 30 +++--- CliFx/Exceptions/CliFxException.cs | 29 ++---- CliFx/Exceptions/CommandException.cs | 19 ++-- CliFx/Extensibility/BindingValidationError.cs | 9 +- CliFx/Infrastructure/ConsoleReader.cs | 22 ++--- CliFx/Infrastructure/ConsoleWriter.cs | 22 ++--- CliFx/Infrastructure/DelegateTypeActivator.cs | 12 +-- CliFx/Input/CommandInput.cs | 4 +- 30 files changed, 186 insertions(+), 390 deletions(-) diff --git a/CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj b/CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj index c5a296d..09a4d41 100644 --- a/CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj +++ b/CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/CliFx.Analyzers.Tests/OptionMustHaveValidValidatorsAnalyzerSpecs.cs b/CliFx.Analyzers.Tests/OptionMustHaveValidValidatorsAnalyzerSpecs.cs index 9fcb78e..d454d36 100644 --- a/CliFx.Analyzers.Tests/OptionMustHaveValidValidatorsAnalyzerSpecs.cs +++ b/CliFx.Analyzers.Tests/OptionMustHaveValidValidatorsAnalyzerSpecs.cs @@ -23,7 +23,7 @@ public class OptionMustHaveValidValidatorsAnalyzerSpecs [Command] public class MyCommand : ICommand { - [CommandOption("foo", Validators = new[] {typeof(MyValidator)})] + [CommandOption("foo", Validators = new[] { typeof(MyValidator) })] public string? Foo { get; init; } public ValueTask ExecuteAsync(IConsole console) => default; @@ -48,7 +48,7 @@ public class OptionMustHaveValidValidatorsAnalyzerSpecs [Command] public class MyCommand : ICommand { - [CommandOption("foo", Validators = new[] {typeof(MyValidator)})] + [CommandOption("foo", Validators = new[] { typeof(MyValidator) })] public string? Foo { get; init; } public ValueTask ExecuteAsync(IConsole console) => default; @@ -73,7 +73,7 @@ public class OptionMustHaveValidValidatorsAnalyzerSpecs [Command] public class MyCommand : ICommand { - [CommandOption("foo", Validators = new[] {typeof(MyValidator)})] + [CommandOption("foo", Validators = new[] { typeof(MyValidator) })] public string? Foo { get; init; } public ValueTask ExecuteAsync(IConsole console) => default; diff --git a/CliFx.Analyzers.Tests/ParameterMustHaveValidValidatorsAnalyzerSpecs.cs b/CliFx.Analyzers.Tests/ParameterMustHaveValidValidatorsAnalyzerSpecs.cs index 778a702..d1c3bb8 100644 --- a/CliFx.Analyzers.Tests/ParameterMustHaveValidValidatorsAnalyzerSpecs.cs +++ b/CliFx.Analyzers.Tests/ParameterMustHaveValidValidatorsAnalyzerSpecs.cs @@ -23,7 +23,7 @@ public class ParameterMustHaveValidValidatorsAnalyzerSpecs [Command] public class MyCommand : ICommand { - [CommandParameter(0, Validators = new[] {typeof(MyValidator)})] + [CommandParameter(0, Validators = new[] { typeof(MyValidator) })] public required string Foo { get; init; } public ValueTask ExecuteAsync(IConsole console) => default; @@ -48,7 +48,7 @@ public class ParameterMustHaveValidValidatorsAnalyzerSpecs [Command] public class MyCommand : ICommand { - [CommandParameter(0, Validators = new[] {typeof(MyValidator)})] + [CommandParameter(0, Validators = new[] { typeof(MyValidator) })] public required string Foo { get; init; } public ValueTask ExecuteAsync(IConsole console) => default; @@ -73,7 +73,7 @@ public class ParameterMustHaveValidValidatorsAnalyzerSpecs [Command] public class MyCommand : ICommand { - [CommandParameter(0, Validators = new[] {typeof(MyValidator)})] + [CommandParameter(0, Validators = new[] { typeof(MyValidator) })] public required string Foo { get; init; } public ValueTask ExecuteAsync(IConsole console) => default; diff --git a/CliFx.Analyzers.Tests/Utils/AnalyzerAssertions.cs b/CliFx.Analyzers.Tests/Utils/AnalyzerAssertions.cs index e08239c..100caa7 100644 --- a/CliFx.Analyzers.Tests/Utils/AnalyzerAssertions.cs +++ b/CliFx.Analyzers.Tests/Utils/AnalyzerAssertions.cs @@ -47,14 +47,14 @@ internal class AnalyzerAssertions(DiagnosticAnalyzer analyzer) // Parse the source code var ast = SyntaxFactory.ParseSyntaxTree( SourceText.From(sourceCodeWithUsings), - CSharpParseOptions.Default + CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview) ); // Compile the code to IL var compilation = CSharpCompilation.Create( "CliFxTests_DynamicAssembly_" + Guid.NewGuid(), - new[] { ast }, - Net70 + [ast], + Net80 .References .All .Append(MetadataReference.CreateFromFile(typeof(ICommand).Assembly.Location)), diff --git a/CliFx.Benchmarks/Benchmarks.Cocona.cs b/CliFx.Benchmarks/Benchmarks.Cocona.cs index a42526c..99ed778 100644 --- a/CliFx.Benchmarks/Benchmarks.Cocona.cs +++ b/CliFx.Benchmarks/Benchmarks.Cocona.cs @@ -8,9 +8,9 @@ public partial class Benchmarks public class CoconaCommand { public void Execute( - [Option("str", new[] { 's' })] string? strOption, - [Option("int", new[] { 'i' })] int intOption, - [Option("bool", new[] { 'b' })] bool boolOption + [Option("str", ['s'])] string? strOption, + [Option("int", ['i'])] int intOption, + [Option("bool", ['b'])] bool boolOption ) { } } diff --git a/CliFx.Benchmarks/Benchmarks.SystemCommandLine.cs b/CliFx.Benchmarks/Benchmarks.SystemCommandLine.cs index 28522a3..5a5d433 100644 --- a/CliFx.Benchmarks/Benchmarks.SystemCommandLine.cs +++ b/CliFx.Benchmarks/Benchmarks.SystemCommandLine.cs @@ -15,9 +15,9 @@ public partial class Benchmarks { var command = new RootCommand { - new Option(new[] { "--str", "-s" }) { Argument = new Argument() }, - new Option(new[] { "--int", "-i" }) { Argument = new Argument() }, - new Option(new[] { "--bool", "-b" }) { Argument = new Argument() } + new Option(["--str", "-s"]) { Argument = new Argument() }, + new Option(["--int", "-i"]) { Argument = new Argument() }, + new Option(["--bool", "-b"]) { Argument = new Argument() } }; command.Handler = CommandHandler.Create( diff --git a/CliFx.Benchmarks/Benchmarks.cs b/CliFx.Benchmarks/Benchmarks.cs index c01fc3e..d8124c7 100644 --- a/CliFx.Benchmarks/Benchmarks.cs +++ b/CliFx.Benchmarks/Benchmarks.cs @@ -9,7 +9,7 @@ namespace CliFx.Benchmarks; [Orderer(SummaryOrderPolicy.FastestToSlowest)] public partial class Benchmarks { - private static readonly string[] Arguments = { "--str", "hello world", "-i", "13", "-b" }; + private static readonly string[] Arguments = ["--str", "hello world", "-i", "13", "-b"]; public static void Main() => BenchmarkRunner.Run( diff --git a/CliFx.Tests/ApplicationSpecs.cs b/CliFx.Tests/ApplicationSpecs.cs index d39e39a..4b03589 100644 --- a/CliFx.Tests/ApplicationSpecs.cs +++ b/CliFx.Tests/ApplicationSpecs.cs @@ -32,8 +32,8 @@ public class ApplicationSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutp var app = new CliApplicationBuilder() .AddCommand() .AddCommandsFrom(typeof(NoOpCommand).Assembly) - .AddCommands(new[] { typeof(NoOpCommand) }) - .AddCommandsFrom(new[] { typeof(NoOpCommand).Assembly }) + .AddCommands([typeof(NoOpCommand)]) + .AddCommandsFrom([typeof(NoOpCommand).Assembly]) .AddCommandsFromThisAssembly() .AllowDebugMode() .AllowPreviewMode() diff --git a/CliFx.Tests/CliFx.Tests.csproj b/CliFx.Tests/CliFx.Tests.csproj index 9544750..55a21bc 100644 --- a/CliFx.Tests/CliFx.Tests.csproj +++ b/CliFx.Tests/CliFx.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/CliFx.Tests/ConversionSpecs.cs b/CliFx.Tests/ConversionSpecs.cs index cb88f17..5d9bda8 100644 --- a/CliFx.Tests/ConversionSpecs.cs +++ b/CliFx.Tests/ConversionSpecs.cs @@ -38,10 +38,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "xyz" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -78,10 +75,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "xyz" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -128,7 +122,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "true", "-b", "false", "-c" }, + ["-f", "true", "-b", "false", "-c"], new Dictionary() ); @@ -167,10 +161,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "32" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "32"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -208,7 +199,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "32.14" }, + ["-f", "32.14"], new Dictionary() ); @@ -248,7 +239,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "1995-04-28Z" }, + ["-f", "1995-04-28Z"], new Dictionary() ); @@ -288,7 +279,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "12:34:56" }, + ["-f", "12:34:56"], new Dictionary() ); @@ -329,10 +320,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "two" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "two"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -374,10 +362,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-b", "123" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-b", "123"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -421,10 +406,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-b", "two" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-b", "two"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -468,10 +450,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "xyz" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -534,7 +513,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "hello", "-b", "world" }, + ["-f", "hello", "-b", "world"], new Dictionary() ); @@ -580,7 +559,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "hello world" }, + ["-f", "hello world"], new Dictionary() ); @@ -622,7 +601,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "one", "two", "three" }, + ["-f", "one", "two", "three"], new Dictionary() ); @@ -664,7 +643,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "one", "two", "three" }, + ["-f", "one", "two", "three"], new Dictionary() ); @@ -706,7 +685,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "one", "two", "three" }, + ["-f", "one", "two", "three"], new Dictionary() ); @@ -748,7 +727,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "1", "13", "27" }, + ["-f", "1", "13", "27"], new Dictionary() ); @@ -787,10 +766,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "xyz" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "xyz"], new Dictionary()); // Assert exitCode.Should().NotBe(0); @@ -831,7 +807,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "one", "two" }, + ["-f", "one", "two"], new Dictionary() ); @@ -867,7 +843,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "-f", "12.34" }, + ["-f", "12.34"], new Dictionary() ); @@ -898,7 +874,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu [Command] public class Command : ICommand { - [CommandOption('f', Validators = new[] {typeof(ValidatorA), typeof(ValidatorB)})] + [CommandOption('f', Validators = [typeof(ValidatorA), typeof(ValidatorB)])] public int Foo { get; init; } public ValueTask ExecuteAsync(IConsole console) => default; @@ -912,10 +888,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "12" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "12"], new Dictionary()); // Assert exitCode.Should().NotBe(0); @@ -957,10 +930,7 @@ public class ConversionSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "-f", "bar" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["-f", "bar"], new Dictionary()); // Assert exitCode.Should().NotBe(0); diff --git a/CliFx.Tests/DirectivesSpecs.cs b/CliFx.Tests/DirectivesSpecs.cs index f5e601f..130e93a 100644 --- a/CliFx.Tests/DirectivesSpecs.cs +++ b/CliFx.Tests/DirectivesSpecs.cs @@ -63,7 +63,7 @@ public class DirectivesSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutpu // Act var exitCode = await application.RunAsync( - new[] { "[preview]", "cmd", "param", "-abc", "--option", "foo" }, + ["[preview]", "cmd", "param", "-abc", "--option", "foo"], new Dictionary { ["ENV_QOP"] = "hello", ["ENV_KIL"] = "world" } ); diff --git a/CliFx.Tests/EnvironmentSpecs.cs b/CliFx.Tests/EnvironmentSpecs.cs index 61e4d8a..aa233a4 100644 --- a/CliFx.Tests/EnvironmentSpecs.cs +++ b/CliFx.Tests/EnvironmentSpecs.cs @@ -48,7 +48,7 @@ public class EnvironmentSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutp // Act var exitCode = await application.RunAsync( - new[] { "--foo", "42" }, + ["--foo", "42"], new Dictionary { ["ENV_FOO"] = "100", ["ENV_BAR"] = "200" } ); diff --git a/CliFx.Tests/HelpTextSpecs.cs b/CliFx.Tests/HelpTextSpecs.cs index f8ce50e..4fae4bb 100644 --- a/CliFx.Tests/HelpTextSpecs.cs +++ b/CliFx.Tests/HelpTextSpecs.cs @@ -55,10 +55,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -95,10 +92,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -141,7 +135,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) // Act var exitCode = await application.RunAsync( - new[] { "cmd", "--help" }, + ["cmd", "--help"], new Dictionary() ); @@ -186,7 +180,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) // Act var exitCode = await application.RunAsync( - new[] { "cmd", "sub", "--help" }, + ["cmd", "sub", "--help"], new Dictionary() ); @@ -209,7 +203,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) // Act var exitCode = await application.RunAsync( - new[] { "invalid-command", "--invalid-option" }, + ["invalid-command", "--invalid-option"], new Dictionary() ); @@ -235,10 +229,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -268,10 +259,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -307,10 +295,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -349,10 +334,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -398,10 +380,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -440,10 +419,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -481,10 +457,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -523,10 +496,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -566,7 +536,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) // Act var exitCode = await application.RunAsync( - new[] { "cmd", "--help" }, + ["cmd", "--help"], new Dictionary() ); @@ -609,10 +579,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -665,10 +632,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -721,10 +685,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -777,10 +738,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -846,10 +804,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -927,10 +882,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -1001,10 +953,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--help" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--help"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -1035,10 +984,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--version" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--version"], new Dictionary()); // Assert exitCode.Should().Be(0); diff --git a/CliFx.Tests/OptionBindingSpecs.cs b/CliFx.Tests/OptionBindingSpecs.cs index 8acc2df..b635803 100644 --- a/CliFx.Tests/OptionBindingSpecs.cs +++ b/CliFx.Tests/OptionBindingSpecs.cs @@ -39,10 +39,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--foo" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--foo"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -79,7 +76,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu .Build(); // Act - var exitCode = await application.RunAsync(new[] { "-f" }, new Dictionary()); + var exitCode = await application.RunAsync(["-f"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -122,7 +119,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "one", "--bar", "two" }, + ["--foo", "one", "--bar", "two"], new Dictionary() ); @@ -167,7 +164,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "-f", "one", "-b", "two" }, + ["-f", "one", "-b", "two"], new Dictionary() ); @@ -212,7 +209,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "-fb", "value" }, + ["-fb", "value"], new Dictionary() ); @@ -254,7 +251,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "one", "two", "three" }, + ["--foo", "one", "two", "three"], new Dictionary() ); @@ -296,7 +293,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "-f", "one", "two", "three" }, + ["-f", "one", "two", "three"], new Dictionary() ); @@ -338,7 +335,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "one", "--foo", "two", "--foo", "three" }, + ["--foo", "one", "--foo", "two", "--foo", "three"], new Dictionary() ); @@ -380,7 +377,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "-f", "one", "-f", "two", "-f", "three" }, + ["-f", "one", "-f", "two", "-f", "three"], new Dictionary() ); @@ -422,7 +419,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "one", "-f", "two", "--foo", "three" }, + ["--foo", "one", "-f", "two", "--foo", "three"], new Dictionary() ); @@ -467,7 +464,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "one" }, + ["--foo", "one"], new Dictionary() ); @@ -541,7 +538,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu .Build(); // Act - var exitCode = await application.RunAsync(new[] { "--foo", "42", "--bar", "--baz", "xyz" }); + var exitCode = await application.RunAsync(["--foo", "42", "--bar", "--baz", "xyz"]); // Assert exitCode.Should().Be(0); @@ -579,7 +576,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "-13" }, + ["--foo", "-13"], new Dictionary() ); @@ -650,10 +647,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--foo" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--foo"], new Dictionary()); // Assert exitCode.Should().NotBe(0); @@ -686,10 +680,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "--foo" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["--foo"], new Dictionary()); // Assert exitCode.Should().NotBe(0); @@ -723,7 +714,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "one", "--bar", "two" }, + ["--foo", "one", "--bar", "two"], new Dictionary() ); @@ -759,7 +750,7 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu // Act var exitCode = await application.RunAsync( - new[] { "--foo", "one", "two", "three" }, + ["--foo", "one", "two", "three"], new Dictionary() ); diff --git a/CliFx.Tests/ParameterBindingSpecs.cs b/CliFx.Tests/ParameterBindingSpecs.cs index 9832a88..01318e4 100644 --- a/CliFx.Tests/ParameterBindingSpecs.cs +++ b/CliFx.Tests/ParameterBindingSpecs.cs @@ -43,10 +43,7 @@ public class ParameterBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(tes .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "one", "two" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["one", "two"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -98,7 +95,7 @@ public class ParameterBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(tes // Act var exitCode = await application.RunAsync( - new[] { "one", "two", "three", "four", "five", "--boo", "xxx" }, + ["one", "two", "three", "four", "five", "--boo", "xxx"], new Dictionary() ); @@ -138,10 +135,7 @@ public class ParameterBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(tes .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "one" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["one"], new Dictionary()); // Assert exitCode.Should().NotBe(0); @@ -177,10 +171,7 @@ public class ParameterBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(tes .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "one" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["one"], new Dictionary()); // Assert exitCode.Should().NotBe(0); @@ -222,10 +213,7 @@ public class ParameterBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(tes .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "abc" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["abc"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -262,7 +250,7 @@ public class ParameterBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(tes // Act var exitCode = await application.RunAsync( - new[] { "one", "two", "three" }, + ["one", "two", "three"], new Dictionary() ); diff --git a/CliFx.Tests/RoutingSpecs.cs b/CliFx.Tests/RoutingSpecs.cs index 4c346f0..0a36a1e 100644 --- a/CliFx.Tests/RoutingSpecs.cs +++ b/CliFx.Tests/RoutingSpecs.cs @@ -112,10 +112,7 @@ public class RoutingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) .Build(); // Act - var exitCode = await application.RunAsync( - new[] { "cmd" }, - new Dictionary() - ); + var exitCode = await application.RunAsync(["cmd"], new Dictionary()); // Assert exitCode.Should().Be(0); @@ -170,7 +167,7 @@ public class RoutingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput) // Act var exitCode = await application.RunAsync( - new[] { "cmd", "child" }, + ["cmd", "child"], new Dictionary() ); diff --git a/CliFx.Tests/Utils/DynamicCommandBuilder.cs b/CliFx.Tests/Utils/DynamicCommandBuilder.cs index 2c03eb9..43b9c26 100644 --- a/CliFx.Tests/Utils/DynamicCommandBuilder.cs +++ b/CliFx.Tests/Utils/DynamicCommandBuilder.cs @@ -49,14 +49,14 @@ internal static class DynamicCommandBuilder // Parse the source code var ast = SyntaxFactory.ParseSyntaxTree( SourceText.From(sourceCodeWithUsings), - CSharpParseOptions.Default + CSharpParseOptions.Default.WithLanguageVersion(LanguageVersion.Preview) ); // Compile the code to IL var compilation = CSharpCompilation.Create( "CliFxTests_DynamicAssembly_" + Guid.NewGuid(), - new[] { ast }, - Net70 + [ast], + Net80 .References .All .Append(MetadataReference.CreateFromFile(typeof(ICommand).Assembly.Location)) diff --git a/CliFx/ApplicationConfiguration.cs b/CliFx/ApplicationConfiguration.cs index 1f0e4d7..5aa9842 100644 --- a/CliFx/ApplicationConfiguration.cs +++ b/CliFx/ApplicationConfiguration.cs @@ -6,34 +6,24 @@ namespace CliFx; /// /// Configuration of an application. /// -public class ApplicationConfiguration +public class ApplicationConfiguration( + IReadOnlyList commandTypes, + bool isDebugModeAllowed, + bool isPreviewModeAllowed +) { /// /// Command types defined in the application. /// - public IReadOnlyList CommandTypes { get; } + public IReadOnlyList CommandTypes { get; } = commandTypes; /// /// Whether debug mode is allowed in the application. /// - public bool IsDebugModeAllowed { get; } + public bool IsDebugModeAllowed { get; } = isDebugModeAllowed; /// /// Whether preview mode is allowed in the application. /// - public bool IsPreviewModeAllowed { get; } - - /// - /// Initializes an instance of . - /// - public ApplicationConfiguration( - IReadOnlyList commandTypes, - bool isDebugModeAllowed, - bool isPreviewModeAllowed - ) - { - CommandTypes = commandTypes; - IsDebugModeAllowed = isDebugModeAllowed; - IsPreviewModeAllowed = isPreviewModeAllowed; - } + public bool IsPreviewModeAllowed { get; } = isPreviewModeAllowed; } diff --git a/CliFx/ApplicationMetadata.cs b/CliFx/ApplicationMetadata.cs index 7c3c8ae..e074299 100644 --- a/CliFx/ApplicationMetadata.cs +++ b/CliFx/ApplicationMetadata.cs @@ -3,41 +3,30 @@ /// /// Metadata associated with an application. /// -public class ApplicationMetadata +public class ApplicationMetadata( + string title, + string executableName, + string version, + string? description +) { /// /// Application title. /// - public string Title { get; } + public string Title { get; } = title; /// /// Application executable name. /// - public string ExecutableName { get; } + public string ExecutableName { get; } = executableName; /// /// Application version. /// - public string Version { get; } + public string Version { get; } = version; /// /// Application description. /// - public string? Description { get; } - - /// - /// Initializes an instance of . - /// - public ApplicationMetadata( - string title, - string executableName, - string version, - string? description - ) - { - Title = title; - ExecutableName = executableName; - Version = version; - Description = description; - } + public string? Description { get; } = description; } diff --git a/CliFx/Attributes/CommandParameterAttribute.cs b/CliFx/Attributes/CommandParameterAttribute.cs index 3dcbc17..3314403 100644 --- a/CliFx/Attributes/CommandParameterAttribute.cs +++ b/CliFx/Attributes/CommandParameterAttribute.cs @@ -7,7 +7,7 @@ namespace CliFx.Attributes; /// Annotates a property that defines a command parameter. /// [AttributeUsage(AttributeTargets.Property)] -public sealed class CommandParameterAttribute : Attribute +public sealed class CommandParameterAttribute(int order) : Attribute { /// /// Parameter order. @@ -18,7 +18,7 @@ public sealed class CommandParameterAttribute : Attribute /// Parameter whose type is a non-scalar (e.g. array), must always be the last in order. /// Only one non-scalar parameter is allowed in a command. /// - public int Order { get; } + public int Order { get; } = order; /// /// Whether this parameter is required (default: true). @@ -62,12 +62,4 @@ public sealed class CommandParameterAttribute : Attribute /// Validators must derive from . /// public Type[] Validators { get; set; } = Array.Empty(); - - /// - /// Initializes an instance of . - /// - public CommandParameterAttribute(int order) - { - Order = order; - } } diff --git a/CliFx/CliApplication.cs b/CliFx/CliApplication.cs index ab9f6b7..061e3c3 100644 --- a/CliFx/CliApplication.cs +++ b/CliFx/CliApplication.cs @@ -17,40 +17,24 @@ namespace CliFx; /// /// Command-line application facade. /// -public class CliApplication +public class CliApplication( + ApplicationMetadata metadata, + ApplicationConfiguration configuration, + IConsole console, + ITypeActivator typeActivator +) { + private readonly CommandBinder _commandBinder = new(typeActivator); + /// /// Application metadata. /// - public ApplicationMetadata Metadata { get; } + public ApplicationMetadata Metadata { get; } = metadata; /// /// Application configuration. /// - public ApplicationConfiguration Configuration { get; } - - private readonly IConsole _console; - private readonly ITypeActivator _typeActivator; - - private readonly CommandBinder _commandBinder; - - /// - /// Initializes an instance of . - /// - public CliApplication( - ApplicationMetadata metadata, - ApplicationConfiguration configuration, - IConsole console, - ITypeActivator typeActivator - ) - { - Metadata = metadata; - Configuration = configuration; - _console = console; - _typeActivator = typeActivator; - - _commandBinder = new CommandBinder(typeActivator); - } + public ApplicationConfiguration Configuration { get; } = configuration; private bool IsDebugModeEnabled(CommandInput commandInput) => Configuration.IsDebugModeAllowed && commandInput.IsDebugDirectiveSpecified; @@ -70,9 +54,9 @@ public class CliApplication private async ValueTask PromptDebuggerAsync() { - using (_console.WithForegroundColor(ConsoleColor.Green)) + using (console.WithForegroundColor(ConsoleColor.Green)) { - _console + console .Output .WriteLine( $"Attach the debugger to process with ID {ProcessEx.GetCurrentProcessId()} to continue." @@ -93,7 +77,7 @@ public class CliApplication { // Console colors may have already been overridden by the parent process, // so we need to reset it to make sure that everything we write looks properly. - _console.ResetColor(); + console.ResetColor(); // Handle the debug directive if (IsDebugModeEnabled(commandInput)) @@ -104,7 +88,7 @@ public class CliApplication // Handle the preview directive if (IsPreviewModeEnabled(commandInput)) { - _console.Output.WriteCommandInput(commandInput); + console.Output.WriteCommandInput(commandInput); return 0; } @@ -128,7 +112,7 @@ public class CliApplication var commandInstance = commandSchema == FallbackDefaultCommand.Schema ? new FallbackDefaultCommand() // bypass the activator - : _typeActivator.CreateInstance(commandSchema.Type); + : typeActivator.CreateInstance(commandSchema.Type); // Assemble the help context var helpContext = new HelpContext( @@ -141,14 +125,14 @@ public class CliApplication // Handle the help option if (ShouldShowHelpText(commandSchema, commandInput)) { - _console.Output.WriteHelpText(helpContext); + console.Output.WriteHelpText(helpContext); return 0; } // Handle the version option if (ShouldShowVersionText(commandSchema, commandInput)) { - _console.Output.WriteLine(Metadata.Version); + console.Output.WriteLine(Metadata.Version); return 0; } @@ -160,18 +144,18 @@ public class CliApplication { // Bind and execute the command _commandBinder.Bind(commandInput, commandSchema, commandInstance); - await commandInstance.ExecuteAsync(_console); + await commandInstance.ExecuteAsync(console); return 0; } catch (CliFxException ex) { - _console.Error.WriteException(ex); + console.Error.WriteException(ex); if (ex.ShowHelp) { - _console.Output.WriteLine(); - _console.Output.WriteHelpText(helpContext); + console.Output.WriteLine(); + console.Output.WriteHelpText(helpContext); } return ex.ExitCode; @@ -211,7 +195,7 @@ public class CliApplication // developer, so we don't swallow them in that case. catch (Exception ex) when (!Debugger.IsAttached) { - _console.Error.WriteException(ex); + console.Error.WriteException(ex); return 1; } } diff --git a/CliFx/CliApplicationBuilder.cs b/CliFx/CliApplicationBuilder.cs index 5bea678..80b4671 100644 --- a/CliFx/CliApplicationBuilder.cs +++ b/CliFx/CliApplicationBuilder.cs @@ -16,7 +16,7 @@ namespace CliFx; /// public partial class CliApplicationBuilder { - private readonly HashSet _commandTypes = new(); + private readonly HashSet _commandTypes = []; private bool _isDebugModeAllowed = true; private bool _isPreviewModeAllowed = true; diff --git a/CliFx/CommandBinder.cs b/CliFx/CommandBinder.cs index b788a16..e9fdbf2 100644 --- a/CliFx/CommandBinder.cs +++ b/CliFx/CommandBinder.cs @@ -74,27 +74,24 @@ internal class CommandBinder(ITypeActivator typeActivator) } // String-constructable (FileInfo, etc) - var stringConstructor = targetType.GetConstructor(new[] { typeof(string) }); + var stringConstructor = targetType.GetConstructor([typeof(string)]); if (stringConstructor is not null) { - return stringConstructor.Invoke(new object?[] { rawValue }); + return stringConstructor.Invoke([rawValue]); } // String-parseable (with IFormatProvider) var parseMethodWithFormatProvider = targetType.TryGetStaticParseMethod(true); if (parseMethodWithFormatProvider is not null) { - return parseMethodWithFormatProvider.Invoke( - null, - new object?[] { rawValue, _formatProvider } - ); + return parseMethodWithFormatProvider.Invoke(null, [rawValue, _formatProvider]); } // String-parseable (without IFormatProvider) var parseMethod = targetType.TryGetStaticParseMethod(); if (parseMethod is not null) { - return parseMethod.Invoke(null, new object?[] { rawValue }); + return parseMethod.Invoke(null, [rawValue]); } throw CliFxException.InternalError( @@ -126,10 +123,10 @@ internal class CommandBinder(ITypeActivator typeActivator) } // Array-constructable (List, HashSet, etc) - var arrayConstructor = targetEnumerableType.GetConstructor(new[] { arrayType }); + var arrayConstructor = targetEnumerableType.GetConstructor([arrayType]); if (arrayConstructor is not null) { - return arrayConstructor.Invoke(new object?[] { array }); + return arrayConstructor.Invoke([array]); } throw CliFxException.InternalError( @@ -263,12 +260,9 @@ internal class CommandBinder(ITypeActivator typeActivator) if (parameterSchema.Property.IsScalar()) { var parameterInput = commandInput.Parameters[position]; - - var rawValues = new[] { parameterInput.Value }; - BindMember(parameterSchema, commandInstance, rawValues); + BindMember(parameterSchema, commandInstance, [parameterInput.Value]); position++; - remainingParameterInputs.Remove(parameterInput); } // Non-scalar: take all remaining inputs starting from the current position @@ -276,11 +270,13 @@ internal class CommandBinder(ITypeActivator typeActivator) { var parameterInputs = commandInput.Parameters.Skip(position).ToArray(); - var rawValues = parameterInputs.Select(p => p.Value).ToArray(); - BindMember(parameterSchema, commandInstance, rawValues); + BindMember( + parameterSchema, + commandInstance, + parameterInputs.Select(p => p.Value).ToArray() + ); position += parameterInputs.Length; - remainingParameterInputs.RemoveRange(parameterInputs); } @@ -347,7 +343,7 @@ internal class CommandBinder(ITypeActivator typeActivator) else if (environmentVariableInput is not null) { var rawValues = optionSchema.Property.IsScalar() - ? new[] { environmentVariableInput.Value } + ? [environmentVariableInput.Value] : environmentVariableInput.SplitValues(); BindMember(optionSchema, commandInstance, rawValues); diff --git a/CliFx/Exceptions/CliFxException.cs b/CliFx/Exceptions/CliFxException.cs index cb5f30c..206e18f 100644 --- a/CliFx/Exceptions/CliFxException.cs +++ b/CliFx/Exceptions/CliFxException.cs @@ -5,7 +5,12 @@ namespace CliFx.Exceptions; /// /// Exception thrown when there is an error during application execution. /// -public partial class CliFxException : Exception +public partial class CliFxException( + string message, + int exitCode = CliFxException.DefaultExitCode, + bool showHelp = false, + Exception? innerException = null +) : Exception(message, innerException) { internal const int DefaultExitCode = 1; @@ -13,33 +18,17 @@ public partial class CliFxException : Exception // provides a default message that is not very useful. // This property is used to identify whether this instance was created with // a custom message, so that we can avoid printing the default message. - internal bool HasCustomMessage { get; } + internal bool HasCustomMessage { get; } = !string.IsNullOrWhiteSpace(message); /// /// Returned exit code. /// - public int ExitCode { get; } + public int ExitCode { get; } = exitCode; /// /// Whether to show the help text before exiting. /// - public bool ShowHelp { get; } - - /// - /// Initializes an instance of . - /// - public CliFxException( - string message, - int exitCode = DefaultExitCode, - bool showHelp = false, - Exception? innerException = null - ) - : base(message, innerException) - { - HasCustomMessage = !string.IsNullOrWhiteSpace(message); - ExitCode = exitCode; - ShowHelp = showHelp; - } + public bool ShowHelp { get; } = showHelp; } public partial class CliFxException diff --git a/CliFx/Exceptions/CommandException.cs b/CliFx/Exceptions/CommandException.cs index 3d26abb..9ad6055 100644 --- a/CliFx/Exceptions/CommandException.cs +++ b/CliFx/Exceptions/CommandException.cs @@ -6,16 +6,9 @@ namespace CliFx.Exceptions; /// Exception thrown when a command cannot proceed with its normal execution due to an error. /// Use this exception to report an error to the console and return a specific exit code. /// -public class CommandException : CliFxException -{ - /// - /// Initializes an instance of . - /// - public CommandException( - string message, - int exitCode = DefaultExitCode, - bool showHelp = false, - Exception? innerException = null - ) - : base(message, exitCode, showHelp, innerException) { } -} +public class CommandException( + string message, + int exitCode = CliFxException.DefaultExitCode, + bool showHelp = false, + Exception? innerException = null +) : CliFxException(message, exitCode, showHelp, innerException); diff --git a/CliFx/Extensibility/BindingValidationError.cs b/CliFx/Extensibility/BindingValidationError.cs index 9bd0204..92b20fd 100644 --- a/CliFx/Extensibility/BindingValidationError.cs +++ b/CliFx/Extensibility/BindingValidationError.cs @@ -3,15 +3,10 @@ /// /// Represents a validation error. /// -public class BindingValidationError +public class BindingValidationError(string message) { /// /// Error message shown to the user. /// - public string Message { get; } - - /// - /// Initializes an instance of . - /// - public BindingValidationError(string message) => Message = message; + public string Message { get; } = message; } diff --git a/CliFx/Infrastructure/ConsoleReader.cs b/CliFx/Infrastructure/ConsoleReader.cs index 7d2aa5b..69f7dd2 100644 --- a/CliFx/Infrastructure/ConsoleReader.cs +++ b/CliFx/Infrastructure/ConsoleReader.cs @@ -11,28 +11,20 @@ namespace CliFx.Infrastructure; /// // Both the underlying stream AND the stream reader must be synchronized! // https://github.com/Tyrrrz/CliFx/issues/123 -public partial class ConsoleReader : StreamReader +public partial class ConsoleReader(IConsole console, Stream stream, Encoding encoding) + : StreamReader(stream, encoding, false, 4096) { - /// - /// Console that owns this stream. - /// - public IConsole Console { get; } - - /// - /// Initializes an instance of . - /// - public ConsoleReader(IConsole console, Stream stream, Encoding encoding) - : base(stream, encoding, false, 4096) - { - Console = console; - } - /// /// Initializes an instance of . /// public ConsoleReader(IConsole console, Stream stream) : this(console, stream, System.Console.InputEncoding) { } + /// + /// Console that owns this stream. + /// + public IConsole Console { get; } = console; + // The following overrides are required to establish thread-safe behavior // in methods deriving from StreamReader. diff --git a/CliFx/Infrastructure/ConsoleWriter.cs b/CliFx/Infrastructure/ConsoleWriter.cs index 444f3c1..17676b1 100644 --- a/CliFx/Infrastructure/ConsoleWriter.cs +++ b/CliFx/Infrastructure/ConsoleWriter.cs @@ -12,28 +12,20 @@ namespace CliFx.Infrastructure; /// // Both the underlying stream AND the stream writer must be synchronized! // https://github.com/Tyrrrz/CliFx/issues/123 -public partial class ConsoleWriter : StreamWriter +public partial class ConsoleWriter(IConsole console, Stream stream, Encoding encoding) + : StreamWriter(stream, encoding.WithoutPreamble(), 256) { - /// - /// Console that owns this stream. - /// - public IConsole Console { get; } - - /// - /// Initializes an instance of . - /// - public ConsoleWriter(IConsole console, Stream stream, Encoding encoding) - : base(stream, encoding.WithoutPreamble(), 256) - { - Console = console; - } - /// /// Initializes an instance of . /// public ConsoleWriter(IConsole console, Stream stream) : this(console, stream, System.Console.OutputEncoding) { } + /// + /// Console that owns this stream. + /// + public IConsole Console { get; } = console; + // The following overrides are required to establish thread-safe behavior // in methods deriving from StreamWriter. diff --git a/CliFx/Infrastructure/DelegateTypeActivator.cs b/CliFx/Infrastructure/DelegateTypeActivator.cs index 7096184..0de5be8 100644 --- a/CliFx/Infrastructure/DelegateTypeActivator.cs +++ b/CliFx/Infrastructure/DelegateTypeActivator.cs @@ -6,20 +6,12 @@ namespace CliFx.Infrastructure; /// /// Implementation of that instantiates an object by using a predefined delegate. /// -public class DelegateTypeActivator : ITypeActivator +public class DelegateTypeActivator(Func createInstance) : ITypeActivator { - private readonly Func _createInstance; - - /// - /// Initializes an instance of . - /// - public DelegateTypeActivator(Func createInstance) => - _createInstance = createInstance; - /// public object CreateInstance(Type type) { - var instance = _createInstance(type); + var instance = createInstance(type); if (instance is null) { diff --git a/CliFx/Input/CommandInput.cs b/CliFx/Input/CommandInput.cs index bd645fd..408a06e 100644 --- a/CliFx/Input/CommandInput.cs +++ b/CliFx/Input/CommandInput.cs @@ -160,7 +160,7 @@ internal partial class CommandInput result.Add(new OptionInput(lastOptionIdentifier, lastOptionValues)); lastOptionIdentifier = argument[2..]; - lastOptionValues = new List(); + lastOptionValues = []; } // Short name else if (argument.StartsWith('-') && argument.Length > 1 && char.IsLetter(argument[1])) @@ -172,7 +172,7 @@ internal partial class CommandInput result.Add(new OptionInput(lastOptionIdentifier, lastOptionValues)); lastOptionIdentifier = identifier.AsString(); - lastOptionValues = new List(); + lastOptionValues = []; } } // Value