mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d2f934310 | ||
|
|
95a00b0952 | ||
|
|
cb3fee65f3 | ||
|
|
65628b145a | ||
|
|
802bbfccc6 | ||
|
|
6e7742a4f3 | ||
|
|
f6a1a40471 | ||
|
|
33ca4da260 | ||
|
|
cbb72b16ae | ||
|
|
c58629e999 | ||
|
|
387fb72718 | ||
|
|
e04f0da318 | ||
|
|
d25873ee10 | ||
|
|
a28223fc8b | ||
|
|
1dab27de55 | ||
|
|
698629b153 | ||
|
|
65b66b0d27 | ||
|
|
7d3ba612c4 | ||
|
|
8c3b8d1f49 | ||
|
|
fdd39855ad |
8
.github/workflows/CI.yml
vendored
8
.github/workflows/CI.yml
vendored
@@ -20,10 +20,16 @@ jobs:
|
|||||||
dotnet-version: 3.1.100
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
- name: Build & test
|
- name: Build & test
|
||||||
run: dotnet test --configuration Release
|
run: dotnet test --configuration Release --logger GitHubActions
|
||||||
|
|
||||||
- name: Upload coverage
|
- name: Upload coverage
|
||||||
uses: codecov/codecov-action@v1.0.5
|
uses: codecov/codecov-action@v1.0.5
|
||||||
with:
|
with:
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
file: CliFx.Tests/bin/Release/Coverage.xml
|
file: CliFx.Tests/bin/Release/Coverage.xml
|
||||||
|
|
||||||
|
- name: Upload coverage (analyzers)
|
||||||
|
uses: codecov/codecov-action@v1.0.5
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
file: CliFx.Analyzers.Tests/bin/Release/Coverage.xml
|
||||||
|
|||||||
11
Changelog.md
11
Changelog.md
@@ -1,3 +1,14 @@
|
|||||||
|
### v1.2 (11-May-2020)
|
||||||
|
|
||||||
|
- Added built-in Roslyn analyzers that help catch incorrect usage of the library. Currently, all analyzers report issues as warnings so as to not prevent the project from building. In the future that may change.
|
||||||
|
- Added an optional parameter to `new CommandException(...)` called `showHelp` which can be used to instruct CliFx to show help for the current command after printing the error. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||||
|
- Improved help text shown for enum options and parameters by providing the list of valid values that the enum can accept. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||||
|
- Fixed an issue where it was possible to set an option without providing a value, while the option was marked as required.
|
||||||
|
- Fixed an issue where it was possible to configure an option with an empty name or a name consisting of a single character. If you want to use a single character as a name, you should set the option's short name instead.
|
||||||
|
- Added `CursorLeft` and `CursorTop` properties to `IConsole` and its implementations. In `VirtualConsole`, these are just auto-properties.
|
||||||
|
- Improved exception messages.
|
||||||
|
- Improved exceptions related to user input by also showing help text after the error message. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||||
|
|
||||||
### v1.1 (16-Mar-2020)
|
### v1.1 (16-Mar-2020)
|
||||||
|
|
||||||
- Changed `IConsole` interface (and as a result, `SystemConsole` and `VirtualConsole`) to support writing binary data. Instead of `TextReader`/`TextWriter` instances, the streams are now exposed as `StreamReader`/`StreamWriter` which provide the `BaseStream` property that allows raw access. Existing usages inside commands should remain the same because `StreamReader`/`StreamWriter` are compatible with their base classes `TextReader`/`TextWriter`, but if you were using `VirtualConsole` in tests, you may have to update it to the new API. Refer to the readme for more info.
|
- Changed `IConsole` interface (and as a result, `SystemConsole` and `VirtualConsole`) to support writing binary data. Instead of `TextReader`/`TextWriter` instances, the streams are now exposed as `StreamReader`/`StreamWriter` which provide the `BaseStream` property that allows raw access. Existing usages inside commands should remain the same because `StreamReader`/`StreamWriter` are compatible with their base classes `TextReader`/`TextWriter`, but if you were using `VirtualConsole` in tests, you may have to update it to the new API. Refer to the readme for more info.
|
||||||
|
|||||||
43
CliFx.Analyzers.Tests/AnalyzerTestCase.cs
Normal file
43
CliFx.Analyzers.Tests/AnalyzerTestCase.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers.Tests
|
||||||
|
{
|
||||||
|
public class AnalyzerTestCase
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public IReadOnlyList<DiagnosticDescriptor> TestedDiagnostics { get; }
|
||||||
|
|
||||||
|
public IReadOnlyList<string> SourceCodes { get; }
|
||||||
|
|
||||||
|
public AnalyzerTestCase(
|
||||||
|
string name,
|
||||||
|
IReadOnlyList<DiagnosticDescriptor> testedDiagnostics,
|
||||||
|
IReadOnlyList<string> sourceCodes)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
TestedDiagnostics = testedDiagnostics;
|
||||||
|
SourceCodes = sourceCodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnalyzerTestCase(
|
||||||
|
string name,
|
||||||
|
IReadOnlyList<DiagnosticDescriptor> testedDiagnostics,
|
||||||
|
string sourceCode)
|
||||||
|
: this(name, testedDiagnostics, new[] {sourceCode})
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public AnalyzerTestCase(
|
||||||
|
string name,
|
||||||
|
DiagnosticDescriptor testedDiagnostic,
|
||||||
|
string sourceCode)
|
||||||
|
: this(name, new[] {testedDiagnostic}, sourceCode)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => $"{Name} [{string.Join(", ", TestedDiagnostics.Select(d => d.Id))}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
29
CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj
Normal file
29
CliFx.Analyzers.Tests/CliFx.Analyzers.Tests.csproj
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
|
<CollectCoverage>true</CollectCoverage>
|
||||||
|
<CoverletOutputFormat>opencover</CoverletOutputFormat>
|
||||||
|
<CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Gu.Roslyn.Asserts" Version="3.3.0" />
|
||||||
|
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
|
||||||
|
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||||
|
<PackageReference Include="xunit" Version="2.4.0" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
|
||||||
|
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" />
|
||||||
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
489
CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs
Normal file
489
CliFx.Analyzers.Tests/CommandSchemaAnalyzerTests.cs
Normal file
@@ -0,0 +1,489 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Analyzers.Tests.Internal;
|
||||||
|
using Microsoft.CodeAnalysis.Diagnostics;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers.Tests
|
||||||
|
{
|
||||||
|
public class CommandSchemaAnalyzerTests
|
||||||
|
{
|
||||||
|
private static DiagnosticAnalyzer Analyzer { get; } = new CommandSchemaAnalyzer();
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> GetValidCases()
|
||||||
|
{
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Non-command type",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
public class Foo
|
||||||
|
{
|
||||||
|
public int Bar { get; set; } = 5;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Command implements interface and has attribute",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Command doesn't have an attribute but is an abstract type",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
public abstract class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Parameters with unique order",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(15)]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Parameters with unique names",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13, Name = ""foo"")]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(15, Name = ""bar"")]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Single non-scalar parameter",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(2)]
|
||||||
|
public HashSet<string> ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Non-scalar parameter is last in order",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(2)]
|
||||||
|
public IReadOnlyList<string> ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Option with a proper name",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(""foo"")]
|
||||||
|
public string Param { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Option with a proper name and short name",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(""foo"", 'f')]
|
||||||
|
public string Param { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Options with unique names",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(""foo"")]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(""bar"")]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Options with unique short names",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('f')]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Options with unique environment variable names",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('a', EnvironmentVariableName = ""env_var_a"")]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('b', EnvironmentVariableName = ""env_var_b"")]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> GetInvalidCases()
|
||||||
|
{
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Command is missing the attribute",
|
||||||
|
DiagnosticDescriptors.CliFx0002,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Command doesn't implement the interface",
|
||||||
|
DiagnosticDescriptors.CliFx0001,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Parameters with duplicate order",
|
||||||
|
DiagnosticDescriptors.CliFx0021,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Parameters with duplicate names",
|
||||||
|
DiagnosticDescriptors.CliFx0022,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13, Name = ""foo"")]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(15, Name = ""foo"")]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Multiple non-scalar parameters",
|
||||||
|
DiagnosticDescriptors.CliFx0023,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public IReadOnlyList<string> ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(2)]
|
||||||
|
public HashSet<string> ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Non-last non-scalar parameter",
|
||||||
|
DiagnosticDescriptors.CliFx0024,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public IReadOnlyList<string> ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(2)]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Option with an empty name",
|
||||||
|
DiagnosticDescriptors.CliFx0041,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("""")]
|
||||||
|
public string Param { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Option with a name which is too short",
|
||||||
|
DiagnosticDescriptors.CliFx0042,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(""a"")]
|
||||||
|
public string Param { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Options with duplicate names",
|
||||||
|
DiagnosticDescriptors.CliFx0043,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(""foo"")]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(""foo"")]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Options with duplicate short names",
|
||||||
|
DiagnosticDescriptors.CliFx0044,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('f')]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('f')]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Options with duplicate environment variable names",
|
||||||
|
DiagnosticDescriptors.CliFx0045,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('a', EnvironmentVariableName = ""env_var"")]
|
||||||
|
public string ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('b', EnvironmentVariableName = ""env_var"")]
|
||||||
|
public string ParamB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(GetValidCases))]
|
||||||
|
public void Valid(AnalyzerTestCase testCase) =>
|
||||||
|
Analyzer.Should().NotProduceDiagnostics(testCase);
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(GetInvalidCases))]
|
||||||
|
public void Invalid(AnalyzerTestCase testCase) =>
|
||||||
|
Analyzer.Should().ProduceDiagnostics(testCase);
|
||||||
|
}
|
||||||
|
}
|
||||||
144
CliFx.Analyzers.Tests/ConsoleUsageAnalyzerTests.cs
Normal file
144
CliFx.Analyzers.Tests/ConsoleUsageAnalyzerTests.cs
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using CliFx.Analyzers.Tests.Internal;
|
||||||
|
using Microsoft.CodeAnalysis.Diagnostics;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers.Tests
|
||||||
|
{
|
||||||
|
public class ConsoleUsageAnalyzerTests
|
||||||
|
{
|
||||||
|
private static DiagnosticAnalyzer Analyzer { get; } = new ConsoleUsageAnalyzer();
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> GetValidCases()
|
||||||
|
{
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Using console abstraction",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(""Hello world"");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Console abstraction is not available in scope",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public void SomeOtherMethod() => Console.WriteLine(""Test"");
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<object[]> GetInvalidCases()
|
||||||
|
{
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Not using available console abstraction in the ExecuteAsync method",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
Console.WriteLine(""Hello world"");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Not using available console abstraction in the ExecuteAsync method when writing stderr",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
Console.Error.WriteLine(""Hello world"");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Not using available console abstraction while referencing System.Console by full name",
|
||||||
|
Analyzer.SupportedDiagnostics,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
System.Console.Error.WriteLine(""Hello world"");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
yield return new object[]
|
||||||
|
{
|
||||||
|
new AnalyzerTestCase(
|
||||||
|
"Not using available console abstraction in another method",
|
||||||
|
DiagnosticDescriptors.CliFx0100,
|
||||||
|
|
||||||
|
// language=cs
|
||||||
|
@"
|
||||||
|
[Command]
|
||||||
|
public class MyCommand : ICommand
|
||||||
|
{
|
||||||
|
public void SomeOtherMethod(IConsole console) => Console.WriteLine(""Test"");
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}"
|
||||||
|
)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(GetValidCases))]
|
||||||
|
public void Valid(AnalyzerTestCase testCase) =>
|
||||||
|
Analyzer.Should().NotProduceDiagnostics(testCase);
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(GetInvalidCases))]
|
||||||
|
public void Invalid(AnalyzerTestCase testCase) =>
|
||||||
|
Analyzer.Should().ProduceDiagnostics(testCase);
|
||||||
|
}
|
||||||
|
}
|
||||||
107
CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs
Normal file
107
CliFx.Analyzers.Tests/Internal/AnalyzerAssertions.cs
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using FluentAssertions.Execution;
|
||||||
|
using FluentAssertions.Primitives;
|
||||||
|
using Gu.Roslyn.Asserts;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
|
using Microsoft.CodeAnalysis.Diagnostics;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers.Tests.Internal
|
||||||
|
{
|
||||||
|
internal partial class AnalyzerAssertions : ReferenceTypeAssertions<DiagnosticAnalyzer, AnalyzerAssertions>
|
||||||
|
{
|
||||||
|
protected override string Identifier { get; } = "analyzer";
|
||||||
|
|
||||||
|
public AnalyzerAssertions(DiagnosticAnalyzer analyzer)
|
||||||
|
: base(analyzer)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ProduceDiagnostics(
|
||||||
|
IReadOnlyList<DiagnosticDescriptor> diagnostics,
|
||||||
|
IReadOnlyList<string> sourceCodes)
|
||||||
|
{
|
||||||
|
var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes);
|
||||||
|
|
||||||
|
var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||||
|
var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||||
|
|
||||||
|
var result = expectedIds.Intersect(producedIds).Count() == expectedIds.Length;
|
||||||
|
|
||||||
|
Execute.Assertion.ForCondition(result).FailWith($@"
|
||||||
|
Expected and produced diagnostics do not match.
|
||||||
|
|
||||||
|
Expected: {string.Join(", ", expectedIds)}
|
||||||
|
Produced: {(producedIds.Any() ? string.Join(", ", producedIds) : "<none>")}
|
||||||
|
".Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ProduceDiagnostics(AnalyzerTestCase testCase) =>
|
||||||
|
ProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes);
|
||||||
|
|
||||||
|
public void NotProduceDiagnostics(
|
||||||
|
IReadOnlyList<DiagnosticDescriptor> diagnostics,
|
||||||
|
IReadOnlyList<string> sourceCodes)
|
||||||
|
{
|
||||||
|
var producedDiagnostics = GetProducedDiagnostics(Subject, sourceCodes);
|
||||||
|
|
||||||
|
var expectedIds = diagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||||
|
var producedIds = producedDiagnostics.Select(d => d.Id).Distinct().OrderBy(d => d).ToArray();
|
||||||
|
|
||||||
|
var result = !expectedIds.Intersect(producedIds).Any();
|
||||||
|
|
||||||
|
Execute.Assertion.ForCondition(result).FailWith($@"
|
||||||
|
Expected no produced diagnostics.
|
||||||
|
|
||||||
|
Produced: {string.Join(", ", producedIds)}
|
||||||
|
".Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void NotProduceDiagnostics(AnalyzerTestCase testCase) =>
|
||||||
|
NotProduceDiagnostics(testCase.TestedDiagnostics, testCase.SourceCodes);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class AnalyzerAssertions
|
||||||
|
{
|
||||||
|
private static IReadOnlyList<MetadataReference> DefaultMetadataReferences { get; } =
|
||||||
|
MetadataReferences.Transitive(typeof(CliApplication).Assembly).ToArray();
|
||||||
|
|
||||||
|
private static string WrapCodeWithUsingDirectives(string code)
|
||||||
|
{
|
||||||
|
var usingDirectives = new[]
|
||||||
|
{
|
||||||
|
"using System;",
|
||||||
|
"using System.Collections.Generic;",
|
||||||
|
"using System.Threading.Tasks;",
|
||||||
|
"using CliFx;",
|
||||||
|
"using CliFx.Attributes;",
|
||||||
|
"using CliFx.Exceptions;",
|
||||||
|
"using CliFx.Utilities;"
|
||||||
|
};
|
||||||
|
|
||||||
|
return
|
||||||
|
string.Join(Environment.NewLine, usingDirectives) +
|
||||||
|
Environment.NewLine +
|
||||||
|
code;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IReadOnlyList<Diagnostic> GetProducedDiagnostics(
|
||||||
|
DiagnosticAnalyzer analyzer,
|
||||||
|
IReadOnlyList<string> sourceCodes)
|
||||||
|
{
|
||||||
|
var compilationOptions = new CSharpCompilationOptions(OutputKind.ConsoleApplication);
|
||||||
|
var wrappedSourceCodes = sourceCodes.Select(WrapCodeWithUsingDirectives).ToArray();
|
||||||
|
|
||||||
|
return Analyze.GetDiagnostics(analyzer, wrappedSourceCodes, compilationOptions, DefaultMetadataReferences)
|
||||||
|
.SelectMany(d => d)
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static class AnalyzerAssertionsExtensions
|
||||||
|
{
|
||||||
|
public static AnalyzerAssertions Should(this DiagnosticAnalyzer analyzer) => new AnalyzerAssertions(analyzer);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
CliFx.Analyzers/CliFx.Analyzers.csproj
Normal file
13
CliFx.Analyzers/CliFx.Analyzers.csproj
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
<Nullable>annotations</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" PrivateAssets="all" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
297
CliFx.Analyzers/CommandSchemaAnalyzer.cs
Normal file
297
CliFx.Analyzers/CommandSchemaAnalyzer.cs
Normal file
@@ -0,0 +1,297 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.Diagnostics;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers
|
||||||
|
{
|
||||||
|
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||||
|
public class CommandSchemaAnalyzer : DiagnosticAnalyzer
|
||||||
|
{
|
||||||
|
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
|
||||||
|
DiagnosticDescriptors.CliFx0001,
|
||||||
|
DiagnosticDescriptors.CliFx0002,
|
||||||
|
DiagnosticDescriptors.CliFx0021,
|
||||||
|
DiagnosticDescriptors.CliFx0022,
|
||||||
|
DiagnosticDescriptors.CliFx0023,
|
||||||
|
DiagnosticDescriptors.CliFx0024,
|
||||||
|
DiagnosticDescriptors.CliFx0041,
|
||||||
|
DiagnosticDescriptors.CliFx0042,
|
||||||
|
DiagnosticDescriptors.CliFx0043,
|
||||||
|
DiagnosticDescriptors.CliFx0044,
|
||||||
|
DiagnosticDescriptors.CliFx0045
|
||||||
|
);
|
||||||
|
|
||||||
|
private static bool IsScalarType(ITypeSymbol typeSymbol) =>
|
||||||
|
KnownSymbols.IsSystemString(typeSymbol) ||
|
||||||
|
!typeSymbol.AllInterfaces.Select(i => i.ConstructedFrom).Any(KnownSymbols.IsSystemCollectionsGenericIEnumerable);
|
||||||
|
|
||||||
|
private static void CheckCommandParameterProperties(
|
||||||
|
SymbolAnalysisContext context,
|
||||||
|
IReadOnlyList<IPropertySymbol> properties)
|
||||||
|
{
|
||||||
|
var parameters = properties
|
||||||
|
.Select(p =>
|
||||||
|
{
|
||||||
|
var attribute = p
|
||||||
|
.GetAttributes()
|
||||||
|
.First(a => KnownSymbols.IsCommandParameterAttribute(a.AttributeClass));
|
||||||
|
|
||||||
|
var order = attribute
|
||||||
|
.ConstructorArguments
|
||||||
|
.Select(a => a.Value)
|
||||||
|
.FirstOrDefault() as int?;
|
||||||
|
|
||||||
|
var name = attribute
|
||||||
|
.NamedArguments
|
||||||
|
.Where(a => a.Key == "Name")
|
||||||
|
.Select(a => a.Value.Value)
|
||||||
|
.FirstOrDefault() as string;
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
Property = p,
|
||||||
|
Order = order,
|
||||||
|
Name = name
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
// Duplicate order
|
||||||
|
var duplicateOrderParameters = parameters
|
||||||
|
.Where(p => p.Order != null)
|
||||||
|
.GroupBy(p => p.Order)
|
||||||
|
.Where(g => g.Count() > 1)
|
||||||
|
.SelectMany(g => g.AsEnumerable())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var parameter in duplicateOrderParameters)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0021, parameter.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duplicate name
|
||||||
|
var duplicateNameParameters = parameters
|
||||||
|
.Where(p => !string.IsNullOrWhiteSpace(p.Name))
|
||||||
|
.GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.Where(g => g.Count() > 1)
|
||||||
|
.SelectMany(g => g.AsEnumerable())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var parameter in duplicateNameParameters)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0022, parameter.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiple non-scalar
|
||||||
|
var nonScalarParameters = parameters
|
||||||
|
.Where(p => !IsScalarType(p.Property.Type))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (nonScalarParameters.Length > 1)
|
||||||
|
{
|
||||||
|
foreach (var parameter in nonScalarParameters)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0023, parameter.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Non-last non-scalar
|
||||||
|
var nonLastNonScalarParameter = parameters
|
||||||
|
.OrderByDescending(a => a.Order)
|
||||||
|
.Skip(1)
|
||||||
|
.LastOrDefault(p => !IsScalarType(p.Property.Type));
|
||||||
|
|
||||||
|
if (nonLastNonScalarParameter != null)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0024, nonLastNonScalarParameter.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CheckCommandOptionProperties(
|
||||||
|
SymbolAnalysisContext context,
|
||||||
|
IReadOnlyList<IPropertySymbol> properties)
|
||||||
|
{
|
||||||
|
var options = properties
|
||||||
|
.Select(p =>
|
||||||
|
{
|
||||||
|
var attribute = p
|
||||||
|
.GetAttributes()
|
||||||
|
.First(a => KnownSymbols.IsCommandOptionAttribute(a.AttributeClass));
|
||||||
|
|
||||||
|
var name = attribute
|
||||||
|
.ConstructorArguments
|
||||||
|
.Where(a => KnownSymbols.IsSystemString(a.Type))
|
||||||
|
.Select(a => a.Value)
|
||||||
|
.FirstOrDefault() as string;
|
||||||
|
|
||||||
|
var shortName = attribute
|
||||||
|
.ConstructorArguments
|
||||||
|
.Where(a => KnownSymbols.IsSystemChar(a.Type))
|
||||||
|
.Select(a => a.Value)
|
||||||
|
.FirstOrDefault() as char?;
|
||||||
|
|
||||||
|
var envVarName = attribute
|
||||||
|
.NamedArguments
|
||||||
|
.Where(a => a.Key == "EnvironmentVariableName")
|
||||||
|
.Select(a => a.Value.Value)
|
||||||
|
.FirstOrDefault() as string;
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
Property = p,
|
||||||
|
Name = name,
|
||||||
|
ShortName = shortName,
|
||||||
|
EnvironmentVariableName = envVarName
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
// No name
|
||||||
|
var noNameOptions = options
|
||||||
|
.Where(o => string.IsNullOrWhiteSpace(o.Name) && o.ShortName == null)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var option in noNameOptions)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0041, option.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Too short name
|
||||||
|
var invalidNameLengthOptions = options
|
||||||
|
.Where(o => !string.IsNullOrWhiteSpace(o.Name) && o.Name.Length <= 1)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var option in invalidNameLengthOptions)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0042, option.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duplicate name
|
||||||
|
var duplicateNameOptions = options
|
||||||
|
.Where(p => !string.IsNullOrWhiteSpace(p.Name))
|
||||||
|
.GroupBy(p => p.Name, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.Where(g => g.Count() > 1)
|
||||||
|
.SelectMany(g => g.AsEnumerable())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var option in duplicateNameOptions)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0043, option.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duplicate name
|
||||||
|
var duplicateShortNameOptions = options
|
||||||
|
.Where(p => p.ShortName != null)
|
||||||
|
.GroupBy(p => p.ShortName)
|
||||||
|
.Where(g => g.Count() > 1)
|
||||||
|
.SelectMany(g => g.AsEnumerable())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var option in duplicateShortNameOptions)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0044, option.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Duplicate environment variable name
|
||||||
|
var duplicateEnvironmentVariableNameOptions = options
|
||||||
|
.Where(p => !string.IsNullOrWhiteSpace(p.EnvironmentVariableName))
|
||||||
|
.GroupBy(p => p.EnvironmentVariableName, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.Where(g => g.Count() > 1)
|
||||||
|
.SelectMany(g => g.AsEnumerable())
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
foreach (var option in duplicateEnvironmentVariableNameOptions)
|
||||||
|
{
|
||||||
|
context.ReportDiagnostic(
|
||||||
|
Diagnostic.Create(DiagnosticDescriptors.CliFx0045, option.Property.Locations.First()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CheckCommandType(SymbolAnalysisContext context)
|
||||||
|
{
|
||||||
|
// Named type: MyCommand
|
||||||
|
if (!(context.Symbol is INamedTypeSymbol namedTypeSymbol))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Only classes
|
||||||
|
if (namedTypeSymbol.TypeKind != TypeKind.Class)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Implements ICommand?
|
||||||
|
var implementsCommandInterface = namedTypeSymbol
|
||||||
|
.AllInterfaces
|
||||||
|
.Any(KnownSymbols.IsCommandInterface);
|
||||||
|
|
||||||
|
// Has CommandAttribute?
|
||||||
|
var hasCommandAttribute = namedTypeSymbol
|
||||||
|
.GetAttributes()
|
||||||
|
.Select(a => a.AttributeClass)
|
||||||
|
.Any(KnownSymbols.IsCommandAttribute);
|
||||||
|
|
||||||
|
var isValidCommandType =
|
||||||
|
// implements interface
|
||||||
|
implementsCommandInterface && (
|
||||||
|
// and either abstract class or has attribute
|
||||||
|
namedTypeSymbol.IsAbstract || hasCommandAttribute
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!isValidCommandType)
|
||||||
|
{
|
||||||
|
// See if this was meant to be a command type (either interface or attribute present)
|
||||||
|
var isAlmostValidCommandType = implementsCommandInterface ^ hasCommandAttribute;
|
||||||
|
|
||||||
|
if (isAlmostValidCommandType && !implementsCommandInterface)
|
||||||
|
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0001, namedTypeSymbol.Locations.First()));
|
||||||
|
|
||||||
|
if (isAlmostValidCommandType && !hasCommandAttribute)
|
||||||
|
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0002, namedTypeSymbol.Locations.First()));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var properties = namedTypeSymbol
|
||||||
|
.GetMembers()
|
||||||
|
.Where(m => m.Kind == SymbolKind.Property)
|
||||||
|
.OfType<IPropertySymbol>().ToArray();
|
||||||
|
|
||||||
|
// Check parameters
|
||||||
|
var parameterProperties = properties
|
||||||
|
.Where(p => p
|
||||||
|
.GetAttributes()
|
||||||
|
.Select(a => a.AttributeClass)
|
||||||
|
.Any(KnownSymbols.IsCommandParameterAttribute))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
CheckCommandParameterProperties(context, parameterProperties);
|
||||||
|
|
||||||
|
// Check options
|
||||||
|
var optionsProperties = properties
|
||||||
|
.Where(p => p
|
||||||
|
.GetAttributes()
|
||||||
|
.Select(a => a.AttributeClass)
|
||||||
|
.Any(KnownSymbols.IsCommandOptionAttribute))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
CheckCommandOptionProperties(context, optionsProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Initialize(AnalysisContext context)
|
||||||
|
{
|
||||||
|
context.EnableConcurrentExecution();
|
||||||
|
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||||
|
|
||||||
|
context.RegisterSymbolAction(CheckCommandType, SymbolKind.NamedType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
80
CliFx.Analyzers/ConsoleUsageAnalyzer.cs
Normal file
80
CliFx.Analyzers/ConsoleUsageAnalyzer.cs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
using System.Collections.Immutable;
|
||||||
|
using System.Linq;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||||
|
using Microsoft.CodeAnalysis.Diagnostics;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers
|
||||||
|
{
|
||||||
|
[DiagnosticAnalyzer(LanguageNames.CSharp)]
|
||||||
|
public class ConsoleUsageAnalyzer : DiagnosticAnalyzer
|
||||||
|
{
|
||||||
|
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(
|
||||||
|
DiagnosticDescriptors.CliFx0100
|
||||||
|
);
|
||||||
|
|
||||||
|
private static bool IsSystemConsoleInvocation(
|
||||||
|
SyntaxNodeAnalysisContext context,
|
||||||
|
InvocationExpressionSyntax invocationSyntax)
|
||||||
|
{
|
||||||
|
// Get the method member access (Console.WriteLine or Console.Error.WriteLine)
|
||||||
|
if (!(invocationSyntax.Expression is MemberAccessExpressionSyntax memberAccessSyntax))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Get the semantic model for the invoked method
|
||||||
|
if (!(context.SemanticModel.GetSymbolInfo(memberAccessSyntax).Symbol is IMethodSymbol methodSymbol))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Check if contained within System.Console
|
||||||
|
if (KnownSymbols.IsSystemConsole(methodSymbol.ContainingType))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
// In case with Console.Error.WriteLine that wouldn't work, we need to check parent member access too
|
||||||
|
if (!(memberAccessSyntax.Expression is MemberAccessExpressionSyntax parentMemberAccessSyntax))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Get the semantic model for the parent member
|
||||||
|
if (!(context.SemanticModel.GetSymbolInfo(parentMemberAccessSyntax).Symbol is IPropertySymbol propertySymbol))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// Check if contained within System.Console
|
||||||
|
if (KnownSymbols.IsSystemConsole(propertySymbol.ContainingType))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void CheckSystemConsoleUsage(SyntaxNodeAnalysisContext context)
|
||||||
|
{
|
||||||
|
if (!(context.Node is InvocationExpressionSyntax invocationSyntax))
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (!IsSystemConsoleInvocation(context, invocationSyntax))
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Check if IConsole is available in the scope as a viable alternative
|
||||||
|
var isConsoleInterfaceAvailable = invocationSyntax
|
||||||
|
.Ancestors()
|
||||||
|
.OfType<MethodDeclarationSyntax>()
|
||||||
|
.SelectMany(m => m.ParameterList.Parameters)
|
||||||
|
.Select(p => p.Type)
|
||||||
|
.Select(t => context.SemanticModel.GetSymbolInfo(t).Symbol)
|
||||||
|
.Where(s => s != null)
|
||||||
|
.Any(KnownSymbols.IsConsoleInterface!);
|
||||||
|
|
||||||
|
if (!isConsoleInterfaceAvailable)
|
||||||
|
return;
|
||||||
|
|
||||||
|
context.ReportDiagnostic(Diagnostic.Create(DiagnosticDescriptors.CliFx0100, invocationSyntax.GetLocation()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Initialize(AnalysisContext context)
|
||||||
|
{
|
||||||
|
context.EnableConcurrentExecution();
|
||||||
|
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
|
||||||
|
|
||||||
|
context.RegisterSyntaxNodeAction(CheckSystemConsoleUsage, SyntaxKind.InvocationExpression);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
79
CliFx.Analyzers/DiagnosticDescriptors.cs
Normal file
79
CliFx.Analyzers/DiagnosticDescriptors.cs
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers
|
||||||
|
{
|
||||||
|
public static class DiagnosticDescriptors
|
||||||
|
{
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0001 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0001),
|
||||||
|
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
|
||||||
|
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0002 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0002),
|
||||||
|
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
|
||||||
|
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0021 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0021),
|
||||||
|
"Parameter order must be unique within its command",
|
||||||
|
"Parameter order must be unique within its command",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0022 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0022),
|
||||||
|
"Parameter order must have unique name within its command",
|
||||||
|
"Parameter order must have unique name within its command",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0023 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0023),
|
||||||
|
"Only one non-scalar parameter per command is allowed",
|
||||||
|
"Only one non-scalar parameter per command is allowed",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0024 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0024),
|
||||||
|
"Non-scalar parameter must be last in order",
|
||||||
|
"Non-scalar parameter must be last in order",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0041 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0041),
|
||||||
|
"Option must have a name or short name specified",
|
||||||
|
"Option must have a name or short name specified",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0042 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0042),
|
||||||
|
"Option name must be at least 2 characters long",
|
||||||
|
"Option name must be at least 2 characters long",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0043 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0043),
|
||||||
|
"Option name must be unique within its command",
|
||||||
|
"Option name must be unique within its command",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0044 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0044),
|
||||||
|
"Option short name must be unique within its command",
|
||||||
|
"Option short name must be unique within its command",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0045 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0045),
|
||||||
|
"Option environment variable name must be unique within its command",
|
||||||
|
"Option environment variable name must be unique within its command",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
|
||||||
|
public static readonly DiagnosticDescriptor CliFx0100 =
|
||||||
|
new DiagnosticDescriptor(nameof(CliFx0100),
|
||||||
|
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
|
||||||
|
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
|
||||||
|
"Usage", DiagnosticSeverity.Warning, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
CliFx.Analyzers/Internal/RoslynExtensions.cs
Normal file
11
CliFx.Analyzers/Internal/RoslynExtensions.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers.Internal
|
||||||
|
{
|
||||||
|
internal static class RoslynExtensions
|
||||||
|
{
|
||||||
|
public static bool DisplayNameMatches(this ISymbol symbol, string name) =>
|
||||||
|
string.Equals(symbol.ToDisplayString(), name, StringComparison.Ordinal);
|
||||||
|
}
|
||||||
|
}
|
||||||
37
CliFx.Analyzers/KnownSymbols.cs
Normal file
37
CliFx.Analyzers/KnownSymbols.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using CliFx.Analyzers.Internal;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace CliFx.Analyzers
|
||||||
|
{
|
||||||
|
public static class KnownSymbols
|
||||||
|
{
|
||||||
|
public static bool IsSystemString(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("string") ||
|
||||||
|
symbol.DisplayNameMatches("System.String");
|
||||||
|
|
||||||
|
public static bool IsSystemChar(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("char") ||
|
||||||
|
symbol.DisplayNameMatches("System.Char");
|
||||||
|
|
||||||
|
public static bool IsSystemCollectionsGenericIEnumerable(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("System.Collections.Generic.IEnumerable<T>");
|
||||||
|
|
||||||
|
public static bool IsSystemConsole(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("System.Console");
|
||||||
|
|
||||||
|
public static bool IsConsoleInterface(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("CliFx.IConsole");
|
||||||
|
|
||||||
|
public static bool IsCommandInterface(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("CliFx.ICommand");
|
||||||
|
|
||||||
|
public static bool IsCommandAttribute(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("CliFx.Attributes.CommandAttribute");
|
||||||
|
|
||||||
|
public static bool IsCommandParameterAttribute(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("CliFx.Attributes.CommandParameterAttribute");
|
||||||
|
|
||||||
|
public static bool IsCommandOptionAttribute(ISymbol symbol) =>
|
||||||
|
symbol.DisplayNameMatches("CliFx.Attributes.CommandOptionAttribute");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,12 +7,13 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
|
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
|
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Dummy.Commands
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Demos how to show an error message then help text from an organizational command.
|
||||||
|
/// </summary>
|
||||||
|
[Command("cmd-err", Description = "This is an organizational command. " +
|
||||||
|
"I don't do anything except provide a route to my subcommands. " +
|
||||||
|
"If you use just me, I print an error message then the help text " +
|
||||||
|
"to remind you of my subcommands.")]
|
||||||
|
public class ShowErrorMessageThenHelpTextOnCommandExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) =>
|
||||||
|
throw new CommandException("It is an error to use me without a subcommand. " +
|
||||||
|
"Please refer to the help text below for guidance.", showHelp: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
18
CliFx.Tests.Dummy/Commands/ShowHelpTextOnErrorCommand.cs
Normal file
18
CliFx.Tests.Dummy/Commands/ShowHelpTextOnErrorCommand.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Dummy.Commands
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Demos how to show help text from an organizational command.
|
||||||
|
/// </summary>
|
||||||
|
[Command("cmd", Description = "This is an organizational command. " +
|
||||||
|
"I don't do anything except provide a route to my subcommands. " +
|
||||||
|
"If you use just me, I print the help text to remind you of my subcommands.")]
|
||||||
|
public class ShowHelpTextOnCommandExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) =>
|
||||||
|
throw new CommandException(null, showHelp: false);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -76,6 +76,24 @@ namespace CliFx.Tests
|
|||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class EmptyOptionNameCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("")]
|
||||||
|
public string? Apples { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class SingleCharacterOptionNameCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("a")]
|
||||||
|
public string? Apples { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
[Command]
|
[Command]
|
||||||
private class DuplicateOptionNamesCommand : ICommand
|
private class DuplicateOptionNamesCommand : ICommand
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -4,11 +4,16 @@ using CliFx.Domain;
|
|||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class ApplicationSpecs
|
public partial class ApplicationSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public ApplicationSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Application_can_be_created_with_a_default_configuration()
|
public void Application_can_be_created_with_a_default_configuration()
|
||||||
{
|
{
|
||||||
@@ -38,7 +43,7 @@ namespace CliFx.Tests
|
|||||||
.UseVersionText("test")
|
.UseVersionText("test")
|
||||||
.UseDescription("test")
|
.UseDescription("test")
|
||||||
.UseConsole(new VirtualConsole(Stream.Null))
|
.UseConsole(new VirtualConsole(Stream.Null))
|
||||||
.UseTypeActivator(Activator.CreateInstance)
|
.UseTypeActivator(Activator.CreateInstance!)
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
@@ -52,7 +57,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = Array.Empty<Type>();
|
var commandTypes = Array.Empty<Type>();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -62,7 +68,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -72,7 +79,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -82,7 +90,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -92,7 +101,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -102,7 +112,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -112,7 +123,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
|
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -122,7 +134,30 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
|
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_must_have_names_that_are_not_empty()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(EmptyOptionNameCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_must_have_names_that_are_longer_than_one_character()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(SingleCharacterOptionNameCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -132,7 +167,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -142,7 +178,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -152,7 +189,8 @@ namespace CliFx.Tests
|
|||||||
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -174,7 +212,7 @@ namespace CliFx.Tests
|
|||||||
new[]
|
new[]
|
||||||
{
|
{
|
||||||
new CommandParameterSchema(
|
new CommandParameterSchema(
|
||||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter)),
|
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter))!,
|
||||||
13,
|
13,
|
||||||
"param",
|
"param",
|
||||||
"Param description")
|
"Param description")
|
||||||
@@ -182,7 +220,7 @@ namespace CliFx.Tests
|
|||||||
new[]
|
new[]
|
||||||
{
|
{
|
||||||
new CommandOptionSchema(
|
new CommandOptionSchema(
|
||||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option)),
|
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option))!,
|
||||||
"option",
|
"option",
|
||||||
'o',
|
'o',
|
||||||
"ENV",
|
"ENV",
|
||||||
|
|||||||
@@ -17,20 +17,14 @@ namespace CliFx.Tests
|
|||||||
{
|
{
|
||||||
public string Value { get; }
|
public string Value { get; }
|
||||||
|
|
||||||
public StringConstructable(string value)
|
public StringConstructable(string value) => Value = value;
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class StringParseable
|
private class StringParseable
|
||||||
{
|
{
|
||||||
public string Value { get; }
|
public string Value { get; }
|
||||||
|
|
||||||
private StringParseable(string value)
|
private StringParseable(string value) => Value = value;
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static StringParseable Parse(string value) => new StringParseable(value);
|
public static StringParseable Parse(string value) => new StringParseable(value);
|
||||||
}
|
}
|
||||||
@@ -39,10 +33,7 @@ namespace CliFx.Tests
|
|||||||
{
|
{
|
||||||
public string Value { get; }
|
public string Value { get; }
|
||||||
|
|
||||||
private StringParseableWithFormatProvider(string value)
|
private StringParseableWithFormatProvider(string value) => Value = value;
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||||
new StringParseableWithFormatProvider(value + " " + formatProvider);
|
new StringParseableWithFormatProvider(value + " " + formatProvider);
|
||||||
@@ -54,9 +45,7 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
public class CustomEnumerable<T> : IEnumerable<T>
|
public class CustomEnumerable<T> : IEnumerable<T>
|
||||||
{
|
{
|
||||||
private readonly T[] _arr = new T[0];
|
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _arr).GetEnumerator();
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,16 @@ using CliFx.Domain;
|
|||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class ArgumentBindingSpecs
|
public partial class ArgumentBindingSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public ArgumentBindingSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Property_of_type_object_is_bound_directly_from_the_argument_value()
|
public void Property_of_type_object_is_bound_directly_from_the_argument_value()
|
||||||
{
|
{
|
||||||
@@ -933,7 +938,7 @@ namespace CliFx.Tests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Property_annotated_as_a_required_option_must_always_be_bound_to_some_value()
|
public void Property_annotated_as_a_required_option_must_always_be_set()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var schema = ApplicationSchema.Resolve(new[] {typeof(RequiredOptionCommand)});
|
var schema = ApplicationSchema.Resolve(new[] {typeof(RequiredOptionCommand)});
|
||||||
@@ -943,7 +948,23 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Property_annotated_as_a_required_option_must_always_be_bound_to_some_value()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var schema = ApplicationSchema.Resolve(new[] {typeof(RequiredOptionCommand)});
|
||||||
|
|
||||||
|
var input = new CommandLineInputBuilder()
|
||||||
|
.AddOption(nameof(RequiredOptionCommand.OptionB))
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -982,7 +1003,8 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -996,7 +1018,8 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -1010,7 +1033,8 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -1024,7 +1048,8 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -1038,7 +1063,8 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -1053,7 +1079,8 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -1070,7 +1097,8 @@ namespace CliFx.Tests
|
|||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
var ex = Assert.Throws<CliFxException>(() => schema.InitializeEntryPoint(input));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,10 +17,11 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CliWrap" Version="3.0.0" />
|
<PackageReference Include="CliWrap" Version="3.0.0" />
|
||||||
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
||||||
|
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||||
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
|
||||||
<PackageReference Include="xunit" Version="2.4.1" />
|
<PackageReference Include="xunit" Version="2.4.1" />
|
||||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||||
|
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class DependencyInjectionSpecs
|
public partial class DependencyInjectionSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public DependencyInjectionSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Default_type_activator_can_initialize_a_command_if_it_has_a_parameterless_constructor()
|
public void Default_type_activator_can_initialize_a_command_if_it_has_a_parameterless_constructor()
|
||||||
{
|
{
|
||||||
@@ -26,8 +31,8 @@ namespace CliFx.Tests
|
|||||||
var activator = new DefaultTypeActivator();
|
var activator = new DefaultTypeActivator();
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() =>
|
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||||
activator.CreateInstance(typeof(WithDependenciesCommand)));
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -48,11 +53,11 @@ namespace CliFx.Tests
|
|||||||
public void Delegate_type_activator_throws_if_the_underlying_function_returns_null()
|
public void Delegate_type_activator_throws_if_the_underlying_function_returns_null()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var activator = new DelegateTypeActivator(_ => null);
|
var activator = new DelegateTypeActivator(_ => null!);
|
||||||
|
|
||||||
// Act & assert
|
// Act & assert
|
||||||
Assert.Throws<CliFxException>(() =>
|
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||||
activator.CreateInstance(typeof(WithDependenciesCommand)));
|
_output.WriteLine(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -27,5 +27,51 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
|
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Command("exc")]
|
||||||
|
private class ShowHelpTextOnlyCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(null, showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("exc sub")]
|
||||||
|
private class ShowHelpTextOnlySubCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("exc")]
|
||||||
|
private class ShowErrorMessageThenHelpTextCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) =>
|
||||||
|
throw new CommandException("Error message.", showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("exc sub")]
|
||||||
|
private class ShowErrorMessageThenHelpTextSubCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("exc")]
|
||||||
|
private class StackTraceOnlyCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("msg", 'm')]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("inv")]
|
||||||
|
private class InvalidUserInputCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("required", 'r')]
|
||||||
|
public string? RequiredOption { get; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,13 +3,18 @@ using System.IO;
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class ErrorReportingSpecs
|
public partial class ErrorReportingSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public ErrorReportingSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_full_error_details()
|
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
await using var stdErr = new MemoryStream();
|
await using var stdErr = new MemoryStream();
|
||||||
@@ -29,8 +34,12 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().NotBe(0);
|
exitCode.Should().NotBe(0);
|
||||||
stdErrData.Should().Contain("Kaput");
|
stdErrData.Should().ContainAll(
|
||||||
stdErrData.Length.Should().BeGreaterThan("Kaput".Length);
|
"System.Exception:",
|
||||||
|
"Kaput", "at",
|
||||||
|
"CliFx.Tests");
|
||||||
|
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -55,6 +64,8 @@ namespace CliFx.Tests
|
|||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().Be(69);
|
exitCode.Should().Be(69);
|
||||||
stdErrData.Should().Be("Kaput");
|
stdErrData.Should().Be("Kaput");
|
||||||
|
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -79,6 +90,149 @@ namespace CliFx.Tests
|
|||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().NotBe(0);
|
exitCode.Should().NotBe(0);
|
||||||
stdErrData.Should().NotBeEmpty();
|
stdErrData.Should().NotBeEmpty();
|
||||||
|
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_which_shows_only_the_help_text()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ShowHelpTextOnlyCommand))
|
||||||
|
.AddCommand(typeof(ShowHelpTextOnlySubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"exc"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
var stdErrData = console.Output.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdErrData.Should().BeEmpty();
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"[command]",
|
||||||
|
"Options",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"Commands",
|
||||||
|
"sub",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_specialized_exception_which_shows_the_error_message_then_the_help_text()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ShowErrorMessageThenHelpTextCommand))
|
||||||
|
.AddCommand(typeof(ShowErrorMessageThenHelpTextSubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"exc"});
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdErrData.Should().Be("Error message.");
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"[command]",
|
||||||
|
"Options",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"Commands",
|
||||||
|
"sub",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_which_shows_only_a_stack_trace_and_no_help_text()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(GenericExceptionCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"exc", "-m", "Kaput"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErrData.Should().ContainAll(
|
||||||
|
"System.Exception:",
|
||||||
|
"Kaput", "at",
|
||||||
|
"CliFx.Tests");
|
||||||
|
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_shows_help_text_on_exceptions_related_to_invalid_user_input()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(InvalidUserInputCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"not-a-valid-command", "-r", "foo"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErrData.Should().ContainAll(
|
||||||
|
"Can't find a command that matches the following arguments:",
|
||||||
|
"not-a-valid-command"
|
||||||
|
);
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"[command]",
|
||||||
|
"Options",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"Commands",
|
||||||
|
"inv",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
@@ -81,6 +82,23 @@ namespace CliFx.Tests
|
|||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Command("cmd-with-enum-args")]
|
||||||
|
private class EnumArgumentsCommand : ICommand
|
||||||
|
{
|
||||||
|
public enum TestEnum { Value1, Value2, Value3 };
|
||||||
|
|
||||||
|
[CommandParameter(0, Name = "value", Description = "Enum parameter.")]
|
||||||
|
public TestEnum ParamA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("value", Description = "Enum option.", IsRequired = true)]
|
||||||
|
public TestEnum OptionA { get; set; } = TestEnum.Value1;
|
||||||
|
|
||||||
|
[CommandOption("nullable-value", Description = "Nullable enum option.")]
|
||||||
|
public TestEnum? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
[Command("cmd-with-env-vars")]
|
[Command("cmd-with-env-vars")]
|
||||||
private class EnvironmentVariableCommand : ICommand
|
private class EnvironmentVariableCommand : ICommand
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,11 +2,16 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public partial class HelpTextSpecs
|
public partial class HelpTextSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
|
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
|
||||||
{
|
{
|
||||||
@@ -29,6 +34,8 @@ namespace CliFx.Tests
|
|||||||
// Assert
|
// Assert
|
||||||
exitCode.Should().Be(0);
|
exitCode.Should().Be(0);
|
||||||
stdOutData.Should().Be("v6.9");
|
stdOutData.Should().Be("v6.9");
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -68,6 +75,8 @@ namespace CliFx.Tests
|
|||||||
"cmd", "NamedCommand description.",
|
"cmd", "NamedCommand description.",
|
||||||
"You can run", "to show help on a specific command."
|
"You can run", "to show help on a specific command."
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -104,6 +113,8 @@ namespace CliFx.Tests
|
|||||||
"sub", "SubCommand description.",
|
"sub", "SubCommand description.",
|
||||||
"You can run", "to show help on a specific command."
|
"You can run", "to show help on a specific command."
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -137,6 +148,8 @@ namespace CliFx.Tests
|
|||||||
"-e|--option-e", "OptionE description.",
|
"-e|--option-e", "OptionE description.",
|
||||||
"-h|--help", "Shows help text."
|
"-h|--help", "Shows help text."
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -167,6 +180,8 @@ namespace CliFx.Tests
|
|||||||
"cmd", "NamedCommand description.",
|
"cmd", "NamedCommand description.",
|
||||||
"You can run", "to show help on a specific command."
|
"You can run", "to show help on a specific command."
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -190,6 +205,8 @@ namespace CliFx.Tests
|
|||||||
"Usage",
|
"Usage",
|
||||||
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -217,6 +234,38 @@ namespace CliFx.Tests
|
|||||||
"* -g|--option-g",
|
"* -g|--option-g",
|
||||||
"-h|--option-h"
|
"-h|--option-h"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_all_valid_values_for_enum_arguments()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(EnumArgumentsCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] { "cmd-with-enum-args", "--help" });
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"cmd-with-enum-args", "[options]",
|
||||||
|
"Parameters",
|
||||||
|
"value", "Valid values: Value1, Value2, Value3.",
|
||||||
|
"Options",
|
||||||
|
"* --value", "Enum option.", "Valid values: Value1, Value2, Value3.",
|
||||||
|
"--nullable-value", "Nullable enum option.", "Valid values: Value1, Value2, Value3."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -241,6 +290,8 @@ namespace CliFx.Tests
|
|||||||
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
||||||
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ namespace CliFx.Tests
|
|||||||
private class ConcatCommand : ICommand
|
private class ConcatCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
||||||
public IReadOnlyList<string> Inputs { get; set; }
|
public IReadOnlyList<string> Inputs { get; set; } = Array.Empty<string>();
|
||||||
|
|
||||||
[CommandOption('s', Description = "String separator.")]
|
[CommandOption('s', Description = "String separator.")]
|
||||||
public string Separator { get; set; } = "";
|
public string Separator { get; set; } = "";
|
||||||
@@ -36,10 +37,10 @@ namespace CliFx.Tests
|
|||||||
private class DivideCommand : ICommand
|
private class DivideCommand : ICommand
|
||||||
{
|
{
|
||||||
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
||||||
public double Dividend { get; set; }
|
public double Dividend { get; set; } = 0;
|
||||||
|
|
||||||
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
||||||
public double Divisor { get; set; }
|
public double Divisor { get; set; } = 0;
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console)
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,11 +3,16 @@ using System.Linq;
|
|||||||
using CliFx.Utilities;
|
using CliFx.Utilities;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
namespace CliFx.Tests
|
namespace CliFx.Tests
|
||||||
{
|
{
|
||||||
public class UtilitiesSpecs
|
public class UtilitiesSpecs
|
||||||
{
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public UtilitiesSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Progress_ticker_can_be_used_to_report_progress_to_console()
|
public void Progress_ticker_can_be_used_to_report_progress_to_console()
|
||||||
{
|
{
|
||||||
@@ -28,6 +33,8 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOutData.Should().ContainAll(progressStringValues);
|
stdOutData.Should().ContainAll(progressStringValues);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -49,6 +56,8 @@ namespace CliFx.Tests
|
|||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
stdOutData.Should().BeEmpty();
|
stdOutData.Should().BeEmpty();
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project>
|
<Project>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>1.1</Version>
|
<Version>1.2</Version>
|
||||||
<Company>Tyrrrz</Company>
|
<Company>Tyrrrz</Company>
|
||||||
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
|
|||||||
32
CliFx.sln
32
CliFx.sln
@@ -10,16 +10,20 @@ EndProject
|
|||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3AAE8166-BB8E-49DA-844C-3A0EE6BD40A0}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3AAE8166-BB8E-49DA-844C-3A0EE6BD40A0}"
|
||||||
ProjectSection(SolutionItems) = preProject
|
ProjectSection(SolutionItems) = preProject
|
||||||
Changelog.md = Changelog.md
|
Changelog.md = Changelog.md
|
||||||
|
CliFx.props = CliFx.props
|
||||||
License.txt = License.txt
|
License.txt = License.txt
|
||||||
Readme.md = Readme.md
|
Readme.md = Readme.md
|
||||||
CliFx.props = CliFx.props
|
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Benchmarks", "CliFx.Benchmarks\CliFx.Benchmarks.csproj", "{8ACD6DC2-D768-4850-9223-5B7C83A78513}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Benchmarks", "CliFx.Benchmarks\CliFx.Benchmarks.csproj", "{8ACD6DC2-D768-4850-9223-5B7C83A78513}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Demo", "CliFx.Demo\CliFx.Demo.csproj", "{AAB6844C-BF71-448F-A11B-89AEE459AB15}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Demo", "CliFx.Demo\CliFx.Demo.csproj", "{AAB6844C-BF71-448F-A11B-89AEE459AB15}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Analyzers", "CliFx.Analyzers\CliFx.Analyzers.csproj", "{F8460D69-F8CF-405C-A6ED-BED02A21DB42}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Analyzers.Tests", "CliFx.Analyzers.Tests\CliFx.Analyzers.Tests.csproj", "{49878E75-2097-4C79-9151-B98A28FBB973}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -91,6 +95,30 @@ Global
|
|||||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x64.Build.0 = Release|Any CPU
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.ActiveCfg = Release|Any CPU
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.Build.0 = Release|Any CPU
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{F8460D69-F8CF-405C-A6ED-BED02A21DB42}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{49878E75-2097-4C79-9151-B98A28FBB973}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ namespace CliFx.Attributes
|
|||||||
public class CommandOptionAttribute : Attribute
|
public class CommandOptionAttribute : Attribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Option name.
|
/// Option name (must be longer than a single character).
|
||||||
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
|
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
|
||||||
/// All options in a command must have different names (comparison is not case-sensitive).
|
/// All options in a command must have different names (comparison is not case-sensitive).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string? Name { get; }
|
public string? Name { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Option short name.
|
/// Option short name (single character).
|
||||||
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
|
/// Either <see cref="Name"/> or <see cref="ShortName"/> must be set.
|
||||||
/// All options in a command must have different short names (comparison is case-sensitive).
|
/// All options in a command must have different short names (comparison is case-sensitive).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -143,6 +143,32 @@ namespace CliFx
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handle <see cref="CommandException"/>s differently from the rest because we want to
|
||||||
|
/// display it different based on whether we are showing the help text or not.
|
||||||
|
/// </summary>
|
||||||
|
private int HandleCliFxException(IReadOnlyList<string> commandLineArguments, CliFxException cfe)
|
||||||
|
{
|
||||||
|
var showHelp = cfe.ShowHelp;
|
||||||
|
|
||||||
|
var errorMessage = cfe.HasMessage
|
||||||
|
? cfe.Message
|
||||||
|
: cfe.ToString();
|
||||||
|
|
||||||
|
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
|
||||||
|
|
||||||
|
if (showHelp)
|
||||||
|
{
|
||||||
|
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
|
||||||
|
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
|
||||||
|
var commandSchema = applicationSchema.TryFindCommand(commandLineInput) ??
|
||||||
|
CommandSchema.StubDefaultCommand;
|
||||||
|
_helpTextWriter.Write(applicationSchema, commandSchema);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfe.ExitCode;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -162,21 +188,18 @@ namespace CliFx
|
|||||||
HandleHelpOption(applicationSchema, commandLineInput) ??
|
HandleHelpOption(applicationSchema, commandLineInput) ??
|
||||||
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (CliFxException cfe)
|
||||||
{
|
{
|
||||||
// We want to catch exceptions in order to print errors and return correct exit codes.
|
// We want to catch exceptions in order to print errors and return correct exit codes.
|
||||||
// Doing this also gets rid of the annoying Windows troubleshooting dialog that shows up on unhandled exceptions.
|
// Doing this also gets rid of the annoying Windows troubleshooting dialog that shows up on unhandled exceptions.
|
||||||
|
var exitCode = HandleCliFxException(commandLineArguments, cfe);
|
||||||
// Prefer showing message without stack trace on exceptions coming from CliFx or on CommandException
|
return exitCode;
|
||||||
var errorMessage = !string.IsNullOrWhiteSpace(ex.Message) && (ex is CliFxException || ex is CommandException)
|
}
|
||||||
? ex.Message
|
catch (Exception ex)
|
||||||
: ex.ToString();
|
{
|
||||||
|
// For all other errors, we just write the entire thing to stderr.
|
||||||
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
|
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(ex.ToString()));
|
||||||
|
return ex.HResult;
|
||||||
return ex is CommandException commandException
|
|
||||||
? commandException.ExitCode
|
|
||||||
: ex.HResult;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -173,10 +173,10 @@ namespace CliFx
|
|||||||
|
|
||||||
public partial class CliApplicationBuilder
|
public partial class CliApplicationBuilder
|
||||||
{
|
{
|
||||||
private static readonly Lazy<Assembly> LazyEntryAssembly = new Lazy<Assembly>(Assembly.GetEntryAssembly);
|
private static readonly Lazy<Assembly?> LazyEntryAssembly = new Lazy<Assembly?>(Assembly.GetEntryAssembly);
|
||||||
|
|
||||||
// Entry assembly is null in tests
|
// Entry assembly is null in tests
|
||||||
private static Assembly EntryAssembly => LazyEntryAssembly.Value;
|
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
|
||||||
|
|
||||||
private static string? GetDefaultTitle() => EntryAssembly?.GetName().Name;
|
private static string? GetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||||
|
|
||||||
@@ -184,14 +184,12 @@ namespace CliFx
|
|||||||
{
|
{
|
||||||
var entryAssemblyLocation = EntryAssembly?.Location;
|
var entryAssemblyLocation = EntryAssembly?.Location;
|
||||||
|
|
||||||
// If it's a .dll assembly, prepend 'dotnet' and keep the file extension
|
// The assembly can be an executable or a dll, depending on how it was packaged
|
||||||
if (string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase))
|
var isDll = string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase);
|
||||||
{
|
|
||||||
return "dotnet " + Path.GetFileName(entryAssemblyLocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise just use assembly file name without extension
|
return isDll
|
||||||
return Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
? "dotnet " + Path.GetFileName(entryAssemblyLocation)
|
||||||
|
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string? GetDefaultVersionText() =>
|
private static string? GetDefaultVersionText() =>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<Import Project="../CliFx.props" />
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>netstandard2.1;netstandard2.0;net45</TargetFrameworks>
|
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
|
||||||
<Authors>$(Company)</Authors>
|
<Authors>$(Company)</Authors>
|
||||||
<Description>Declarative framework for CLI applications</Description>
|
<Description>Declarative framework for CLI applications</Description>
|
||||||
<PackageTags>command line executable interface framework parser arguments net core</PackageTags>
|
<PackageTags>command line executable interface framework parser arguments net core</PackageTags>
|
||||||
@@ -10,12 +10,17 @@
|
|||||||
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
|
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
|
||||||
<PackageIcon>favicon.png</PackageIcon>
|
<PackageIcon>favicon.png</PackageIcon>
|
||||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||||
<PublishRepositoryUrl>True</PublishRepositoryUrl>
|
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||||
<EmbedUntrackedSources>True</EmbedUntrackedSources>
|
<IncludeSymbols>true</IncludeSymbols>
|
||||||
<IncludeSymbols>True</IncludeSymbols>
|
|
||||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
|
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);CopyAnalyzerToPackage</TargetsForTfmSpecificContentInPackage>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- Disable nullability warnings on older frameworks because there is no nullability info for BCL -->
|
||||||
|
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
|
<Nullable>annotations</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -27,18 +32,29 @@
|
|||||||
</AssemblyAttribute>
|
</AssemblyAttribute>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<None Include="../favicon.png" Pack="True" PackagePath="" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="all" />
|
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="all" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net45'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
|
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="../favicon.png" Pack="true" PackagePath="" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- The following item group and target ensure that the analyzer project is copied into the output NuGet package -->
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="true" IncludeAssets="CliFx.Analyzers.dll" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<Target Name="CopyAnalyzerToPackage">
|
||||||
|
<ItemGroup>
|
||||||
|
<TfmSpecificPackageFile Include="$(OutDir)/CliFx.Analyzers.dll" PackagePath="analyzers/dotnet/cs" BuildAction="none" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Target>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -18,11 +18,7 @@ namespace CliFx
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.DefaultActivatorFailed(type, ex);
|
||||||
.Append($"Failed to create an instance of {type.FullName}.").Append(" ")
|
|
||||||
.AppendLine("The type must have a public parameter-less constructor in order to be instantiated by the default activator.")
|
|
||||||
.Append($"To supply a custom activator (for example when using dependency injection), call {nameof(CliApplicationBuilder)}.{nameof(CliApplicationBuilder.UseTypeActivator)}(...).")
|
|
||||||
.ToString(), ex);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,10 +18,6 @@ namespace CliFx
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public object CreateInstance(Type type) =>
|
public object CreateInstance(Type type) =>
|
||||||
_func(type) ?? throw new CliFxException(new StringBuilder()
|
_func(type) ?? throw CliFxException.DelegateActivatorReceivedNull(type);
|
||||||
.Append($"Failed to create an instance of type {type.FullName}, received <null> instead.").Append(" ")
|
|
||||||
.Append("Make sure that the provided type activator was configured correctly.").Append(" ")
|
|
||||||
.Append("If you are using a dependency container, make sure that this type is registered.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using CliFx.Internal;
|
|
||||||
|
|
||||||
namespace CliFx.Domain
|
namespace CliFx.Domain
|
||||||
{
|
{
|
||||||
@@ -71,18 +68,14 @@ namespace CliFx.Domain
|
|||||||
IReadOnlyDictionary<string, string> environmentVariables,
|
IReadOnlyDictionary<string, string> environmentVariables,
|
||||||
ITypeActivator activator)
|
ITypeActivator activator)
|
||||||
{
|
{
|
||||||
var command = TryFindCommand(commandLineInput, out var argumentOffset);
|
var command = TryFindCommand(commandLineInput, out var argumentOffset) ??
|
||||||
if (command == null)
|
throw CliFxException.CannotFindMatchingCommand(commandLineInput);
|
||||||
{
|
|
||||||
throw new CliFxException(
|
|
||||||
$"Can't find a command that matches arguments [{string.Join(" ", commandLineInput.UnboundArguments)}].");
|
|
||||||
}
|
|
||||||
|
|
||||||
var parameterValues = argumentOffset == 0
|
var parameterInputs = argumentOffset == 0
|
||||||
? commandLineInput.UnboundArguments.Select(a => a.Value).ToArray()
|
? commandLineInput.UnboundArguments.ToArray()
|
||||||
: commandLineInput.UnboundArguments.Skip(argumentOffset).Select(a => a.Value).ToArray();
|
: commandLineInput.UnboundArguments.Skip(argumentOffset).ToArray();
|
||||||
|
|
||||||
return command.CreateInstance(parameterValues, commandLineInput.Options, environmentVariables, activator);
|
return command.CreateInstance(parameterInputs, commandLineInput.Options, environmentVariables, activator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICommand InitializeEntryPoint(
|
public ICommand InitializeEntryPoint(
|
||||||
@@ -106,28 +99,23 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
if (duplicateOrderGroup != null)
|
if (duplicateOrderGroup != null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.CommandParametersDuplicateOrder(
|
||||||
.AppendLine($"Command {command.Type.FullName} contains two or more parameters that have the same order ({duplicateOrderGroup.Key}):")
|
command,
|
||||||
.AppendBulletList(duplicateOrderGroup.Select(o => o.Property.Name))
|
duplicateOrderGroup.Key,
|
||||||
.AppendLine()
|
duplicateOrderGroup.ToArray());
|
||||||
.Append("Parameters in a command must all have unique order.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var duplicateNameGroup = command.Parameters
|
var duplicateNameGroup = command.Parameters
|
||||||
.Where(a => !string.IsNullOrWhiteSpace(a.Name))
|
.Where(a => !string.IsNullOrWhiteSpace(a.Name))
|
||||||
.GroupBy(a => a.Name, StringComparer.OrdinalIgnoreCase)
|
.GroupBy(a => a.Name!, StringComparer.OrdinalIgnoreCase)
|
||||||
.FirstOrDefault(g => g.Count() > 1);
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
if (duplicateNameGroup != null)
|
if (duplicateNameGroup != null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.CommandParametersDuplicateName(
|
||||||
.AppendLine($"Command {command.Type.FullName} contains two or more parameters that have the same name ({duplicateNameGroup.Key}):")
|
command,
|
||||||
.AppendBulletList(duplicateNameGroup.Select(o => o.Property.Name))
|
duplicateNameGroup.Key,
|
||||||
.AppendLine()
|
duplicateNameGroup.ToArray());
|
||||||
.Append("Parameters in a command must all have unique names.").Append(" ")
|
|
||||||
.Append("Comparison is NOT case-sensitive.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var nonScalarParameters = command.Parameters
|
var nonScalarParameters = command.Parameters
|
||||||
@@ -136,13 +124,9 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
if (nonScalarParameters.Length > 1)
|
if (nonScalarParameters.Length > 1)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.CommandParametersTooManyNonScalar(
|
||||||
.AppendLine($"Command [{command.Type.FullName}] contains two or more parameters of an enumerable type:")
|
command,
|
||||||
.AppendBulletList(nonScalarParameters.Select(o => o.Property.Name))
|
nonScalarParameters);
|
||||||
.AppendLine()
|
|
||||||
.AppendLine("There can only be one parameter of an enumerable type in a command.")
|
|
||||||
.Append("Note, the string type is not considered enumerable in this context.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var nonLastNonScalarParameter = command.Parameters
|
var nonLastNonScalarParameter = command.Parameters
|
||||||
@@ -152,63 +136,74 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
if (nonLastNonScalarParameter != null)
|
if (nonLastNonScalarParameter != null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.CommandParametersNonLastNonScalar(
|
||||||
.AppendLine($"Command {command.Type.FullName} contains a parameter of an enumerable type which doesn't appear last in order:")
|
command,
|
||||||
.AppendLine($"- {nonLastNonScalarParameter.Property.Name}")
|
nonLastNonScalarParameter);
|
||||||
.AppendLine()
|
|
||||||
.Append("Parameter of an enumerable type must always come last to avoid ambiguity.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ValidateOptions(CommandSchema command)
|
private static void ValidateOptions(CommandSchema command)
|
||||||
{
|
{
|
||||||
|
var noNameGroup = command.Options
|
||||||
|
.Where(o => o.ShortName == null && string.IsNullOrWhiteSpace(o.Name))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (noNameGroup.Any())
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandOptionsNoName(
|
||||||
|
command,
|
||||||
|
noNameGroup.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
var invalidLengthNameGroup = command.Options
|
||||||
|
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
||||||
|
.Where(o => o.Name!.Length <= 1)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (invalidLengthNameGroup.Any())
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandOptionsInvalidLengthName(
|
||||||
|
command,
|
||||||
|
invalidLengthNameGroup);
|
||||||
|
}
|
||||||
|
|
||||||
var duplicateNameGroup = command.Options
|
var duplicateNameGroup = command.Options
|
||||||
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
||||||
.GroupBy(o => o.Name, StringComparer.OrdinalIgnoreCase)
|
.GroupBy(o => o.Name!, StringComparer.OrdinalIgnoreCase)
|
||||||
.FirstOrDefault(g => g.Count() > 1);
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
if (duplicateNameGroup != null)
|
if (duplicateNameGroup != null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.CommandOptionsDuplicateName(
|
||||||
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same name ({duplicateNameGroup.Key}):")
|
command,
|
||||||
.AppendBulletList(duplicateNameGroup.Select(o => o.Property.Name))
|
duplicateNameGroup.Key,
|
||||||
.AppendLine()
|
duplicateNameGroup.ToArray());
|
||||||
.Append("Options in a command must all have unique names.").Append(" ")
|
|
||||||
.Append("Comparison is NOT case-sensitive.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var duplicateShortNameGroup = command.Options
|
var duplicateShortNameGroup = command.Options
|
||||||
.Where(o => o.ShortName != null)
|
.Where(o => o.ShortName != null)
|
||||||
.GroupBy(o => o.ShortName)
|
.GroupBy(o => o.ShortName!.Value)
|
||||||
.FirstOrDefault(g => g.Count() > 1);
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
if (duplicateShortNameGroup != null)
|
if (duplicateShortNameGroup != null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.CommandOptionsDuplicateShortName(
|
||||||
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same short name ({duplicateShortNameGroup.Key}):")
|
command,
|
||||||
.AppendBulletList(duplicateShortNameGroup.Select(o => o.Property.Name))
|
duplicateShortNameGroup.Key,
|
||||||
.AppendLine()
|
duplicateShortNameGroup.ToArray());
|
||||||
.Append("Options in a command must all have unique short names.").Append(" ")
|
|
||||||
.Append("Comparison is case-sensitive.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var duplicateEnvironmentVariableNameGroup = command.Options
|
var duplicateEnvironmentVariableNameGroup = command.Options
|
||||||
.Where(o => !string.IsNullOrWhiteSpace(o.EnvironmentVariableName))
|
.Where(o => !string.IsNullOrWhiteSpace(o.EnvironmentVariableName))
|
||||||
.GroupBy(o => o.EnvironmentVariableName, StringComparer.OrdinalIgnoreCase)
|
.GroupBy(o => o.EnvironmentVariableName!, StringComparer.OrdinalIgnoreCase)
|
||||||
.FirstOrDefault(g => g.Count() > 1);
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
if (duplicateEnvironmentVariableNameGroup != null)
|
if (duplicateEnvironmentVariableNameGroup != null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.CommandOptionsDuplicateEnvironmentVariableName(
|
||||||
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same environment variable name ({duplicateEnvironmentVariableNameGroup.Key}):")
|
command,
|
||||||
.AppendBulletList(duplicateEnvironmentVariableNameGroup.Select(o => o.Property.Name))
|
duplicateEnvironmentVariableNameGroup.Key,
|
||||||
.AppendLine()
|
duplicateEnvironmentVariableNameGroup.ToArray());
|
||||||
.Append("Options in a command must all have unique environment variable names.").Append(" ")
|
|
||||||
.Append("Comparison is NOT case-sensitive.")
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,7 +211,7 @@ namespace CliFx.Domain
|
|||||||
{
|
{
|
||||||
if (!commands.Any())
|
if (!commands.Any())
|
||||||
{
|
{
|
||||||
throw new CliFxException("There are no commands configured for this application.");
|
throw CliFxException.CommandsNotRegistered();
|
||||||
}
|
}
|
||||||
|
|
||||||
var duplicateNameGroup = commands
|
var duplicateNameGroup = commands
|
||||||
@@ -225,13 +220,12 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
if (duplicateNameGroup != null)
|
if (duplicateNameGroup != null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
if (!string.IsNullOrWhiteSpace(duplicateNameGroup.Key))
|
||||||
.AppendLine($"Application contains two or more commands that have the same name ({duplicateNameGroup.Key}):")
|
throw CliFxException.CommandsDuplicateName(
|
||||||
.AppendBulletList(duplicateNameGroup.Select(o => o.Type.FullName))
|
duplicateNameGroup.Key,
|
||||||
.AppendLine()
|
duplicateNameGroup.ToArray());
|
||||||
.Append("Commands must all have unique names. Likewise, there must not be more than one command without a name.").Append(" ")
|
|
||||||
.Append("Comparison is NOT case-sensitive.")
|
throw CliFxException.CommandsTooManyDefaults(duplicateNameGroup.ToArray());
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,17 +235,8 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
foreach (var commandType in commandTypes)
|
foreach (var commandType in commandTypes)
|
||||||
{
|
{
|
||||||
var command = CommandSchema.TryResolve(commandType);
|
var command = CommandSchema.TryResolve(commandType) ??
|
||||||
if (command == null)
|
throw CliFxException.InvalidCommandType(commandType);
|
||||||
{
|
|
||||||
throw new CliFxException(new StringBuilder()
|
|
||||||
.Append($"Command {commandType.FullName} is not a valid command type.").Append(" ")
|
|
||||||
.AppendLine("In order to be a valid command type it must:")
|
|
||||||
.AppendLine($" - Be annotated with {typeof(CommandAttribute).FullName}")
|
|
||||||
.AppendLine($" - Implement {typeof(ICommand).FullName}")
|
|
||||||
.AppendLine(" - Not be an abstract class")
|
|
||||||
.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
ValidateParameters(command);
|
ValidateParameters(command);
|
||||||
ValidateOptions(command);
|
ValidateOptions(command);
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
|
||||||
using CliFx.Exceptions;
|
using CliFx.Exceptions;
|
||||||
using CliFx.Internal;
|
using CliFx.Internal;
|
||||||
|
|
||||||
@@ -15,7 +14,9 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public string? Description { get; }
|
public string? Description { get; }
|
||||||
|
|
||||||
public bool IsScalar => GetEnumerableArgumentUnderlyingType() == null;
|
public abstract string DisplayName { get; }
|
||||||
|
|
||||||
|
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == null;
|
||||||
|
|
||||||
protected CommandArgumentSchema(PropertyInfo property, string? description)
|
protected CommandArgumentSchema(PropertyInfo property, string? description)
|
||||||
{
|
{
|
||||||
@@ -23,28 +24,85 @@ namespace CliFx.Domain
|
|||||||
Description = description;
|
Description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Type? GetEnumerableArgumentUnderlyingType() =>
|
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
||||||
Property.PropertyType != typeof(string)
|
Property.PropertyType != typeof(string)
|
||||||
? Property.PropertyType.GetEnumerableUnderlyingType()
|
? Property.PropertyType.GetEnumerableUnderlyingType()
|
||||||
: null;
|
: null;
|
||||||
|
|
||||||
private object Convert(IReadOnlyList<string> values)
|
private object? ConvertScalar(string? value, Type targetType)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Primitive
|
||||||
|
var primitiveConverter = PrimitiveConverters.GetValueOrDefault(targetType);
|
||||||
|
if (primitiveConverter != null)
|
||||||
|
return primitiveConverter(value);
|
||||||
|
|
||||||
|
// Enum
|
||||||
|
if (targetType.IsEnum)
|
||||||
|
return Enum.Parse(targetType, value, true);
|
||||||
|
|
||||||
|
// Nullable
|
||||||
|
var nullableUnderlyingType = targetType.GetNullableUnderlyingType();
|
||||||
|
if (nullableUnderlyingType != null)
|
||||||
|
return !string.IsNullOrWhiteSpace(value)
|
||||||
|
? ConvertScalar(value, nullableUnderlyingType)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
// String-constructable
|
||||||
|
var stringConstructor = GetStringConstructor(targetType);
|
||||||
|
if (stringConstructor != null)
|
||||||
|
return stringConstructor.Invoke(new object[] {value!});
|
||||||
|
|
||||||
|
// String-parseable (with format provider)
|
||||||
|
var parseMethodWithFormatProvider = GetStaticParseMethodWithFormatProvider(targetType);
|
||||||
|
if (parseMethodWithFormatProvider != null)
|
||||||
|
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, ConversionFormatProvider});
|
||||||
|
|
||||||
|
// String-parseable (without format provider)
|
||||||
|
var parseMethod = GetStaticParseMethod(targetType);
|
||||||
|
if (parseMethod != null)
|
||||||
|
return parseMethod.Invoke(null, new object[] {value!});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw CliFxException.CannotConvertToType(this, value, targetType, ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw CliFxException.CannotConvertToType(this, value, targetType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
|
||||||
|
{
|
||||||
|
var array = values
|
||||||
|
.Select(v => ConvertScalar(v, targetElementType))
|
||||||
|
.ToNonGenericArray(targetElementType);
|
||||||
|
|
||||||
|
var arrayType = array.GetType();
|
||||||
|
|
||||||
|
// Assignable from an array
|
||||||
|
if (targetEnumerableType.IsAssignableFrom(arrayType))
|
||||||
|
return array;
|
||||||
|
|
||||||
|
// Constructable from an array
|
||||||
|
var arrayConstructor = targetEnumerableType.GetConstructor(new[] {arrayType});
|
||||||
|
if (arrayConstructor != null)
|
||||||
|
return arrayConstructor.Invoke(new object[] {array});
|
||||||
|
|
||||||
|
throw CliFxException.CannotConvertNonScalar(this, values, targetEnumerableType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private object? Convert(IReadOnlyList<string> values)
|
||||||
{
|
{
|
||||||
var targetType = Property.PropertyType;
|
var targetType = Property.PropertyType;
|
||||||
var enumerableUnderlyingType = GetEnumerableArgumentUnderlyingType();
|
var enumerableUnderlyingType = TryGetEnumerableArgumentUnderlyingType();
|
||||||
|
|
||||||
// Scalar
|
// Scalar
|
||||||
if (enumerableUnderlyingType == null)
|
if (enumerableUnderlyingType == null)
|
||||||
{
|
{
|
||||||
if (values.Count > 1)
|
return values.Count <= 1
|
||||||
{
|
? ConvertScalar(values.SingleOrDefault(), targetType)
|
||||||
throw new CliFxException(new StringBuilder()
|
: throw CliFxException.CannotConvertMultipleValuesToNonScalar(this, values);
|
||||||
.AppendLine($"Can't convert a sequence of values [{string.Join(", ", values)}] to type {targetType.FullName}.")
|
|
||||||
.Append("Target type is not enumerable and can't accept more than one value.")
|
|
||||||
.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return ConvertScalar(values.SingleOrDefault(), targetType);
|
|
||||||
}
|
}
|
||||||
// Non-scalar
|
// Non-scalar
|
||||||
else
|
else
|
||||||
@@ -58,14 +116,32 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public void Inject(ICommand command, params string[] values) =>
|
public void Inject(ICommand command, params string[] values) =>
|
||||||
Inject(command, (IReadOnlyList<string>) values);
|
Inject(command, (IReadOnlyList<string>) values);
|
||||||
|
|
||||||
|
public IReadOnlyList<string> GetValidValues()
|
||||||
|
{
|
||||||
|
var result = new List<string>();
|
||||||
|
|
||||||
|
// Some arguments may have this as null due to a hack that enables built-in options
|
||||||
|
if (Property == null)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
var underlyingPropertyType =
|
||||||
|
Property.PropertyType.GetNullableUnderlyingType() ?? Property.PropertyType;
|
||||||
|
|
||||||
|
// Enum
|
||||||
|
if (underlyingPropertyType.IsEnum)
|
||||||
|
result.AddRange(Enum.GetNames(underlyingPropertyType));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal partial class CommandArgumentSchema
|
internal partial class CommandArgumentSchema
|
||||||
{
|
{
|
||||||
private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture;
|
private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture;
|
||||||
|
|
||||||
private static readonly IReadOnlyDictionary<Type, Func<string, object>> PrimitiveConverters =
|
private static readonly IReadOnlyDictionary<Type, Func<string?, object?>> PrimitiveConverters =
|
||||||
new Dictionary<Type, Func<string?, object>>
|
new Dictionary<Type, Func<string?, object?>>
|
||||||
{
|
{
|
||||||
[typeof(object)] = v => v,
|
[typeof(object)] = v => v,
|
||||||
[typeof(string)] = v => v,
|
[typeof(string)] = v => v,
|
||||||
@@ -99,78 +175,5 @@ namespace CliFx.Domain
|
|||||||
type.GetMethod("Parse",
|
type.GetMethod("Parse",
|
||||||
BindingFlags.Public | BindingFlags.Static,
|
BindingFlags.Public | BindingFlags.Static,
|
||||||
null, new[] {typeof(string), typeof(IFormatProvider)}, null);
|
null, new[] {typeof(string), typeof(IFormatProvider)}, null);
|
||||||
|
|
||||||
private static object ConvertScalar(string? value, Type targetType)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Primitive
|
|
||||||
var primitiveConverter = PrimitiveConverters.GetValueOrDefault(targetType);
|
|
||||||
if (primitiveConverter != null)
|
|
||||||
return primitiveConverter(value);
|
|
||||||
|
|
||||||
// Enum
|
|
||||||
if (targetType.IsEnum)
|
|
||||||
return Enum.Parse(targetType, value, true);
|
|
||||||
|
|
||||||
// Nullable
|
|
||||||
var nullableUnderlyingType = targetType.GetNullableUnderlyingType();
|
|
||||||
if (nullableUnderlyingType != null)
|
|
||||||
return !string.IsNullOrWhiteSpace(value)
|
|
||||||
? ConvertScalar(value, nullableUnderlyingType)
|
|
||||||
: null;
|
|
||||||
|
|
||||||
// String-constructable
|
|
||||||
var stringConstructor = GetStringConstructor(targetType);
|
|
||||||
if (stringConstructor != null)
|
|
||||||
return stringConstructor.Invoke(new object[] {value});
|
|
||||||
|
|
||||||
// String-parseable (with format provider)
|
|
||||||
var parseMethodWithFormatProvider = GetStaticParseMethodWithFormatProvider(targetType);
|
|
||||||
if (parseMethodWithFormatProvider != null)
|
|
||||||
return parseMethodWithFormatProvider.Invoke(null, new object[] {value, ConversionFormatProvider});
|
|
||||||
|
|
||||||
// String-parseable (without format provider)
|
|
||||||
var parseMethod = GetStaticParseMethod(targetType);
|
|
||||||
if (parseMethod != null)
|
|
||||||
return parseMethod.Invoke(null, new object[] {value});
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new CliFxException(new StringBuilder()
|
|
||||||
.AppendLine($"Failed to convert value '{value ?? "<null>"}' to type {targetType.FullName}.")
|
|
||||||
.Append(ex.Message)
|
|
||||||
.ToString(), ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new CliFxException(new StringBuilder()
|
|
||||||
.AppendLine($"Can't convert value '{value ?? "<null>"}' to type {targetType.FullName}.")
|
|
||||||
.Append("Target type is not supported by CliFx.")
|
|
||||||
.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
|
|
||||||
{
|
|
||||||
var array = values
|
|
||||||
.Select(v => ConvertScalar(v, targetElementType))
|
|
||||||
.ToNonGenericArray(targetElementType);
|
|
||||||
|
|
||||||
var arrayType = array.GetType();
|
|
||||||
|
|
||||||
// Assignable from an array
|
|
||||||
if (targetEnumerableType.IsAssignableFrom(arrayType))
|
|
||||||
return array;
|
|
||||||
|
|
||||||
// Constructable from an array
|
|
||||||
var arrayConstructor = targetEnumerableType.GetConstructor(new[] {arrayType});
|
|
||||||
if (arrayConstructor != null)
|
|
||||||
return arrayConstructor.Invoke(new object[] {array});
|
|
||||||
|
|
||||||
throw new CliFxException(new StringBuilder()
|
|
||||||
.AppendLine($"Can't convert a sequence of values [{string.Join(", ", values)}] to type {targetEnumerableType.FullName}.")
|
|
||||||
.AppendLine($"Underlying element type is [{targetElementType.FullName}].")
|
|
||||||
.Append("Target type must either be assignable from an array or have a public constructor that takes a single array argument.")
|
|
||||||
.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,11 @@ namespace CliFx.Domain
|
|||||||
{
|
{
|
||||||
public string Alias { get; }
|
public string Alias { get; }
|
||||||
|
|
||||||
|
public string DisplayAlias =>
|
||||||
|
Alias.Length > 1
|
||||||
|
? $"--{Alias}"
|
||||||
|
: $"-{Alias}";
|
||||||
|
|
||||||
public IReadOnlyList<string> Values { get; }
|
public IReadOnlyList<string> Values { get; }
|
||||||
|
|
||||||
public bool IsHelpOption => CommandOptionSchema.HelpOption.MatchesNameOrShortName(Alias);
|
public bool IsHelpOption => CommandOptionSchema.HelpOption.MatchesNameOrShortName(Alias);
|
||||||
@@ -24,8 +29,7 @@ namespace CliFx.Domain
|
|||||||
{
|
{
|
||||||
var buffer = new StringBuilder();
|
var buffer = new StringBuilder();
|
||||||
|
|
||||||
buffer.Append(Alias.Length > 1 ? "--" : "-");
|
buffer.Append(DisplayAlias);
|
||||||
buffer.Append(Alias);
|
|
||||||
|
|
||||||
foreach (var value in Values)
|
foreach (var value in Values)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ using System.Linq;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using CliFx.Attributes;
|
using CliFx.Attributes;
|
||||||
using CliFx.Internal;
|
|
||||||
|
|
||||||
namespace CliFx.Domain
|
namespace CliFx.Domain
|
||||||
{
|
{
|
||||||
@@ -13,9 +12,9 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public char? ShortName { get; }
|
public char? ShortName { get; }
|
||||||
|
|
||||||
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
|
public override string DisplayName => !string.IsNullOrWhiteSpace(Name)
|
||||||
? Name
|
? $"--{Name}"
|
||||||
: ShortName?.AsString()!;
|
: $"-{ShortName}";
|
||||||
|
|
||||||
public string? EnvironmentVariableName { get; }
|
public string? EnvironmentVariableName { get; }
|
||||||
|
|
||||||
@@ -40,7 +39,7 @@ namespace CliFx.Domain
|
|||||||
!string.IsNullOrWhiteSpace(Name) &&
|
!string.IsNullOrWhiteSpace(Name) &&
|
||||||
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
|
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
public bool MatchesShortName(char shortName) =>
|
public bool MatchesShortName(char? shortName) =>
|
||||||
ShortName != null &&
|
ShortName != null &&
|
||||||
ShortName == shortName;
|
ShortName == shortName;
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public string? Name { get; }
|
public string? Name { get; }
|
||||||
|
|
||||||
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
|
public override string DisplayName =>
|
||||||
|
!string.IsNullOrWhiteSpace(Name)
|
||||||
? Name
|
? Name
|
||||||
: Property.Name.ToLowerInvariant();
|
: Property.Name.ToLowerInvariant();
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,22 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public bool MatchesName(string? name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase);
|
public bool MatchesName(string? name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
private void InjectParameters(ICommand command, IReadOnlyList<string> parameterInputs)
|
public IReadOnlyList<CommandOptionSchema> GetBuiltInOptions()
|
||||||
|
{
|
||||||
|
var result = new List<CommandOptionSchema>(2);
|
||||||
|
|
||||||
|
var helpOption = CommandOptionSchema.HelpOption;
|
||||||
|
var versionOption = CommandOptionSchema.VersionOption;
|
||||||
|
|
||||||
|
result.Add(helpOption);
|
||||||
|
|
||||||
|
if (IsDefault)
|
||||||
|
result.Add(versionOption);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void InjectParameters(ICommand command, IReadOnlyList<CommandUnboundArgumentInput> parameterInputs)
|
||||||
{
|
{
|
||||||
// All inputs must be bound
|
// All inputs must be bound
|
||||||
var remainingParameterInputs = parameterInputs.ToList();
|
var remainingParameterInputs = parameterInputs.ToList();
|
||||||
@@ -57,9 +72,9 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
var scalarParameterInput = i < parameterInputs.Count
|
var scalarParameterInput = i < parameterInputs.Count
|
||||||
? parameterInputs[i]
|
? parameterInputs[i]
|
||||||
: throw new CliFxException($"Missing value for parameter <{scalarParameter.DisplayName}>.");
|
: throw CliFxException.ParameterNotSet(scalarParameter);
|
||||||
|
|
||||||
scalarParameter.Inject(command, scalarParameterInput);
|
scalarParameter.Inject(command, scalarParameterInput.Value);
|
||||||
remainingParameterInputs.Remove(scalarParameterInput);
|
remainingParameterInputs.Remove(scalarParameterInput);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,18 +85,16 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
if (nonScalarParameter != null)
|
if (nonScalarParameter != null)
|
||||||
{
|
{
|
||||||
var nonScalarParameterInputs = parameterInputs.Skip(scalarParameters.Length).ToArray();
|
var nonScalarParameterValues = parameterInputs.Skip(scalarParameters.Length).Select(i => i.Value).ToArray();
|
||||||
nonScalarParameter.Inject(command, nonScalarParameterInputs);
|
|
||||||
|
nonScalarParameter.Inject(command, nonScalarParameterValues);
|
||||||
remainingParameterInputs.Clear();
|
remainingParameterInputs.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure all inputs were bound
|
// Ensure all inputs were bound
|
||||||
if (remainingParameterInputs.Any())
|
if (remainingParameterInputs.Any())
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.UnrecognizedParametersProvided(remainingParameterInputs);
|
||||||
.AppendLine("Unrecognized parameters provided:")
|
|
||||||
.AppendBulletList(remainingParameterInputs)
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,15 +110,15 @@ namespace CliFx.Domain
|
|||||||
var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();
|
var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();
|
||||||
|
|
||||||
// Environment variables
|
// Environment variables
|
||||||
foreach (var environmentVariable in environmentVariables)
|
foreach (var (name, value) in environmentVariables)
|
||||||
{
|
{
|
||||||
var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(environmentVariable.Key));
|
var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(name));
|
||||||
|
|
||||||
if (option != null)
|
if (option != null)
|
||||||
{
|
{
|
||||||
var values = option.IsScalar
|
var values = option.IsScalar
|
||||||
? new[] {environmentVariable.Value}
|
? new[] {value}
|
||||||
: environmentVariable.Value.Split(Path.PathSeparator);
|
: value.Split(Path.PathSeparator);
|
||||||
|
|
||||||
option.Inject(command, values);
|
option.Inject(command, values);
|
||||||
unsetRequiredOptions.Remove(option);
|
unsetRequiredOptions.Remove(option);
|
||||||
@@ -122,11 +135,13 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
if (inputs.Any())
|
if (inputs.Any())
|
||||||
{
|
{
|
||||||
option.Inject(command, inputs.SelectMany(i => i.Values).ToArray());
|
var inputValues = inputs.SelectMany(i => i.Values).ToArray();
|
||||||
|
option.Inject(command, inputValues);
|
||||||
|
|
||||||
foreach (var input in inputs)
|
foreach (var input in inputs)
|
||||||
remainingOptionInputs.Remove(input);
|
remainingOptionInputs.Remove(input);
|
||||||
|
|
||||||
|
if (inputValues.Any())
|
||||||
unsetRequiredOptions.Remove(option);
|
unsetRequiredOptions.Remove(option);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -134,24 +149,18 @@ namespace CliFx.Domain
|
|||||||
// Ensure all required options were set
|
// Ensure all required options were set
|
||||||
if (unsetRequiredOptions.Any())
|
if (unsetRequiredOptions.Any())
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.RequiredOptionsNotSet(unsetRequiredOptions);
|
||||||
.AppendLine("Missing values for some of the required options:")
|
|
||||||
.AppendBulletList(unsetRequiredOptions.Select(o => o.DisplayName))
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure all inputs were bound
|
// Ensure all inputs were bound
|
||||||
if (remainingOptionInputs.Any())
|
if (remainingOptionInputs.Any())
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw CliFxException.UnrecognizedOptionsProvided(remainingOptionInputs);
|
||||||
.AppendLine("Unrecognized options provided:")
|
|
||||||
.AppendBulletList(remainingOptionInputs.Select(o => o.Alias).Distinct())
|
|
||||||
.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICommand CreateInstance(
|
public ICommand CreateInstance(
|
||||||
IReadOnlyList<string> parameterInputs,
|
IReadOnlyList<CommandUnboundArgumentInput> parameterInputs,
|
||||||
IReadOnlyList<CommandOptionInput> optionInputs,
|
IReadOnlyList<CommandOptionInput> optionInputs,
|
||||||
IReadOnlyDictionary<string, string> environmentVariables,
|
IReadOnlyDictionary<string, string> environmentVariables,
|
||||||
ITypeActivator activator)
|
ITypeActivator activator)
|
||||||
@@ -216,8 +225,8 @@ namespace CliFx.Domain
|
|||||||
type,
|
type,
|
||||||
attribute?.Name,
|
attribute?.Name,
|
||||||
attribute?.Description,
|
attribute?.Description,
|
||||||
parameters,
|
parameters!,
|
||||||
options
|
options!
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ namespace CliFx.Domain
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Render(" ");
|
RenderIndent(margin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,6 +199,15 @@ namespace CliFx.Domain
|
|||||||
if (!string.IsNullOrWhiteSpace(parameter.Description))
|
if (!string.IsNullOrWhiteSpace(parameter.Description))
|
||||||
{
|
{
|
||||||
Render(parameter.Description);
|
Render(parameter.Description);
|
||||||
|
Render(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Valid values
|
||||||
|
var validValues = parameter.GetValidValues();
|
||||||
|
if (validValues.Any())
|
||||||
|
{
|
||||||
|
Render($"Valid values: {string.Join(", ", validValues)}.");
|
||||||
|
Render(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderNewLine();
|
RenderNewLine();
|
||||||
@@ -212,12 +221,8 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
var options = command.Options
|
var options = command.Options
|
||||||
.OrderByDescending(o => o.IsRequired)
|
.OrderByDescending(o => o.IsRequired)
|
||||||
.ToList();
|
.Concat(command.GetBuiltInOptions())
|
||||||
|
.ToArray();
|
||||||
// Add built-in options
|
|
||||||
options.Add(CommandOptionSchema.HelpOption);
|
|
||||||
if (command.IsDefault)
|
|
||||||
options.Add(CommandOptionSchema.VersionOption);
|
|
||||||
|
|
||||||
foreach (var option in options)
|
foreach (var option in options)
|
||||||
{
|
{
|
||||||
@@ -257,11 +262,20 @@ namespace CliFx.Domain
|
|||||||
Render(" ");
|
Render(" ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Valid values
|
||||||
|
var validValues = option.GetValidValues();
|
||||||
|
if (validValues.Any())
|
||||||
|
{
|
||||||
|
Render($"Valid values: {string.Join(", ", validValues)}.");
|
||||||
|
Render(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Render default value here.
|
||||||
|
|
||||||
// Environment variable
|
// Environment variable
|
||||||
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
|
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
|
||||||
{
|
{
|
||||||
Render($"(Environment variable: {option.EnvironmentVariableName}).");
|
Render($"Environment variable: {option.EnvironmentVariableName}");
|
||||||
Render(" ");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderNewLine();
|
RenderNewLine();
|
||||||
@@ -279,9 +293,9 @@ namespace CliFx.Domain
|
|||||||
foreach (var childCommand in childCommands)
|
foreach (var childCommand in childCommands)
|
||||||
{
|
{
|
||||||
var relativeCommandName =
|
var relativeCommandName =
|
||||||
string.IsNullOrWhiteSpace(childCommand.Name) || string.IsNullOrWhiteSpace(command.Name)
|
!string.IsNullOrWhiteSpace(command.Name)
|
||||||
? childCommand.Name
|
? childCommand.Name!.Substring(command.Name.Length + 1)
|
||||||
: childCommand.Name.Substring(command.Name.Length + 1);
|
: childCommand.Name!;
|
||||||
|
|
||||||
// Name
|
// Name
|
||||||
RenderIndent();
|
RenderIndent();
|
||||||
|
|||||||
@@ -1,26 +1,376 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Domain;
|
||||||
|
|
||||||
namespace CliFx.Exceptions
|
namespace CliFx.Exceptions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Domain exception thrown within CliFx.
|
/// Domain exception thrown within CliFx.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CliFxException : Exception
|
public partial class CliFxException : Exception
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents the default exit code assigned to exceptions in CliFx.
|
||||||
|
/// </summary>
|
||||||
|
protected const int DefaultExitCode = -100;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether to show the help text after handling this exception.
|
||||||
|
/// </summary>
|
||||||
|
public bool ShowHelp { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether this exception was constructed with a message.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// We cannot check against the 'Message' property because it will always return
|
||||||
|
/// a default message if it was constructed with a null value or is currently null.
|
||||||
|
/// </remarks>
|
||||||
|
public bool HasMessage { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns an exit code associated with this exception.
|
||||||
|
/// </summary>
|
||||||
|
public int ExitCode { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CliFxException"/>.
|
/// Initializes an instance of <see cref="CliFxException"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CliFxException(string? message)
|
public CliFxException(string? message, bool showHelp = false)
|
||||||
: base(message)
|
: this(message, null, showHelp: showHelp)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CliFxException"/>.
|
/// Initializes an instance of <see cref="CliFxException"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CliFxException(string? message, Exception? innerException)
|
public CliFxException(string? message, Exception? innerException, int exitCode = DefaultExitCode, bool showHelp = false)
|
||||||
: base(message, innerException)
|
: base(message, innerException)
|
||||||
{
|
{
|
||||||
|
ExitCode = exitCode != 0
|
||||||
|
? exitCode
|
||||||
|
: throw new ArgumentException("Exit code must not be zero in order to signify failure.");
|
||||||
|
HasMessage = !string.IsNullOrWhiteSpace(message);
|
||||||
|
ShowHelp = showHelp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mid-user-facing exceptions
|
||||||
|
// Provide more diagnostic information here
|
||||||
|
public partial class CliFxException
|
||||||
|
{
|
||||||
|
internal static CliFxException DefaultActivatorFailed(Type type, Exception? innerException = null)
|
||||||
|
{
|
||||||
|
var configureActivatorMethodName = $"{nameof(CliApplicationBuilder)}.{nameof(CliApplicationBuilder.UseTypeActivator)}(...)";
|
||||||
|
|
||||||
|
var message = $@"
|
||||||
|
Failed to create an instance of type '{type.FullName}'.
|
||||||
|
The type must have a public parameterless constructor in order to be instantiated by the default activator.
|
||||||
|
|
||||||
|
To fix this, either make sure this type has a public parameterless constructor, or configure a custom activator using {configureActivatorMethodName}.
|
||||||
|
Refer to the readme to learn how to integrate a dependency container of your choice to act as a type activator.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), innerException);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException DelegateActivatorReceivedNull(Type type)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Failed to create an instance of type '{type.FullName}', received <null> instead.
|
||||||
|
|
||||||
|
To fix this, ensure that the provided type activator was configured correctly, as it's not expected to return <null>.
|
||||||
|
If you are using a dependency container, ensure this type is registered, because it may return <null> otherwise.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException InvalidCommandType(Type type)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{type.FullName}' is not a valid command type.
|
||||||
|
|
||||||
|
In order to be a valid command type, it must:
|
||||||
|
- Not be an abstract class
|
||||||
|
- Implement {typeof(ICommand).FullName}
|
||||||
|
- Be annotated with {typeof(CommandAttribute).FullName}
|
||||||
|
|
||||||
|
If you're experiencing problems, please refer to the readme for a quickstart example.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandsNotRegistered()
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
There are no commands configured in the application.
|
||||||
|
|
||||||
|
To fix this, ensure that at least one command is added through one of the methods on {nameof(CliApplicationBuilder)}.
|
||||||
|
If you're experiencing problems, please refer to the readme for a quickstart example.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandsTooManyDefaults(
|
||||||
|
IReadOnlyList<CommandSchema> invalidCommands)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Application configuration is invalid because there are {invalidCommands.Count} default commands:
|
||||||
|
{string.Join(Environment.NewLine, invalidCommands.Select(p => p.Type.FullName))}
|
||||||
|
|
||||||
|
There can only be one default command (i.e. command with no name) in an application.
|
||||||
|
Other commands must have unique non-empty names that identify them.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandsDuplicateName(
|
||||||
|
string name,
|
||||||
|
IReadOnlyList<CommandSchema> invalidCommands)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Application configuration is invalid because there are {invalidCommands.Count} commands with the same name ('{name}'):
|
||||||
|
{string.Join(Environment.NewLine, invalidCommands.Select(p => p.Type.FullName))}
|
||||||
|
|
||||||
|
Commands must have unique names.
|
||||||
|
Names are not case-sensitive.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandParametersDuplicateOrder(
|
||||||
|
CommandSchema command,
|
||||||
|
int order,
|
||||||
|
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same order ({order}):
|
||||||
|
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
|
||||||
|
|
||||||
|
Parameters must have unique order.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandParametersDuplicateName(
|
||||||
|
CommandSchema command,
|
||||||
|
string name,
|
||||||
|
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same name ('{name}'):
|
||||||
|
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
|
||||||
|
|
||||||
|
Parameters must have unique names to avoid potential confusion in the help text.
|
||||||
|
Names are not case-sensitive.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandParametersTooManyNonScalar(
|
||||||
|
CommandSchema command,
|
||||||
|
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} non-scalar parameters:
|
||||||
|
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
|
||||||
|
|
||||||
|
Non-scalar parameter is such that is bound from more than one value (e.g. array or a complex object).
|
||||||
|
Only one parameter in a command may be non-scalar and it must be the last one in order.
|
||||||
|
|
||||||
|
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandParametersNonLastNonScalar(
|
||||||
|
CommandSchema command,
|
||||||
|
CommandParameterSchema invalidParameter)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains a non-scalar parameter which is not the last in order:
|
||||||
|
{invalidParameter.Property.Name}
|
||||||
|
|
||||||
|
Non-scalar parameter is such that is bound from more than one value (e.g. array or a complex object).
|
||||||
|
Only one parameter in a command may be non-scalar and it must be the last one in order.
|
||||||
|
|
||||||
|
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandOptionsNoName(
|
||||||
|
CommandSchema command,
|
||||||
|
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains one or more options without a name:
|
||||||
|
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||||
|
|
||||||
|
Options must have either a name or a short name or both.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandOptionsInvalidLengthName(
|
||||||
|
CommandSchema command,
|
||||||
|
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains one or more options whose names are too short:
|
||||||
|
{string.Join(Environment.NewLine, invalidOptions.Select(o => $"{o.Property.Name} ('{o.DisplayName}')"))}
|
||||||
|
|
||||||
|
Option names must be at least 2 characters long to avoid confusion with short names.
|
||||||
|
If you intended to set the short name instead, use the attribute overload that accepts a char.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandOptionsDuplicateName(
|
||||||
|
CommandSchema command,
|
||||||
|
string name,
|
||||||
|
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same name ('{name}'):
|
||||||
|
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||||
|
|
||||||
|
Options must have unique names, because that's what identifies them.
|
||||||
|
Names are not case-sensitive.
|
||||||
|
|
||||||
|
To fix this, ensure that all options have different names.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandOptionsDuplicateShortName(
|
||||||
|
CommandSchema command,
|
||||||
|
char shortName,
|
||||||
|
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same short name ('{shortName}'):
|
||||||
|
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||||
|
|
||||||
|
Options must have unique short names.
|
||||||
|
Short names are case-sensitive (i.e. 'a' and 'A' are different short names).";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CommandOptionsDuplicateEnvironmentVariableName(
|
||||||
|
CommandSchema command,
|
||||||
|
string environmentVariableName,
|
||||||
|
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same fallback environment variable name ('{environmentVariableName}'):
|
||||||
|
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||||
|
|
||||||
|
Options cannot share the same environment variable as a fallback.
|
||||||
|
Environment variable names are not case-sensitive.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// End-user-facing exceptions
|
||||||
|
// Avoid internal details and fix recommendations here
|
||||||
|
public partial class CliFxException
|
||||||
|
{
|
||||||
|
internal static CliFxException CannotFindMatchingCommand(CommandLineInput input)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Can't find a command that matches the following arguments:
|
||||||
|
{string.Join(" ", input.UnboundArguments.Select(a => a.Value))}";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
|
||||||
|
CommandArgumentSchema argument,
|
||||||
|
IReadOnlyList<string> values)
|
||||||
|
{
|
||||||
|
var argumentDisplayText = argument is CommandParameterSchema
|
||||||
|
? $"Parameter <{argument.DisplayName}>"
|
||||||
|
: $"Option '{argument.DisplayName}'";
|
||||||
|
|
||||||
|
var message = $@"
|
||||||
|
{argumentDisplayText} expects a single value, but provided with multiple:
|
||||||
|
{string.Join(", ", values.Select(v => $"'{v}'"))}";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CannotConvertToType(
|
||||||
|
CommandArgumentSchema argument,
|
||||||
|
string? value,
|
||||||
|
Type type,
|
||||||
|
Exception? innerException = null)
|
||||||
|
{
|
||||||
|
var argumentDisplayText = argument is CommandParameterSchema
|
||||||
|
? $"parameter <{argument.DisplayName}>"
|
||||||
|
: $"option '{argument.DisplayName}'";
|
||||||
|
|
||||||
|
var message = $@"
|
||||||
|
Can't convert value '{value ?? "<null>"}' to type '{type.FullName}' for {argumentDisplayText}.
|
||||||
|
{innerException?.Message ?? "This type is not supported."}";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), innerException, showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException CannotConvertNonScalar(
|
||||||
|
CommandArgumentSchema argument,
|
||||||
|
IReadOnlyList<string> values,
|
||||||
|
Type type)
|
||||||
|
{
|
||||||
|
var argumentDisplayText = argument is CommandParameterSchema
|
||||||
|
? $"parameter <{argument.DisplayName}>"
|
||||||
|
: $"option '{argument.DisplayName}'";
|
||||||
|
|
||||||
|
var message = $@"
|
||||||
|
Can't convert provided values to type '{type.FullName}' for {argumentDisplayText}:
|
||||||
|
{string.Join(", ", values.Select(v => $"'{v}'"))}
|
||||||
|
|
||||||
|
Target type is not assignable from array and doesn't have a public constructor that takes an array.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException ParameterNotSet(CommandParameterSchema parameter)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Missing value for parameter <{parameter.DisplayName}>.";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException RequiredOptionsNotSet(IReadOnlyList<CommandOptionSchema> options)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Missing values for one or more required options:
|
||||||
|
{string.Join(Environment.NewLine, options.Select(o => o.DisplayName))}";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException UnrecognizedParametersProvided(IReadOnlyList<CommandUnboundArgumentInput> inputs)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Unrecognized parameters provided:
|
||||||
|
{string.Join(Environment.NewLine, inputs.Select(i => $"<{i.Value}>"))}";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), showHelp: true);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static CliFxException UnrecognizedOptionsProvided(IReadOnlyList<CommandOptionInput> inputs)
|
||||||
|
{
|
||||||
|
var message = $@"
|
||||||
|
Unrecognized options provided:
|
||||||
|
{string.Join(Environment.NewLine, inputs.Select(i => i.DisplayAlias))}";
|
||||||
|
|
||||||
|
return new CliFxException(message.Trim(), showHelp: true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,39 +7,31 @@ namespace CliFx.Exceptions
|
|||||||
/// Use this exception if you want to report an error that occured during execution of a command.
|
/// Use this exception if you want to report an error that occured during execution of a command.
|
||||||
/// This exception also allows specifying exit code which will be returned to the calling process.
|
/// This exception also allows specifying exit code which will be returned to the calling process.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class CommandException : Exception
|
public class CommandException : CliFxException
|
||||||
{
|
{
|
||||||
private const int DefaultExitCode = -100;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Process exit code.
|
|
||||||
/// </summary>
|
|
||||||
public int ExitCode { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CommandException"/>.
|
/// Initializes an instance of <see cref="CommandException"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CommandException(string? message, Exception? innerException, int exitCode = DefaultExitCode)
|
public CommandException(string? message, Exception? innerException,
|
||||||
: base(message, innerException)
|
int exitCode = DefaultExitCode, bool showHelp = false)
|
||||||
|
: base(message, innerException, exitCode, showHelp)
|
||||||
{
|
{
|
||||||
ExitCode = exitCode != 0
|
|
||||||
? exitCode
|
|
||||||
: throw new ArgumentException("Exit code must not be zero in order to signify failure.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CommandException"/>.
|
/// Initializes an instance of <see cref="CommandException"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CommandException(string? message, int exitCode = DefaultExitCode)
|
public CommandException(string? message, int exitCode = DefaultExitCode, bool showHelp = false)
|
||||||
: this(message, null, exitCode)
|
: this(message, null, exitCode, showHelp)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CommandException"/>.
|
/// Initializes an instance of <see cref="CommandException"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CommandException(int exitCode = DefaultExitCode)
|
public CommandException(int exitCode = DefaultExitCode, bool showHelp = false)
|
||||||
: this(null, exitCode)
|
: this(null, exitCode, showHelp)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace CliFx
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Extensions for <see cref="CliFx"/>
|
|
||||||
/// </summary>
|
|
||||||
public static class Extensions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
|
|
||||||
/// </summary>
|
|
||||||
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
|
|
||||||
{
|
|
||||||
var lastColor = console.ForegroundColor;
|
|
||||||
console.ForegroundColor = foregroundColor;
|
|
||||||
|
|
||||||
action();
|
|
||||||
|
|
||||||
console.ForegroundColor = lastColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets console background color, executes specified action, and sets the color back to the original value.
|
|
||||||
/// </summary>
|
|
||||||
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
|
|
||||||
{
|
|
||||||
var lastColor = console.BackgroundColor;
|
|
||||||
console.BackgroundColor = backgroundColor;
|
|
||||||
|
|
||||||
action();
|
|
||||||
|
|
||||||
console.BackgroundColor = lastColor;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <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.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -54,6 +54,16 @@ namespace CliFx
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
void ResetColor();
|
void ResetColor();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cursor left offset.
|
||||||
|
/// </summary>
|
||||||
|
int CursorLeft { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Cursor top offset.
|
||||||
|
/// </summary>
|
||||||
|
int CursorTop { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Provides a token that signals when application cancellation is requested.
|
/// Provides a token that signals when application cancellation is requested.
|
||||||
/// Subsequent calls return the same token.
|
/// Subsequent calls return the same token.
|
||||||
@@ -61,4 +71,42 @@ namespace CliFx
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
CancellationToken GetCancellationToken();
|
CancellationToken GetCancellationToken();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extensions for <see cref="IConsole"/>.
|
||||||
|
/// </summary>
|
||||||
|
public static class ConsoleExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
|
||||||
|
/// </summary>
|
||||||
|
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
|
||||||
|
{
|
||||||
|
var lastColor = console.ForegroundColor;
|
||||||
|
console.ForegroundColor = foregroundColor;
|
||||||
|
|
||||||
|
action();
|
||||||
|
|
||||||
|
console.ForegroundColor = lastColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets console background color, executes specified action, and sets the color back to the original value.
|
||||||
|
/// </summary>
|
||||||
|
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
|
||||||
|
{
|
||||||
|
var lastColor = console.BackgroundColor;
|
||||||
|
console.BackgroundColor = backgroundColor;
|
||||||
|
|
||||||
|
action();
|
||||||
|
|
||||||
|
console.BackgroundColor = lastColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,20 @@
|
|||||||
// ReSharper disable CheckNamespace
|
// ReSharper disable CheckNamespace
|
||||||
|
|
||||||
#if NET45 || NETSTANDARD2_0
|
// Polyfills to bridge the missing APIs in older versions of the framework/standard.
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace CliFx.Internal
|
#if NETSTANDARD2_0
|
||||||
|
namespace System.Collections.Generic
|
||||||
{
|
{
|
||||||
internal static class Polyfills
|
internal static class Extensions
|
||||||
{
|
{
|
||||||
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> self, TKey key) =>
|
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
|
||||||
self.TryGetValue(key, out var value) ? value : default;
|
{
|
||||||
|
key = pair.Key;
|
||||||
|
value = pair.Value;
|
||||||
|
}
|
||||||
|
|
||||||
public static StringBuilder AppendJoin<T>(this StringBuilder self, string separator, IEnumerable<T> items) =>
|
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
|
||||||
self.Append(string.Join(separator, items));
|
dic.TryGetValue(key, out var result) ? result! : default!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
14
CliFx/Internal/StringExtensions.cs
Normal file
14
CliFx/Internal/StringExtensions.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace CliFx.Internal
|
||||||
|
{
|
||||||
|
internal static class StringExtensions
|
||||||
|
{
|
||||||
|
public static string Repeat(this char c, int count) => new string(c, count);
|
||||||
|
|
||||||
|
public static string AsString(this char c) => c.Repeat(1);
|
||||||
|
|
||||||
|
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
|
||||||
|
builder.Length > 0 ? builder.Append(value) : builder;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,31 +2,11 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
namespace CliFx.Internal
|
namespace CliFx.Internal
|
||||||
{
|
{
|
||||||
internal static class Extensions
|
internal static class TypeExtensions
|
||||||
{
|
{
|
||||||
public static string Repeat(this char c, int count) => new string(c, count);
|
|
||||||
|
|
||||||
public static string AsString(this char c) => c.Repeat(1);
|
|
||||||
|
|
||||||
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
|
|
||||||
builder.Length > 0 ? builder.Append(value) : builder;
|
|
||||||
|
|
||||||
public static StringBuilder AppendBulletList<T>(this StringBuilder builder, IEnumerable<T> items)
|
|
||||||
{
|
|
||||||
foreach (var item in items)
|
|
||||||
{
|
|
||||||
builder.Append("- ");
|
|
||||||
builder.Append(item);
|
|
||||||
builder.AppendLine();
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType);
|
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);
|
||||||
@@ -7,7 +7,7 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Implementation of <see cref="IConsole"/> that wraps the default system console.
|
/// Implementation of <see cref="IConsole"/> that wraps the default system console.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SystemConsole : IConsole
|
public partial class SystemConsole : IConsole
|
||||||
{
|
{
|
||||||
private CancellationTokenSource? _cancellationTokenSource;
|
private CancellationTokenSource? _cancellationTokenSource;
|
||||||
|
|
||||||
@@ -48,14 +48,28 @@ namespace CliFx
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public SystemConsole()
|
public SystemConsole()
|
||||||
{
|
{
|
||||||
Input = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding, false);
|
Input = WrapInput(Console.OpenStandardInput());
|
||||||
Output = new StreamWriter(Console.OpenStandardOutput(), Console.OutputEncoding) {AutoFlush = true};
|
Output = WrapOutput(Console.OpenStandardOutput());
|
||||||
Error = new StreamWriter(Console.OpenStandardError(), Console.OutputEncoding) {AutoFlush = true};
|
Error = WrapOutput(Console.OpenStandardError());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void ResetColor() => Console.ResetColor();
|
public void ResetColor() => Console.ResetColor();
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public int CursorLeft
|
||||||
|
{
|
||||||
|
get => Console.CursorLeft;
|
||||||
|
set => Console.CursorLeft = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public int CursorTop
|
||||||
|
{
|
||||||
|
get => Console.CursorTop;
|
||||||
|
set => Console.CursorTop = value;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CancellationToken GetCancellationToken()
|
public CancellationToken GetCancellationToken()
|
||||||
{
|
{
|
||||||
@@ -77,4 +91,17 @@ namespace CliFx
|
|||||||
return (_cancellationTokenSource = cts).Token;
|
return (_cancellationTokenSource = cts).Token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public partial class SystemConsole
|
||||||
|
{
|
||||||
|
private static StreamReader WrapInput(Stream? stream) =>
|
||||||
|
stream != null
|
||||||
|
? new StreamReader(stream, Console.InputEncoding, false)
|
||||||
|
: StreamReader.Null;
|
||||||
|
|
||||||
|
private static StreamWriter WrapOutput(Stream? stream) =>
|
||||||
|
stream != null
|
||||||
|
? new StreamWriter(stream, Console.OutputEncoding) {AutoFlush = true}
|
||||||
|
: StreamWriter.Null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
namespace CliFx.Utilities
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Extensions for <see cref="Utilities"/>.
|
|
||||||
/// </summary>
|
|
||||||
public static class Extensions
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a <see cref="ProgressTicker"/> bound to this console.
|
|
||||||
/// </summary>
|
|
||||||
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,26 +9,29 @@ namespace CliFx.Utilities
|
|||||||
{
|
{
|
||||||
private readonly IConsole _console;
|
private readonly IConsole _console;
|
||||||
|
|
||||||
private string _lastOutput = "";
|
private int? _originalCursorLeft;
|
||||||
|
private int? _originalCursorTop;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="ProgressTicker"/>.
|
/// Initializes an instance of <see cref="ProgressTicker"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ProgressTicker(IConsole console)
|
public ProgressTicker(IConsole console) => _console = console;
|
||||||
{
|
|
||||||
_console = console;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void EraseLastOutput()
|
|
||||||
{
|
|
||||||
for (var i = 0; i < _lastOutput.Length; i++)
|
|
||||||
_console.Output.Write('\b');
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RenderProgress(double progress)
|
private void RenderProgress(double progress)
|
||||||
{
|
{
|
||||||
_lastOutput = progress.ToString("P2", _console.Output.FormatProvider);
|
if (_originalCursorLeft != null && _originalCursorTop != null)
|
||||||
_console.Output.Write(_lastOutput);
|
{
|
||||||
|
_console.CursorLeft = _originalCursorLeft.Value;
|
||||||
|
_console.CursorTop = _originalCursorTop.Value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_originalCursorLeft = _console.CursorLeft;
|
||||||
|
_originalCursorTop = _console.CursorTop;
|
||||||
|
}
|
||||||
|
|
||||||
|
var str = progress.ToString("P2", _console.Output.FormatProvider);
|
||||||
|
_console.Output.Write(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -41,9 +44,19 @@ namespace CliFx.Utilities
|
|||||||
// when there's no active console window.
|
// when there's no active console window.
|
||||||
if (!_console.IsOutputRedirected)
|
if (!_console.IsOutputRedirected)
|
||||||
{
|
{
|
||||||
EraseLastOutput();
|
|
||||||
RenderProgress(progress);
|
RenderProgress(progress);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extensions for <see cref="ProgressTicker"/>.
|
||||||
|
/// </summary>
|
||||||
|
public static class ProgressTickerExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a <see cref="ProgressTicker"/> bound to this console.
|
||||||
|
/// </summary>
|
||||||
|
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -44,6 +44,12 @@ namespace CliFx
|
|||||||
BackgroundColor = ConsoleColor.Black;
|
BackgroundColor = ConsoleColor.Black;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public int CursorLeft { get; set; }
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public int CursorTop { get; set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CancellationToken GetCancellationToken() => _cancellationToken;
|
public CancellationToken GetCancellationToken() => _cancellationToken;
|
||||||
|
|
||||||
|
|||||||
14
Readme.md
14
Readme.md
@@ -26,6 +26,7 @@ An important property of CliFx, when compared to some other libraries, is that i
|
|||||||
- Prints errors and routes exit codes on exceptions
|
- Prints errors and routes exit codes on exceptions
|
||||||
- Provides comprehensive and colorful auto-generated help text
|
- Provides comprehensive and colorful auto-generated help text
|
||||||
- Highly testable and easy to debug
|
- Highly testable and easy to debug
|
||||||
|
- Comes with built-in analyzers to help catch common mistakes
|
||||||
- Targets .NET Framework 4.5+ and .NET Standard 2.0+
|
- Targets .NET Framework 4.5+ and .NET Standard 2.0+
|
||||||
- No external dependencies
|
- No external dependencies
|
||||||
|
|
||||||
@@ -430,6 +431,19 @@ Division by zero is not supported.
|
|||||||
1337
|
1337
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You can also specify the `showHelp` parameter to instruct whether to show the help text after printing the error:
|
||||||
|
|
||||||
|
```c#
|
||||||
|
[Command]
|
||||||
|
public class ExampleCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
throw new CommandException("Something went wrong.", showHelp: true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Graceful cancellation
|
### Graceful cancellation
|
||||||
|
|
||||||
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), but you can easily override this behavior.
|
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), but you can easily override this behavior.
|
||||||
|
|||||||
Reference in New Issue
Block a user