mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
8 Commits
357426c536
...
2.3.6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2a02d39dba | ||
|
|
c40b4f3501 | ||
|
|
3fb2a2319b | ||
|
|
1a5a0374c7 | ||
|
|
078ddeaf07 | ||
|
|
c79a8c6502 | ||
|
|
cfbd8f9e76 | ||
|
|
e329f0fc78 |
@@ -9,15 +9,15 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.7.9" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.29.2" PrivateAssets="all" />
|
||||
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.8.0" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.30.6" PrivateAssets="all" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1" PrivateAssets="all" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="FluentAssertions" Version="8.2.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="all" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -13,9 +13,11 @@ using Microsoft.CodeAnalysis.Text;
|
||||
|
||||
namespace CliFx.Analyzers.Tests.Utils;
|
||||
|
||||
internal class AnalyzerAssertions(DiagnosticAnalyzer analyzer)
|
||||
: ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions>(analyzer)
|
||||
internal class AnalyzerAssertions(DiagnosticAnalyzer analyzer, AssertionChain assertionChain)
|
||||
: ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions>(analyzer, assertionChain)
|
||||
{
|
||||
private readonly AssertionChain _assertionChain = assertionChain;
|
||||
|
||||
protected override string Identifier => "analyzer";
|
||||
|
||||
private Compilation Compile(string sourceCode)
|
||||
@@ -102,8 +104,8 @@ internal class AnalyzerAssertions(DiagnosticAnalyzer analyzer)
|
||||
expectedDiagnosticIds.Intersect(producedDiagnosticIds).Count()
|
||||
== expectedDiagnosticIds.Length;
|
||||
|
||||
Execute
|
||||
.Assertion.ForCondition(isSuccessfulAssertion)
|
||||
_assertionChain
|
||||
.ForCondition(isSuccessfulAssertion)
|
||||
.FailWith(() =>
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
@@ -146,8 +148,8 @@ internal class AnalyzerAssertions(DiagnosticAnalyzer analyzer)
|
||||
var producedDiagnostics = GetProducedDiagnostics(sourceCode);
|
||||
var isSuccessfulAssertion = !producedDiagnostics.Any();
|
||||
|
||||
Execute
|
||||
.Assertion.ForCondition(isSuccessfulAssertion)
|
||||
_assertionChain
|
||||
.ForCondition(isSuccessfulAssertion)
|
||||
.FailWith(() =>
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
@@ -170,5 +172,6 @@ internal class AnalyzerAssertions(DiagnosticAnalyzer analyzer)
|
||||
|
||||
internal static class AnalyzerAssertionsExtensions
|
||||
{
|
||||
public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) => new(analyzer);
|
||||
public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) =>
|
||||
new(analyzer, AssertionChain.GetOrCreate());
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.29.2" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.30.6" PrivateAssets="all" />
|
||||
<!-- Make sure to target the lowest possible version of the compiler for wider support -->
|
||||
<PackageReference Include="Microsoft.CodeAnalysis" Version="3.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="PolyShim" Version="1.13.0" PrivateAssets="all" />
|
||||
<PackageReference Include="PolyShim" Version="1.15.0" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -11,7 +11,7 @@
|
||||
<PackageReference Include="clipr" Version="1.6.1" />
|
||||
<PackageReference Include="Cocona" Version="2.2.0" />
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.29.2" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.30.6" PrivateAssets="all" />
|
||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
|
||||
<PackageReference Include="PowerArgs" Version="4.0.3" />
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
@@ -7,8 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.29.2" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.30.6" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.29.2" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.30.6" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
@@ -9,18 +9,18 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.7.9" />
|
||||
<PackageReference Include="CliWrap" Version="3.6.7" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.29.2" PrivateAssets="all" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.1" />
|
||||
<PackageReference Include="Basic.Reference.Assemblies.Net80" Version="1.8.0" />
|
||||
<PackageReference Include="CliWrap" Version="3.8.2" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.30.6" PrivateAssets="all" />
|
||||
<PackageReference Include="FluentAssertions" Version="8.2.0" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="2.4.1" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="PolyShim" Version="1.14.0" PrivateAssets="all" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.4" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
|
||||
<PackageReference Include="PolyShim" Version="1.15.0" PrivateAssets="all" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2" PrivateAssets="all" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -34,7 +34,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_help_text_by_running_the_application_with_the_help_option()
|
||||
public async Task I_can_request_the_help_text_by_running_the_application_with_the_implicit_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
@@ -65,7 +65,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_help_text_by_running_the_application_with_the_help_option_even_if_the_default_command_is_not_defined()
|
||||
public async Task I_can_request_the_help_text_by_running_the_application_with_the_implicit_help_option_even_if_the_default_command_is_not_defined()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = DynamicCommandBuilder.CompileMany(
|
||||
@@ -102,7 +102,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_help_text_for_a_specific_command_by_running_the_application_and_specifying_its_name_with_the_help_option()
|
||||
public async Task I_can_request_the_help_text_for_a_specific_command_by_running_the_application_and_specifying_its_name_with_the_implicit_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = DynamicCommandBuilder.CompileMany(
|
||||
@@ -147,7 +147,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_help_text_for_a_specific_nested_command_by_running_the_application_and_specifying_its_name_with_the_help_option()
|
||||
public async Task I_can_request_the_help_text_for_a_specific_nested_command_by_running_the_application_and_specifying_its_name_with_the_implicit_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = DynamicCommandBuilder.CompileMany(
|
||||
@@ -476,7 +476,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_help_text_to_see_the_help_and_version_options()
|
||||
public async Task I_can_request_the_help_text_to_see_the_help_and_implicit_version_options()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
@@ -515,7 +515,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_help_text_on_a_named_command_to_see_the_help_option()
|
||||
public async Task I_can_request_the_help_text_on_a_named_command_to_see_the_implicit_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
@@ -974,7 +974,7 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_request_the_version_text_by_running_the_application_with_the_version_option()
|
||||
public async Task I_can_request_the_version_text_by_running_the_application_with_the_implicit_version_option()
|
||||
{
|
||||
// Arrange
|
||||
var application = new CliApplicationBuilder()
|
||||
@@ -992,4 +992,72 @@ public class HelpTextSpecs(ITestOutputHelper testOutput) : SpecsBase(testOutput)
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Trim().Should().Be("v6.9");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_cannot_request_the_help_text_by_running_the_application_with_the_implicit_help_option_if_there_is_an_option_with_the_same_identifier()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
// lang=csharp
|
||||
"""
|
||||
[Command]
|
||||
public class DefaultCommand : ICommand
|
||||
{
|
||||
[CommandOption("help", 'h')]
|
||||
public string? Foo { get; init; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
"""
|
||||
);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(commandType)
|
||||
.UseConsole(FakeConsole)
|
||||
.SetDescription("This will be in help text")
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(["--help"], new Dictionary<string, string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Should().NotContain("This will be in help text");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_cannot_request_the_version_text_by_running_the_application_with_the_implicit_version_option_if_there_is_an_option_with_the_same_identifier()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
// lang=csharp
|
||||
"""
|
||||
[Command]
|
||||
public class DefaultCommand : ICommand
|
||||
{
|
||||
[CommandOption("version")]
|
||||
public string? Foo { get; init; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
"""
|
||||
);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(commandType)
|
||||
.SetVersion("v6.9")
|
||||
.UseConsole(FakeConsole)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(["--version"], new Dictionary<string, string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Trim().Should().NotBe("v6.9");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -587,6 +587,86 @@ public class OptionBindingSpecs(ITestOutputHelper testOutput) : SpecsBase(testOu
|
||||
stdOut.Trim().Should().Be("-13");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_bind_an_option_to_a_property_with_the_same_identifier_as_the_implicit_help_option_and_get_the_correct_value()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
// lang=csharp
|
||||
"""
|
||||
[Command]
|
||||
public class Command : ICommand
|
||||
{
|
||||
[CommandOption("help", 'h')]
|
||||
public string? Foo { get; init; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.WriteLine(Foo);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
"""
|
||||
);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(commandType)
|
||||
.UseConsole(FakeConsole)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
["--help", "me"],
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Trim().Should().Be("me");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_bind_an_option_to_a_property_with_the_same_identifier_as_the_implicit_version_option_and_get_the_correct_value()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
// lang=csharp
|
||||
"""
|
||||
[Command]
|
||||
public class Command : ICommand
|
||||
{
|
||||
[CommandOption("version")]
|
||||
public string? Foo { get; init; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.WriteLine(Foo);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
"""
|
||||
);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(commandType)
|
||||
.UseConsole(FakeConsole)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
["--version", "1.2.0"],
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
stdOut.Trim().Should().Be("1.2.0");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task I_can_try_to_bind_a_required_option_to_a_property_and_get_an_error_if_the_user_does_not_provide_the_corresponding_argument()
|
||||
{
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Execution;
|
||||
using FluentAssertions.Primitives;
|
||||
|
||||
namespace CliFx.Tests.Utils.Extensions;
|
||||
@@ -13,7 +12,7 @@ internal static class AssertionExtensions
|
||||
params IEnumerable<string> lines
|
||||
) =>
|
||||
assertions
|
||||
.Subject.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.Subject.Split(['\n', '\r'], StringSplitOptions.RemoveEmptyEntries)
|
||||
.Should()
|
||||
.Equal(lines);
|
||||
|
||||
@@ -30,7 +29,7 @@ internal static class AssertionExtensions
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
Execute.Assertion.FailWith(
|
||||
assertions.CurrentAssertionChain.FailWith(
|
||||
$"Expected string '{assertions.Subject}' to contain '{value}' after position {lastIndex}."
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Formatting;
|
||||
@@ -43,14 +42,14 @@ public class CliApplication(
|
||||
Configuration.IsPreviewModeAllowed && commandInput.IsPreviewDirectiveSpecified;
|
||||
|
||||
private bool ShouldShowHelpText(CommandSchema commandSchema, CommandInput commandInput) =>
|
||||
commandSchema.IsHelpOptionAvailable && commandInput.IsHelpOptionSpecified
|
||||
commandSchema.IsImplicitHelpOptionAvailable && commandInput.IsHelpOptionSpecified
|
||||
||
|
||||
// Show help text also if the fallback default command is executed without any arguments
|
||||
commandSchema == FallbackDefaultCommand.Schema
|
||||
&& !commandInput.HasArguments;
|
||||
|
||||
private bool ShouldShowVersionText(CommandSchema commandSchema, CommandInput commandInput) =>
|
||||
commandSchema.IsVersionOptionAvailable && commandInput.IsVersionOptionSpecified;
|
||||
commandSchema.IsImplicitVersionOptionAvailable && commandInput.IsVersionOptionSpecified;
|
||||
|
||||
private async ValueTask PromptDebuggerAsync()
|
||||
{
|
||||
|
||||
@@ -21,10 +21,10 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.29.2" PrivateAssets="all" />
|
||||
<PackageReference Include="CSharpier.MsBuild" Version="0.30.6" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="PolyShim" Version="1.13.0" PrivateAssets="all" />
|
||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
|
||||
<PackageReference Include="PolyShim" Version="1.15.0" PrivateAssets="all" />
|
||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.6.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
|
||||
</ItemGroup>
|
||||
|
||||
<!-- Embed the analyzer inside the package -->
|
||||
|
||||
@@ -9,9 +9,9 @@ internal class OptionInput(string identifier, IReadOnlyList<string> values)
|
||||
|
||||
public IReadOnlyList<string> Values { get; } = values;
|
||||
|
||||
public bool IsHelpOption => OptionSchema.HelpOption.MatchesIdentifier(Identifier);
|
||||
public bool IsHelpOption => OptionSchema.ImplicitHelpOption.MatchesIdentifier(Identifier);
|
||||
|
||||
public bool IsVersionOption => OptionSchema.VersionOption.MatchesIdentifier(Identifier);
|
||||
public bool IsVersionOption => OptionSchema.ImplicitVersionOption.MatchesIdentifier(Identifier);
|
||||
|
||||
public string GetFormattedIdentifier() =>
|
||||
Identifier switch
|
||||
|
||||
@@ -28,9 +28,10 @@ internal partial class CommandSchema(
|
||||
|
||||
public bool IsDefault => string.IsNullOrWhiteSpace(Name);
|
||||
|
||||
public bool IsHelpOptionAvailable => Options.Contains(OptionSchema.HelpOption);
|
||||
public bool IsImplicitHelpOptionAvailable => Options.Contains(OptionSchema.ImplicitHelpOption);
|
||||
|
||||
public bool IsVersionOptionAvailable => Options.Contains(OptionSchema.VersionOption);
|
||||
public bool IsImplicitVersionOptionAvailable =>
|
||||
Options.Contains(OptionSchema.ImplicitVersionOption);
|
||||
|
||||
public bool MatchesName(string? name) =>
|
||||
!string.IsNullOrWhiteSpace(Name)
|
||||
@@ -74,10 +75,6 @@ internal partial class CommandSchema
|
||||
var name = attribute?.Name?.Trim();
|
||||
var description = attribute?.Description?.Trim();
|
||||
|
||||
var implicitOptionSchemas = string.IsNullOrWhiteSpace(name)
|
||||
? new[] { OptionSchema.HelpOption, OptionSchema.VersionOption }
|
||||
: new[] { OptionSchema.HelpOption };
|
||||
|
||||
var properties = type
|
||||
// Get properties directly on the command type
|
||||
.GetProperties()
|
||||
@@ -103,11 +100,25 @@ internal partial class CommandSchema
|
||||
.WhereNotNull()
|
||||
.ToArray();
|
||||
|
||||
var optionSchemas = properties
|
||||
.Select(OptionSchema.TryResolve)
|
||||
.WhereNotNull()
|
||||
.Concat(implicitOptionSchemas)
|
||||
.ToArray();
|
||||
var optionSchemas = properties.Select(OptionSchema.TryResolve).WhereNotNull().ToList();
|
||||
|
||||
// Include implicit options, if appropriate
|
||||
var isImplicitHelpOptionAvailable =
|
||||
// If the command implements its own help option, don't include the implicit one
|
||||
!optionSchemas.Any(o => o.MatchesShortName('h') || o.MatchesName("help"));
|
||||
|
||||
if (isImplicitHelpOptionAvailable)
|
||||
optionSchemas.Add(OptionSchema.ImplicitHelpOption);
|
||||
|
||||
var isImplicitVersionOptionAvailable =
|
||||
// Only the default command can have the version option
|
||||
string.IsNullOrWhiteSpace(name)
|
||||
&&
|
||||
// If the command implements its own version option, don't include the implicit one
|
||||
!optionSchemas.Any(o => o.MatchesName("version"));
|
||||
|
||||
if (isImplicitVersionOptionAvailable)
|
||||
optionSchemas.Add(OptionSchema.ImplicitVersionOption);
|
||||
|
||||
return new CommandSchema(type, name, description, parameterSchemas, optionSchemas);
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ internal partial class OptionSchema
|
||||
|
||||
internal partial class OptionSchema
|
||||
{
|
||||
public static OptionSchema HelpOption { get; } =
|
||||
public static OptionSchema ImplicitHelpOption { get; } =
|
||||
new(
|
||||
NullPropertyDescriptor.Instance,
|
||||
"help",
|
||||
@@ -115,7 +115,7 @@ internal partial class OptionSchema
|
||||
Array.Empty<Type>()
|
||||
);
|
||||
|
||||
public static OptionSchema VersionOption { get; } =
|
||||
public static OptionSchema ImplicitVersionOption { get; } =
|
||||
new(
|
||||
NullPropertyDescriptor.Instance,
|
||||
"version",
|
||||
|
||||
@@ -6,12 +6,11 @@ namespace CliFx.Utils;
|
||||
|
||||
internal static class EnvironmentEx
|
||||
{
|
||||
private static readonly Lazy<string?> ProcessPathLazy =
|
||||
new(() =>
|
||||
{
|
||||
using var process = Process.GetCurrentProcess();
|
||||
return process.MainModule?.FileName;
|
||||
});
|
||||
private static readonly Lazy<string?> ProcessPathLazy = new(() =>
|
||||
{
|
||||
using var process = Process.GetCurrentProcess();
|
||||
return process.MainModule?.FileName;
|
||||
});
|
||||
|
||||
public static string? ProcessPath => ProcessPathLazy.Value;
|
||||
|
||||
|
||||
@@ -38,43 +38,42 @@ internal partial class StackFrame
|
||||
private const string NotSpace = @"[^\x20\t]";
|
||||
|
||||
// Taken from https://github.com/atifaziz/StackTraceParser
|
||||
private static readonly Regex Pattern =
|
||||
new(
|
||||
$$"""
|
||||
^
|
||||
{{Space}}*
|
||||
\w+ {{Space}}+
|
||||
(?<frame>
|
||||
(?<type> {{NotSpace}}+ ) \.
|
||||
(?<method> {{NotSpace}}+? ) {{Space}}*
|
||||
(?<params> \( ( {{Space}}* \)
|
||||
| (?<pt> .+?) {{Space}}+ (?<pn> .+?)
|
||||
(, {{Space}}* (?<pt> .+?) {{Space}}+ (?<pn> .+?) )* \) ) )
|
||||
( {{Space}}+
|
||||
( # Microsoft .NET stack traces
|
||||
\w+ {{Space}}+
|
||||
(?<file> ( [a-z] \: # Windows rooted path starting with a drive letter
|
||||
| / ) # Unix rooted path starting with a forward-slash
|
||||
.+? )
|
||||
\: \w+ {{Space}}+
|
||||
(?<line> [0-9]+ ) \p{P}?
|
||||
| # Mono stack traces
|
||||
\[0x[0-9a-f]+\] {{Space}}+ \w+ {{Space}}+
|
||||
<(?<file> [^>]+ )>
|
||||
:(?<line> [0-9]+ )
|
||||
)
|
||||
)?
|
||||
)
|
||||
\s*
|
||||
$
|
||||
""",
|
||||
RegexOptions.IgnoreCase
|
||||
| RegexOptions.Multiline
|
||||
| RegexOptions.ExplicitCapture
|
||||
| RegexOptions.CultureInvariant
|
||||
| RegexOptions.IgnorePatternWhitespace,
|
||||
TimeSpan.FromSeconds(5)
|
||||
);
|
||||
private static readonly Regex Pattern = new(
|
||||
$$"""
|
||||
^
|
||||
{{Space}}*
|
||||
\w+ {{Space}}+
|
||||
(?<frame>
|
||||
(?<type> {{NotSpace}}+ ) \.
|
||||
(?<method> {{NotSpace}}+? ) {{Space}}*
|
||||
(?<params> \( ( {{Space}}* \)
|
||||
| (?<pt> .+?) {{Space}}+ (?<pn> .+?)
|
||||
(, {{Space}}* (?<pt> .+?) {{Space}}+ (?<pn> .+?) )* \) ) )
|
||||
( {{Space}}+
|
||||
( # Microsoft .NET stack traces
|
||||
\w+ {{Space}}+
|
||||
(?<file> ( [a-z] \: # Windows rooted path starting with a drive letter
|
||||
| / ) # Unix rooted path starting with a forward-slash
|
||||
.+? )
|
||||
\: \w+ {{Space}}+
|
||||
(?<line> [0-9]+ ) \p{P}?
|
||||
| # Mono stack traces
|
||||
\[0x[0-9a-f]+\] {{Space}}+ \w+ {{Space}}+
|
||||
<(?<file> [^>]+ )>
|
||||
:(?<line> [0-9]+ )
|
||||
)
|
||||
)?
|
||||
)
|
||||
\s*
|
||||
$
|
||||
""",
|
||||
RegexOptions.IgnoreCase
|
||||
| RegexOptions.Multiline
|
||||
| RegexOptions.ExplicitCapture
|
||||
| RegexOptions.CultureInvariant
|
||||
| RegexOptions.IgnorePatternWhitespace,
|
||||
TimeSpan.FromSeconds(5)
|
||||
);
|
||||
|
||||
public static IEnumerable<StackFrame> ParseTrace(string stackTrace)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user