mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
187 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7d2f934310 | ||
|
|
95a00b0952 | ||
|
|
cb3fee65f3 | ||
|
|
65628b145a | ||
|
|
802bbfccc6 | ||
|
|
6e7742a4f3 | ||
|
|
f6a1a40471 | ||
|
|
33ca4da260 | ||
|
|
cbb72b16ae | ||
|
|
c58629e999 | ||
|
|
387fb72718 | ||
|
|
e04f0da318 | ||
|
|
d25873ee10 | ||
|
|
a28223fc8b | ||
|
|
1dab27de55 | ||
|
|
698629b153 | ||
|
|
65b66b0d27 | ||
|
|
7d3ba612c4 | ||
|
|
8c3b8d1f49 | ||
|
|
fdd39855ad | ||
|
|
671532efce | ||
|
|
5b124345b0 | ||
|
|
b812bd1423 | ||
|
|
c854f5fb8d | ||
|
|
f38bd32510 | ||
|
|
765fa5503e | ||
|
|
57f168723b | ||
|
|
79e1a2e3d7 | ||
|
|
f4f6d04857 | ||
|
|
015ede0d15 | ||
|
|
4fd7f7c3ca | ||
|
|
896dd49eb4 | ||
|
|
4365ad457a | ||
|
|
fb3617980e | ||
|
|
7690aae456 | ||
|
|
076678a08c | ||
|
|
104279d6e9 | ||
|
|
515d51a91d | ||
|
|
4fdf543190 | ||
|
|
4e1ab096c9 | ||
|
|
8aa6911cca | ||
|
|
f0362019ed | ||
|
|
82895f2e42 | ||
|
|
4cf622abe5 | ||
|
|
d4e22a78d6 | ||
|
|
3883c831e9 | ||
|
|
63441688fe | ||
|
|
e48839b938 | ||
|
|
ed87373dc3 | ||
|
|
6ce52c70f7 | ||
|
|
d2b0b16121 | ||
|
|
d67a9fe762 | ||
|
|
ce2a3153e6 | ||
|
|
d4b54231fb | ||
|
|
70bfe0bf91 | ||
|
|
9690c380d3 | ||
|
|
85caa275ae | ||
|
|
32026e59c0 | ||
|
|
486ccb9685 | ||
|
|
7b766f70f3 | ||
|
|
f73e96488f | ||
|
|
af63fa5a1f | ||
|
|
e8f53c9463 | ||
|
|
9564cd5d30 | ||
|
|
ed458c3980 | ||
|
|
25538f99db | ||
|
|
36436e7a4b | ||
|
|
a6070332c9 | ||
|
|
25cbfdb4b8 | ||
|
|
d1b5107c2c | ||
|
|
03873d63cd | ||
|
|
89aba39964 | ||
|
|
ab57a103d1 | ||
|
|
d0b2ebc061 | ||
|
|
857257ca73 | ||
|
|
3587155c7e | ||
|
|
ae05e0db96 | ||
|
|
41c0493e66 | ||
|
|
43a304bb26 | ||
|
|
cd3892bf83 | ||
|
|
3f7c02342d | ||
|
|
c65cdf465e | ||
|
|
b5d67ecf24 | ||
|
|
a94b2296e1 | ||
|
|
fa05e4df3f | ||
|
|
b70b25076e | ||
|
|
0662f341e6 | ||
|
|
80bf477f3b | ||
|
|
e4a502d9d6 | ||
|
|
13b15b98ed | ||
|
|
80465e0e51 | ||
|
|
9a1ce7e7e5 | ||
|
|
b45da64664 | ||
|
|
df01dc055e | ||
|
|
31dd24d189 | ||
|
|
2a76dfe1c8 | ||
|
|
59ee2e34d8 | ||
|
|
9e04f79469 | ||
|
|
cd55898011 | ||
|
|
272c079767 | ||
|
|
256b693466 | ||
|
|
89cc3c8785 | ||
|
|
43e3042bac | ||
|
|
c906833ac7 | ||
|
|
dd882a6372 | ||
|
|
3017c3d6c3 | ||
|
|
4b98dbf51f | ||
|
|
e652f9bda4 | ||
|
|
21c550d99c | ||
|
|
23d29a8309 | ||
|
|
70796c1254 | ||
|
|
1b62b2ded2 | ||
|
|
a9f4958c92 | ||
|
|
66f9b1a256 | ||
|
|
de8513c6fa | ||
|
|
105dc88ccd | ||
|
|
b736eeaf7d | ||
|
|
04415cbfc1 | ||
|
|
45c2b9c4e0 | ||
|
|
78ffaeb4b2 | ||
|
|
08e2874eb4 | ||
|
|
6648ae22eb | ||
|
|
bd6b1a1134 | ||
|
|
d5b95bf1f1 | ||
|
|
f5c34ca454 | ||
|
|
63f583b02a | ||
|
|
fa82f892e4 | ||
|
|
5a696c181b | ||
|
|
7d7edaf30f | ||
|
|
172ec1f15e | ||
|
|
e5bbda5892 | ||
|
|
fc1568ce20 | ||
|
|
efd8bbe89f | ||
|
|
2d8b0b4c88 | ||
|
|
87688ec29e | ||
|
|
ddc1ae8537 | ||
|
|
5104a2ebf9 | ||
|
|
b6ea1c3df0 | ||
|
|
cf521a9fb3 | ||
|
|
b5fa60a26b | ||
|
|
500378070d | ||
|
|
24c892b1ab | ||
|
|
f1554fd08a | ||
|
|
5a08b8c19b | ||
|
|
7dfbb40860 | ||
|
|
743241cb3b | ||
|
|
384482a47c | ||
|
|
86fdf72d9c | ||
|
|
dc067ba224 | ||
|
|
a322632e46 | ||
|
|
f09caa876f | ||
|
|
018320582b | ||
|
|
18429827df | ||
|
|
b050ca4d67 | ||
|
|
f8cd2a56b2 | ||
|
|
6a06cdc422 | ||
|
|
b0d9626e74 | ||
|
|
f47cd3774e | ||
|
|
ed72571ddc | ||
|
|
e7e47b1c9d | ||
|
|
50df046754 | ||
|
|
041a995c62 | ||
|
|
5174d5354b | ||
|
|
9856e784f5 | ||
|
|
16676cff8c | ||
|
|
d9c27dc82a | ||
|
|
5bb175fd4b | ||
|
|
d72391df1f | ||
|
|
c1ee1a968a | ||
|
|
4e9effe481 | ||
|
|
5ac9b33056 | ||
|
|
a64a8fc651 | ||
|
|
24eef8957d | ||
|
|
dd2789790e | ||
|
|
d2599af90b | ||
|
|
2bdb2bddc8 | ||
|
|
77c7faa759 | ||
|
|
4ba9413012 | ||
|
|
3611aa51e6 | ||
|
|
74ee927498 | ||
|
|
79cf994386 | ||
|
|
7a5a32d27b | ||
|
|
1543076bf4 | ||
|
|
63d798977d | ||
|
|
e0211fc141 | ||
|
|
fd6ed3ca72 | ||
|
|
3a9ac3d36c |
3
.github/FUNDING.yml
vendored
Normal file
3
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
github: Tyrrrz
|
||||||
|
patreon: Tyrrrz
|
||||||
|
custom: ['buymeacoffee.com/Tyrrrz', 'tyrrrz.me/donate']
|
||||||
25
.github/workflows/CD.yml
vendored
Normal file
25
.github/workflows/CD.yml
vendored
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
name: CD
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Install .NET Core
|
||||||
|
uses: actions/setup-dotnet@v1.4.0
|
||||||
|
with:
|
||||||
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
|
- name: Pack
|
||||||
|
run: dotnet pack CliFx --configuration Release
|
||||||
|
|
||||||
|
- name: Deploy
|
||||||
|
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{ secrets.NUGET_TOKEN }}
|
||||||
35
.github/workflows/CI.yml
vendored
Normal file
35
.github/workflows/CI.yml
vendored
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
name: CI
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Install .NET Core
|
||||||
|
uses: actions/setup-dotnet@v1.4.0
|
||||||
|
with:
|
||||||
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
|
- name: Build & test
|
||||||
|
run: dotnet test --configuration Release --logger GitHubActions
|
||||||
|
|
||||||
|
- name: Upload coverage
|
||||||
|
uses: codecov/codecov-action@v1.0.5
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
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
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -143,6 +143,7 @@ _TeamCity*
|
|||||||
_NCrunch_*
|
_NCrunch_*
|
||||||
.*crunch*.local.xml
|
.*crunch*.local.xml
|
||||||
nCrunchTemp_*
|
nCrunchTemp_*
|
||||||
|
.ncrunchsolution
|
||||||
|
|
||||||
# MightyMoose
|
# MightyMoose
|
||||||
*.mm.*
|
*.mm.*
|
||||||
|
|||||||
BIN
.screenshots/help.png
Normal file
BIN
.screenshots/help.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 20 KiB |
16
Changelog.md
16
Changelog.md
@@ -0,0 +1,16 @@
|
|||||||
|
### 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)
|
||||||
|
|
||||||
|
- 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 argument binding behavior so that an error is produced if the user provides an argument that doesn't match with any parameter or option. This is done in order to improve user experience, as otherwise the user may make a typo without knowing that their input wasn't taken into account.
|
||||||
|
- Changed argument binding behavior so that options can be set to multiple argument values while specifying them with mixed naming. For example, `--option value1 -o value2 --option value3` would result in the option being set to corresponding three values, assuming `--option` and `-o` match with the same option.
|
||||||
|
|||||||
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
52
CliFx.Benchmarks/Benchmarks.cs
Normal file
52
CliFx.Benchmarks/Benchmarks.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using BenchmarkDotNet.Configs;
|
||||||
|
using BenchmarkDotNet.Order;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
|
using CliFx.Benchmarks.Commands;
|
||||||
|
using CommandLine;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks
|
||||||
|
{
|
||||||
|
[SimpleJob]
|
||||||
|
[RankColumn]
|
||||||
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
||||||
|
public class Benchmarks
|
||||||
|
{
|
||||||
|
private static readonly string[] Arguments = {"--str", "hello world", "-i", "13", "-b"};
|
||||||
|
|
||||||
|
[Benchmark(Description = "CliFx", Baseline = true)]
|
||||||
|
public async ValueTask<int> ExecuteWithCliFx() =>
|
||||||
|
await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments, new Dictionary<string, string>());
|
||||||
|
|
||||||
|
[Benchmark(Description = "System.CommandLine")]
|
||||||
|
public async Task<int> ExecuteWithSystemCommandLine() =>
|
||||||
|
await new SystemCommandLineCommand().ExecuteAsync(Arguments);
|
||||||
|
|
||||||
|
[Benchmark(Description = "McMaster.Extensions.CommandLineUtils")]
|
||||||
|
public int ExecuteWithMcMaster() =>
|
||||||
|
McMaster.Extensions.CommandLineUtils.CommandLineApplication.Execute<McMasterCommand>(Arguments);
|
||||||
|
|
||||||
|
[Benchmark(Description = "CommandLineParser")]
|
||||||
|
public void ExecuteWithCommandLineParser() =>
|
||||||
|
new Parser()
|
||||||
|
.ParseArguments(Arguments, typeof(CommandLineParserCommand))
|
||||||
|
.WithParsed<CommandLineParserCommand>(c => c.Execute());
|
||||||
|
|
||||||
|
[Benchmark(Description = "PowerArgs")]
|
||||||
|
public void ExecuteWithPowerArgs() =>
|
||||||
|
PowerArgs.Args.InvokeMain<PowerArgsCommand>(Arguments);
|
||||||
|
|
||||||
|
[Benchmark(Description = "Clipr")]
|
||||||
|
public void ExecuteWithClipr() =>
|
||||||
|
clipr.CliParser.Parse<CliprCommand>(Arguments).Execute();
|
||||||
|
|
||||||
|
[Benchmark(Description = "Cocona")]
|
||||||
|
public void ExecuteWithCocona() =>
|
||||||
|
Cocona.CoconaApp.Run<CoconaCommand>(Arguments);
|
||||||
|
|
||||||
|
public static void Main() =>
|
||||||
|
BenchmarkRunner.Run<Benchmarks>(DefaultConfig.Instance.With(ConfigOptions.DisableOptimizationsValidator));
|
||||||
|
}
|
||||||
|
}
|
||||||
23
CliFx.Benchmarks/CliFx.Benchmarks.csproj
Normal file
23
CliFx.Benchmarks/CliFx.Benchmarks.csproj
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
||||||
|
<PackageReference Include="clipr" Version="1.6.1" />
|
||||||
|
<PackageReference Include="Cocona" Version="1.3.0" />
|
||||||
|
<PackageReference Include="CommandLineParser" Version="2.7.82" />
|
||||||
|
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />
|
||||||
|
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
||||||
|
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
20
CliFx.Benchmarks/Commands/CliFxCommand.cs
Normal file
20
CliFx.Benchmarks/Commands/CliFxCommand.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class CliFxCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("str", 's')]
|
||||||
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("int", 'i')]
|
||||||
|
public int IntOption { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("bool", 'b')]
|
||||||
|
public bool BoolOption { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
CliFx.Benchmarks/Commands/CliprCommand.cs
Normal file
20
CliFx.Benchmarks/Commands/CliprCommand.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using clipr;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class CliprCommand
|
||||||
|
{
|
||||||
|
[NamedArgument('s', "str")]
|
||||||
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
|
[NamedArgument('i', "int")]
|
||||||
|
public int IntOption { get; set; }
|
||||||
|
|
||||||
|
[NamedArgument('b', "bool", Constraint = NumArgsConstraint.Optional, Const = true)]
|
||||||
|
public bool BoolOption { get; set; }
|
||||||
|
|
||||||
|
public void Execute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CliFx.Benchmarks/Commands/CoconaCommand.cs
Normal file
17
CliFx.Benchmarks/Commands/CoconaCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using Cocona;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class CoconaCommand
|
||||||
|
{
|
||||||
|
public void Execute(
|
||||||
|
[Option("str", new []{'s'})]
|
||||||
|
string? strOption,
|
||||||
|
[Option("int", new []{'i'})]
|
||||||
|
int intOption,
|
||||||
|
[Option("bool", new []{'b'})]
|
||||||
|
bool boolOption)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
CliFx.Benchmarks/Commands/CommandLineParserCommand.cs
Normal file
20
CliFx.Benchmarks/Commands/CommandLineParserCommand.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using CommandLine;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class CommandLineParserCommand
|
||||||
|
{
|
||||||
|
[Option('s', "str")]
|
||||||
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
|
[Option('i', "int")]
|
||||||
|
public int IntOption { get; set; }
|
||||||
|
|
||||||
|
[Option('b', "bool")]
|
||||||
|
public bool BoolOption { get; set; }
|
||||||
|
|
||||||
|
public void Execute()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
CliFx.Benchmarks/Commands/McMasterCommand.cs
Normal file
18
CliFx.Benchmarks/Commands/McMasterCommand.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using McMaster.Extensions.CommandLineUtils;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class McMasterCommand
|
||||||
|
{
|
||||||
|
[Option("--str|-s")]
|
||||||
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
|
[Option("--int|-i")]
|
||||||
|
public int IntOption { get; set; }
|
||||||
|
|
||||||
|
[Option("--bool|-b")]
|
||||||
|
public bool BoolOption { get; set; }
|
||||||
|
|
||||||
|
public int OnExecute() => 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
20
CliFx.Benchmarks/Commands/PowerArgsCommand.cs
Normal file
20
CliFx.Benchmarks/Commands/PowerArgsCommand.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using PowerArgs;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class PowerArgsCommand
|
||||||
|
{
|
||||||
|
[ArgShortcut("--str"), ArgShortcut("-s")]
|
||||||
|
public string? StrOption { get; set; }
|
||||||
|
|
||||||
|
[ArgShortcut("--int"), ArgShortcut("-i")]
|
||||||
|
public int IntOption { get; set; }
|
||||||
|
|
||||||
|
[ArgShortcut("--bool"), ArgShortcut("-b")]
|
||||||
|
public bool BoolOption { get; set; }
|
||||||
|
|
||||||
|
public void Main()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
34
CliFx.Benchmarks/Commands/SystemCommandLineCommand.cs
Normal file
34
CliFx.Benchmarks/Commands/SystemCommandLineCommand.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using System.CommandLine;
|
||||||
|
using System.CommandLine.Invocation;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace CliFx.Benchmarks.Commands
|
||||||
|
{
|
||||||
|
public class SystemCommandLineCommand
|
||||||
|
{
|
||||||
|
public static int ExecuteHandler(string s, int i, bool b) => 0;
|
||||||
|
|
||||||
|
public Task<int> ExecuteAsync(string[] args)
|
||||||
|
{
|
||||||
|
var command = new RootCommand
|
||||||
|
{
|
||||||
|
new Option(new[] {"--str", "-s"})
|
||||||
|
{
|
||||||
|
Argument = new Argument<string?>()
|
||||||
|
},
|
||||||
|
new Option(new[] {"--int", "-i"})
|
||||||
|
{
|
||||||
|
Argument = new Argument<int>()
|
||||||
|
},
|
||||||
|
new Option(new[] {"--bool", "-b"})
|
||||||
|
{
|
||||||
|
Argument = new Argument<bool>()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
command.Handler = CommandHandler.Create(typeof(SystemCommandLineCommand).GetMethod(nameof(ExecuteHandler)));
|
||||||
|
|
||||||
|
return command.InvokeAsync(args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
CliFx.Demo/CliFx.Demo.csproj
Normal file
19
CliFx.Demo/CliFx.Demo.csproj
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
|
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
68
CliFx.Demo/Commands/BookAddCommand.cs
Normal file
68
CliFx.Demo/Commands/BookAddCommand.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Demo.Internal;
|
||||||
|
using CliFx.Demo.Models;
|
||||||
|
using CliFx.Demo.Services;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Commands
|
||||||
|
{
|
||||||
|
[Command("book add", Description = "Add a book to the library.")]
|
||||||
|
public partial class BookAddCommand : ICommand
|
||||||
|
{
|
||||||
|
private readonly LibraryService _libraryService;
|
||||||
|
|
||||||
|
[CommandParameter(0, Name = "title", Description = "Book title.")]
|
||||||
|
public string Title { get; set; } = "";
|
||||||
|
|
||||||
|
[CommandOption("author", 'a', IsRequired = true, Description = "Book author.")]
|
||||||
|
public string Author { get; set; } = "";
|
||||||
|
|
||||||
|
[CommandOption("published", 'p', Description = "Book publish date.")]
|
||||||
|
public DateTimeOffset Published { get; set; } = CreateRandomDate();
|
||||||
|
|
||||||
|
[CommandOption("isbn", 'n', Description = "Book ISBN.")]
|
||||||
|
public Isbn Isbn { get; set; } = CreateRandomIsbn();
|
||||||
|
|
||||||
|
public BookAddCommand(LibraryService libraryService)
|
||||||
|
{
|
||||||
|
_libraryService = libraryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
if (_libraryService.GetBook(Title) != null)
|
||||||
|
throw new CommandException("Book already exists.", 1);
|
||||||
|
|
||||||
|
var book = new Book(Title, Author, Published, Isbn);
|
||||||
|
_libraryService.AddBook(book);
|
||||||
|
|
||||||
|
console.Output.WriteLine("Book added.");
|
||||||
|
console.RenderBook(book);
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class BookAddCommand
|
||||||
|
{
|
||||||
|
private static readonly Random Random = new Random();
|
||||||
|
|
||||||
|
private static DateTimeOffset CreateRandomDate() => new DateTimeOffset(
|
||||||
|
Random.Next(1800, 2020),
|
||||||
|
Random.Next(1, 12),
|
||||||
|
Random.Next(1, 28),
|
||||||
|
Random.Next(1, 23),
|
||||||
|
Random.Next(1, 59),
|
||||||
|
Random.Next(1, 59),
|
||||||
|
TimeSpan.Zero);
|
||||||
|
|
||||||
|
private static Isbn CreateRandomIsbn() => new Isbn(
|
||||||
|
Random.Next(0, 999),
|
||||||
|
Random.Next(0, 99),
|
||||||
|
Random.Next(0, 99999),
|
||||||
|
Random.Next(0, 99),
|
||||||
|
Random.Next(0, 9));
|
||||||
|
}
|
||||||
|
}
|
||||||
34
CliFx.Demo/Commands/BookCommand.cs
Normal file
34
CliFx.Demo/Commands/BookCommand.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Demo.Internal;
|
||||||
|
using CliFx.Demo.Services;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Commands
|
||||||
|
{
|
||||||
|
[Command("book", Description = "View, list, add or remove books.")]
|
||||||
|
public class BookCommand : ICommand
|
||||||
|
{
|
||||||
|
private readonly LibraryService _libraryService;
|
||||||
|
|
||||||
|
[CommandParameter(0, Name = "title", Description = "Book title.")]
|
||||||
|
public string Title { get; set; } = "";
|
||||||
|
|
||||||
|
public BookCommand(LibraryService libraryService)
|
||||||
|
{
|
||||||
|
_libraryService = libraryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
var book = _libraryService.GetBook(Title);
|
||||||
|
|
||||||
|
if (book == null)
|
||||||
|
throw new CommandException("Book not found.", 1);
|
||||||
|
|
||||||
|
console.RenderBook(book);
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
CliFx.Demo/Commands/BookListCommand.cs
Normal file
37
CliFx.Demo/Commands/BookListCommand.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Demo.Internal;
|
||||||
|
using CliFx.Demo.Services;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Commands
|
||||||
|
{
|
||||||
|
[Command("book list", Description = "List all books in the library.")]
|
||||||
|
public class BookListCommand : ICommand
|
||||||
|
{
|
||||||
|
private readonly LibraryService _libraryService;
|
||||||
|
|
||||||
|
public BookListCommand(LibraryService libraryService)
|
||||||
|
{
|
||||||
|
_libraryService = libraryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
var library = _libraryService.GetLibrary();
|
||||||
|
|
||||||
|
var isFirst = true;
|
||||||
|
foreach (var book in library.Books)
|
||||||
|
{
|
||||||
|
// Margin
|
||||||
|
if (!isFirst)
|
||||||
|
console.Output.WriteLine();
|
||||||
|
isFirst = false;
|
||||||
|
|
||||||
|
// Render book
|
||||||
|
console.RenderBook(book);
|
||||||
|
}
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
CliFx.Demo/Commands/BookRemoveCommand.cs
Normal file
35
CliFx.Demo/Commands/BookRemoveCommand.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Demo.Services;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Commands
|
||||||
|
{
|
||||||
|
[Command("book remove", Description = "Remove a book from the library.")]
|
||||||
|
public class BookRemoveCommand : ICommand
|
||||||
|
{
|
||||||
|
private readonly LibraryService _libraryService;
|
||||||
|
|
||||||
|
[CommandParameter(0, Name = "title", Description = "Book title.")]
|
||||||
|
public string Title { get; set; } = "";
|
||||||
|
|
||||||
|
public BookRemoveCommand(LibraryService libraryService)
|
||||||
|
{
|
||||||
|
_libraryService = libraryService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
var book = _libraryService.GetBook(Title);
|
||||||
|
|
||||||
|
if (book == null)
|
||||||
|
throw new CommandException("Book not found.", 1);
|
||||||
|
|
||||||
|
_libraryService.RemoveBook(book);
|
||||||
|
|
||||||
|
console.Output.WriteLine($"Book {Title} removed.");
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
29
CliFx.Demo/Internal/Extensions.cs
Normal file
29
CliFx.Demo/Internal/Extensions.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using System;
|
||||||
|
using CliFx.Demo.Models;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Internal
|
||||||
|
{
|
||||||
|
internal static class Extensions
|
||||||
|
{
|
||||||
|
public static void RenderBook(this IConsole console, Book book)
|
||||||
|
{
|
||||||
|
// Title
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Title));
|
||||||
|
|
||||||
|
// Author
|
||||||
|
console.Output.Write(" ");
|
||||||
|
console.Output.Write("Author: ");
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Author));
|
||||||
|
|
||||||
|
// Published
|
||||||
|
console.Output.Write(" ");
|
||||||
|
console.Output.Write("Published: ");
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine($"{book.Published:d}"));
|
||||||
|
|
||||||
|
// ISBN
|
||||||
|
console.Output.Write(" ");
|
||||||
|
console.Output.Write("ISBN: ");
|
||||||
|
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Isbn));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
CliFx.Demo/Models/Book.cs
Normal file
23
CliFx.Demo/Models/Book.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Models
|
||||||
|
{
|
||||||
|
public class Book
|
||||||
|
{
|
||||||
|
public string Title { get; }
|
||||||
|
|
||||||
|
public string Author { get; }
|
||||||
|
|
||||||
|
public DateTimeOffset Published { get; }
|
||||||
|
|
||||||
|
public Isbn Isbn { get; }
|
||||||
|
|
||||||
|
public Book(string title, string author, DateTimeOffset published, Isbn isbn)
|
||||||
|
{
|
||||||
|
Title = title;
|
||||||
|
Author = author;
|
||||||
|
Published = published;
|
||||||
|
Isbn = isbn;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
22
CliFx.Demo/Models/Extensions.cs
Normal file
22
CliFx.Demo/Models/Extensions.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using System.Linq;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Models
|
||||||
|
{
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
public static Library WithBook(this Library library, Book book)
|
||||||
|
{
|
||||||
|
var books = library.Books.ToList();
|
||||||
|
books.Add(book);
|
||||||
|
|
||||||
|
return new Library(books);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Library WithoutBook(this Library library, Book book)
|
||||||
|
{
|
||||||
|
var books = library.Books.Where(b => b != book).ToArray();
|
||||||
|
|
||||||
|
return new Library(books);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
45
CliFx.Demo/Models/Isbn.cs
Normal file
45
CliFx.Demo/Models/Isbn.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Models
|
||||||
|
{
|
||||||
|
public partial class Isbn
|
||||||
|
{
|
||||||
|
public int EanPrefix { get; }
|
||||||
|
|
||||||
|
public int RegistrationGroup { get; }
|
||||||
|
|
||||||
|
public int Registrant { get; }
|
||||||
|
|
||||||
|
public int Publication { get; }
|
||||||
|
|
||||||
|
public int CheckDigit { get; }
|
||||||
|
|
||||||
|
public Isbn(int eanPrefix, int registrationGroup, int registrant, int publication, int checkDigit)
|
||||||
|
{
|
||||||
|
EanPrefix = eanPrefix;
|
||||||
|
RegistrationGroup = registrationGroup;
|
||||||
|
Registrant = registrant;
|
||||||
|
Publication = publication;
|
||||||
|
CheckDigit = checkDigit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() =>
|
||||||
|
$"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Isbn
|
||||||
|
{
|
||||||
|
public static Isbn Parse(string value, IFormatProvider formatProvider)
|
||||||
|
{
|
||||||
|
var components = value.Split('-', 5, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
|
return new Isbn(
|
||||||
|
int.Parse(components[0], formatProvider),
|
||||||
|
int.Parse(components[1], formatProvider),
|
||||||
|
int.Parse(components[2], formatProvider),
|
||||||
|
int.Parse(components[3], formatProvider),
|
||||||
|
int.Parse(components[4], formatProvider)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
CliFx.Demo/Models/Library.cs
Normal file
20
CliFx.Demo/Models/Library.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Models
|
||||||
|
{
|
||||||
|
public partial class Library
|
||||||
|
{
|
||||||
|
public IReadOnlyList<Book> Books { get; }
|
||||||
|
|
||||||
|
public Library(IReadOnlyList<Book> books)
|
||||||
|
{
|
||||||
|
Books = books;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class Library
|
||||||
|
{
|
||||||
|
public static Library Empty { get; } = new Library(Array.Empty<Book>());
|
||||||
|
}
|
||||||
|
}
|
||||||
35
CliFx.Demo/Program.cs
Normal file
35
CliFx.Demo/Program.cs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Demo.Commands;
|
||||||
|
using CliFx.Demo.Services;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace CliFx.Demo
|
||||||
|
{
|
||||||
|
public static class Program
|
||||||
|
{
|
||||||
|
private static IServiceProvider GetServiceProvider()
|
||||||
|
{
|
||||||
|
// We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands
|
||||||
|
var services = new ServiceCollection();
|
||||||
|
|
||||||
|
// Register services
|
||||||
|
services.AddSingleton<LibraryService>();
|
||||||
|
|
||||||
|
// Register commands
|
||||||
|
services.AddTransient<BookCommand>();
|
||||||
|
services.AddTransient<BookAddCommand>();
|
||||||
|
services.AddTransient<BookRemoveCommand>();
|
||||||
|
services.AddTransient<BookListCommand>();
|
||||||
|
|
||||||
|
return services.BuildServiceProvider();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async Task<int> Main() =>
|
||||||
|
await new CliApplicationBuilder()
|
||||||
|
.AddCommandsFromThisAssembly()
|
||||||
|
.UseTypeActivator(GetServiceProvider().GetService)
|
||||||
|
.Build()
|
||||||
|
.RunAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
7
CliFx.Demo/Readme.md
Normal file
7
CliFx.Demo/Readme.md
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# CliFx Demo Project
|
||||||
|
|
||||||
|
Sample command line interface for managing a library of books.
|
||||||
|
|
||||||
|
This demo project shows basic CliFx functionality such as command routing, argument parsing, autogenerated help text, and some other things.
|
||||||
|
|
||||||
|
You can get a list of available commands by running `CliFx.Demo --help`.
|
||||||
42
CliFx.Demo/Services/LibraryService.cs
Normal file
42
CliFx.Demo/Services/LibraryService.cs
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using CliFx.Demo.Models;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace CliFx.Demo.Services
|
||||||
|
{
|
||||||
|
public class LibraryService
|
||||||
|
{
|
||||||
|
private string StorageFilePath => Path.Combine(Directory.GetCurrentDirectory(), "Data.json");
|
||||||
|
|
||||||
|
private void StoreLibrary(Library library)
|
||||||
|
{
|
||||||
|
var data = JsonConvert.SerializeObject(library);
|
||||||
|
File.WriteAllText(StorageFilePath, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Library GetLibrary()
|
||||||
|
{
|
||||||
|
if (!File.Exists(StorageFilePath))
|
||||||
|
return Library.Empty;
|
||||||
|
|
||||||
|
var data = File.ReadAllText(StorageFilePath);
|
||||||
|
|
||||||
|
return JsonConvert.DeserializeObject<Library>(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book? GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
||||||
|
|
||||||
|
public void AddBook(Book book)
|
||||||
|
{
|
||||||
|
var updatedLibrary = GetLibrary().WithBook(book);
|
||||||
|
StoreLibrary(updatedLibrary);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveBook(Book book)
|
||||||
|
{
|
||||||
|
var updatedLibrary = GetLibrary().WithoutBook(book);
|
||||||
|
StoreLibrary(updatedLibrary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net45</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<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>
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Globalization;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Models;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Dummy.Commands
|
|
||||||
{
|
|
||||||
[Command("add")]
|
|
||||||
public class AddCommand : Command
|
|
||||||
{
|
|
||||||
[CommandOption("a", IsRequired = true, Description = "Left operand.")]
|
|
||||||
public double A { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("b", IsRequired = true, Description = "Right operand.")]
|
|
||||||
public double B { get; set; }
|
|
||||||
|
|
||||||
public override ExitCode Execute()
|
|
||||||
{
|
|
||||||
var result = A + B;
|
|
||||||
Console.WriteLine(result.ToString(CultureInfo.InvariantCulture));
|
|
||||||
|
|
||||||
return ExitCode.Success;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
23
CliFx.Tests.Dummy/Commands/ConsoleTestCommand.cs
Normal file
23
CliFx.Tests.Dummy/Commands/ConsoleTestCommand.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Dummy.Commands
|
||||||
|
{
|
||||||
|
[Command("console-test")]
|
||||||
|
public class ConsoleTestCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
var input = console.Input.ReadToEnd();
|
||||||
|
|
||||||
|
console.WithColors(ConsoleColor.Black, ConsoleColor.White, () =>
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(input);
|
||||||
|
console.Error.WriteLine(input);
|
||||||
|
});
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Text;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Models;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Dummy.Commands
|
|
||||||
{
|
|
||||||
[DefaultCommand]
|
|
||||||
public class DefaultCommand : Command
|
|
||||||
{
|
|
||||||
[CommandOption("target", ShortName = 't', Description = "Greeting target.")]
|
|
||||||
public string Target { get; set; } = "world";
|
|
||||||
|
|
||||||
[CommandOption("enthusiastic", ShortName = 'e', Description = "Whether the greeting should be enthusiastic.")]
|
|
||||||
public bool IsEnthusiastic { get; set; }
|
|
||||||
|
|
||||||
public override ExitCode Execute()
|
|
||||||
{
|
|
||||||
var buffer = new StringBuilder();
|
|
||||||
|
|
||||||
buffer.Append("Hello ").Append(Target);
|
|
||||||
|
|
||||||
if (IsEnthusiastic)
|
|
||||||
buffer.Append("!!!");
|
|
||||||
|
|
||||||
Console.WriteLine(buffer.ToString());
|
|
||||||
|
|
||||||
return ExitCode.Success;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
19
CliFx.Tests.Dummy/Commands/HelloWorldCommand.cs
Normal file
19
CliFx.Tests.Dummy/Commands/HelloWorldCommand.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Dummy.Commands
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
public class HelloWorldCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("target", EnvironmentVariableName = "ENV_TARGET")]
|
||||||
|
public string Target { get; set; } = "World";
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine($"Hello {Target}!");
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Globalization;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Models;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Dummy.Commands
|
|
||||||
{
|
|
||||||
[Command("log")]
|
|
||||||
public class LogCommand : Command
|
|
||||||
{
|
|
||||||
[CommandOption("value", IsRequired = true, Description = "Value whose logarithm is to be found.")]
|
|
||||||
public double Value { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("base", Description = "Logarithm base.")]
|
|
||||||
public double Base { get; set; } = 10;
|
|
||||||
|
|
||||||
public override ExitCode Execute()
|
|
||||||
{
|
|
||||||
var result = Math.Log(Value, Base);
|
|
||||||
Console.WriteLine(result.ToString(CultureInfo.InvariantCulture));
|
|
||||||
|
|
||||||
return ExitCode.Success;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +1,21 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CliFx.Tests.Dummy
|
namespace CliFx.Tests.Dummy
|
||||||
{
|
{
|
||||||
public static class Program
|
public static partial class Program
|
||||||
{
|
{
|
||||||
public static Task<int> Main(string[] args) => new CliApplication().RunAsync(args);
|
public static Assembly Assembly { get; } = typeof(Program).Assembly;
|
||||||
|
|
||||||
|
public static string Location { get; } = Assembly.Location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static partial class Program
|
||||||
|
{
|
||||||
|
public static async Task Main() =>
|
||||||
|
await new CliApplicationBuilder()
|
||||||
|
.AddCommandsFromThisAssembly()
|
||||||
|
.Build()
|
||||||
|
.RunAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
155
CliFx.Tests/ApplicationSpecs.Commands.cs
Normal file
155
CliFx.Tests/ApplicationSpecs.Commands.cs
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ApplicationSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class NonImplementedCommand
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private class NonAnnotatedCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("dup")]
|
||||||
|
private class DuplicateNameCommandA : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("dup")]
|
||||||
|
private class DuplicateNameCommandB : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateParameterOrderCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateParameterNameCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "param")]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1, Name = "param")]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class MultipleNonScalarParametersCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public IReadOnlyList<string>? ParameterB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class NonLastNonScalarParameterCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
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]
|
||||||
|
private class DuplicateOptionNamesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("fruits")]
|
||||||
|
public string? Apples { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("fruits")]
|
||||||
|
public string? Oranges { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateOptionShortNamesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class ValidCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("hidden", Description = "Description")]
|
||||||
|
private class HiddenPropertiesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13, Name = "param", Description = "Param description")]
|
||||||
|
public string? Parameter { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option", 'o', Description = "Option description", EnvironmentVariableName = "ENV")]
|
||||||
|
public string? Option { get; set; }
|
||||||
|
|
||||||
|
public string? HiddenA { get; set; }
|
||||||
|
|
||||||
|
public bool? HiddenB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
235
CliFx.Tests/ApplicationSpecs.cs
Normal file
235
CliFx.Tests/ApplicationSpecs.cs
Normal file
@@ -0,0 +1,235 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using CliFx.Domain;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ApplicationSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public ApplicationSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Application_can_be_created_with_a_default_configuration()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var app = new CliApplicationBuilder()
|
||||||
|
.AddCommandsFromThisAssembly()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
app.Should().NotBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Application_can_be_created_with_a_custom_configuration()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var app = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ValidCommand))
|
||||||
|
.AddCommandsFrom(typeof(ValidCommand).Assembly)
|
||||||
|
.AddCommands(new[] {typeof(ValidCommand)})
|
||||||
|
.AddCommandsFrom(new[] {typeof(ValidCommand).Assembly})
|
||||||
|
.AddCommandsFromThisAssembly()
|
||||||
|
.AllowDebugMode()
|
||||||
|
.AllowPreviewMode()
|
||||||
|
.UseTitle("test")
|
||||||
|
.UseExecutableName("test")
|
||||||
|
.UseVersionText("test")
|
||||||
|
.UseDescription("test")
|
||||||
|
.UseConsole(new VirtualConsole(Stream.Null))
|
||||||
|
.UseTypeActivator(Activator.CreateInstance!)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
app.Should().NotBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void At_least_one_command_must_be_defined_in_an_application()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = Array.Empty<Type>();
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Commands_must_implement_the_corresponding_interface()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Commands_must_be_annotated_by_an_attribute()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Commands_must_have_unique_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameters_must_have_unique_order()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameters_must_have_unique_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
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]
|
||||||
|
public void Command_options_must_have_unique_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_must_have_unique_short_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_must_have_unique_environment_variable_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_and_parameters_must_be_annotated_by_corresponding_attributes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(HiddenPropertiesCommand)};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var schema = ApplicationSchema.Resolve(commandTypes);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
schema.Should().BeEquivalentTo(new ApplicationSchema(new[]
|
||||||
|
{
|
||||||
|
new CommandSchema(
|
||||||
|
typeof(HiddenPropertiesCommand),
|
||||||
|
"hidden",
|
||||||
|
"Description",
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
new CommandParameterSchema(
|
||||||
|
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter))!,
|
||||||
|
13,
|
||||||
|
"param",
|
||||||
|
"Param description")
|
||||||
|
},
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
new CommandOptionSchema(
|
||||||
|
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option))!,
|
||||||
|
"option",
|
||||||
|
'o',
|
||||||
|
"ENV",
|
||||||
|
false,
|
||||||
|
"Option description")
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
schema.ToString().Should().NotBeNullOrWhiteSpace(); // this is only for coverage, I'm sorry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
191
CliFx.Tests/ArgumentBindingSpecs.Commands.cs
Normal file
191
CliFx.Tests/ArgumentBindingSpecs.Commands.cs
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ArgumentBindingSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class AllSupportedTypesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(Object))]
|
||||||
|
public object? Object { get; set; } = 42;
|
||||||
|
|
||||||
|
[CommandOption(nameof(String))]
|
||||||
|
public string? String { get; set; } = "foo bar";
|
||||||
|
|
||||||
|
[CommandOption(nameof(Bool))]
|
||||||
|
public bool Bool { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Char))]
|
||||||
|
public char Char { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Sbyte))]
|
||||||
|
public sbyte Sbyte { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Byte))]
|
||||||
|
public byte Byte { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Short))]
|
||||||
|
public short Short { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Ushort))]
|
||||||
|
public ushort Ushort { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Int))]
|
||||||
|
public int Int { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Uint))]
|
||||||
|
public uint Uint { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Long))]
|
||||||
|
public long Long { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Ulong))]
|
||||||
|
public ulong Ulong { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Float))]
|
||||||
|
public float Float { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Double))]
|
||||||
|
public double Double { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Decimal))]
|
||||||
|
public decimal Decimal { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(DateTime))]
|
||||||
|
public DateTime DateTime { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(DateTimeOffset))]
|
||||||
|
public DateTimeOffset DateTimeOffset { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TimeSpan))]
|
||||||
|
public TimeSpan TimeSpan { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(CustomEnum))]
|
||||||
|
public CustomEnum CustomEnum { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(IntNullable))]
|
||||||
|
public int? IntNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(CustomEnumNullable))]
|
||||||
|
public CustomEnum? CustomEnumNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TimeSpanNullable))]
|
||||||
|
public TimeSpan? TimeSpanNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringConstructable))]
|
||||||
|
public StringConstructable? TestStringConstructable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringParseable))]
|
||||||
|
public StringParseable? TestStringParseable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringParseableWithFormatProvider))]
|
||||||
|
public StringParseableWithFormatProvider? TestStringParseableWithFormatProvider { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(ObjectArray))]
|
||||||
|
public object[]? ObjectArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringArray))]
|
||||||
|
public string[]? StringArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(IntArray))]
|
||||||
|
public int[]? IntArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(CustomEnumArray))]
|
||||||
|
public CustomEnum[]? CustomEnumArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(IntNullableArray))]
|
||||||
|
public int?[]? IntNullableArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringConstructableArray))]
|
||||||
|
public StringConstructable[]? TestStringConstructableArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Enumerable))]
|
||||||
|
public IEnumerable? Enumerable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringEnumerable))]
|
||||||
|
public IEnumerable<string>? StringEnumerable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringReadOnlyList))]
|
||||||
|
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringList))]
|
||||||
|
public List<string>? StringList { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringHashSet))]
|
||||||
|
public HashSet<string>? StringHashSet { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class ArrayOptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option", 'o')]
|
||||||
|
public IReadOnlyList<string>? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class RequiredOptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(OptionA))]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(OptionB), IsRequired = true)]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class ParametersCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(2)]
|
||||||
|
public IReadOnlyList<string>? ParameterC { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class UnsupportedPropertyTypeCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(Option))]
|
||||||
|
public DummyType? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class UnsupportedEnumerablePropertyTypeCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(Option))]
|
||||||
|
public CustomEnumerable<string>? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class NoParameterCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(OptionA))]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(OptionB))]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
53
CliFx.Tests/ArgumentBindingSpecs.Types.cs
Normal file
53
CliFx.Tests/ArgumentBindingSpecs.Types.cs
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ArgumentBindingSpecs
|
||||||
|
{
|
||||||
|
private enum CustomEnum
|
||||||
|
{
|
||||||
|
Value1 = 1,
|
||||||
|
Value2 = 2,
|
||||||
|
Value3 = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
private class StringConstructable
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
public StringConstructable(string value) => Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class StringParseable
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
private StringParseable(string value) => Value = value;
|
||||||
|
|
||||||
|
public static StringParseable Parse(string value) => new StringParseable(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class StringParseableWithFormatProvider
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
private StringParseableWithFormatProvider(string value) => Value = value;
|
||||||
|
|
||||||
|
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||||
|
new StringParseableWithFormatProvider(value + " " + formatProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DummyType
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomEnumerable<T> : IEnumerable<T>
|
||||||
|
{
|
||||||
|
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1104
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
1104
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
File diff suppressed because it is too large
Load Diff
315
CliFx.Tests/ArgumentSyntaxSpecs.cs
Normal file
315
CliFx.Tests/ArgumentSyntaxSpecs.cs
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
using System;
|
||||||
|
using CliFx.Domain;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class ArgumentSyntaxSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Input_is_empty_if_no_arguments_are_provided()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var args = Array.Empty<string>();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(args);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(CommandLineInput.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] DirectivesTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]", "[debug]"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.AddDirective("debug")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(DirectivesTestData))]
|
||||||
|
internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] OptionsTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option", "value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option", "value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option", "value1", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option", "value1", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option", "same value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option", "same value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "--option2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1")
|
||||||
|
.AddOption("option2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "value1", "--option2", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1", "value1")
|
||||||
|
.AddOption("option2", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "value1", "value2", "--option2", "value3", "value4"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1", "value1", "value2")
|
||||||
|
.AddOption("option2", "value3", "value4")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "value1", "value2", "--option2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1", "value1", "value2")
|
||||||
|
.AddOption("option2")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(OptionsTestData))]
|
||||||
|
internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] ShortOptionsTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o", "value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o", "value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o", "value1", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o", "value1", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o", "same value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o", "same value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "-b"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "value1", "-b", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a", "value1")
|
||||||
|
.AddOption("b", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "value1", "value2", "-b", "value3", "value4"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a", "value1", "value2")
|
||||||
|
.AddOption("b", "value3", "value4")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "value1", "value2", "-b"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a", "value1", "value2")
|
||||||
|
.AddOption("b")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-abc"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-abc", "value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c", "value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-abc", "value1", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c", "value1", "value2")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(ShortOptionsTestData))]
|
||||||
|
internal void Option_can_be_set_by_specifying_its_short_name_after_a_single_dash(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] UnboundArgumentsTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"foo"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"foo", "bar"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.AddUnboundArgument("bar")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]", "foo"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"foo", "--option", "value", "-abc"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.AddOption("option", "value")
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]", "[debug]", "foo", "bar", "--option", "value", "-abc"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.AddDirective("debug")
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.AddUnboundArgument("bar")
|
||||||
|
.AddOption("option", "value")
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(UnboundArgumentsTestData))]
|
||||||
|
internal void Any_remaining_arguments_are_treated_as_unbound_arguments(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
CliFx.Tests/CancellationSpecs.Commands.cs
Normal file
27
CliFx.Tests/CancellationSpecs.Commands.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class CancellationSpecs
|
||||||
|
{
|
||||||
|
[Command("cancel")]
|
||||||
|
private class CancellableCommand : ICommand
|
||||||
|
{
|
||||||
|
public async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(3), console.GetCancellationToken());
|
||||||
|
console.Output.WriteLine("Never printed");
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine("Cancellation requested");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
CliFx.Tests/CancellationSpecs.cs
Normal file
41
CliFx.Tests/CancellationSpecs.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class CancellationSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(CancellableCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
|
||||||
|
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"cancel"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOutData.Should().Be("Cancellation requested");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestObjects;
|
|
||||||
using Moq;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CliApplicationTests
|
|
||||||
{
|
|
||||||
[Test]
|
|
||||||
public async Task RunAsync_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var command = new TestCommand();
|
|
||||||
var expectedExitCode = await command.ExecuteAsync();
|
|
||||||
|
|
||||||
var commandResolverMock = new Mock<ICommandResolver>();
|
|
||||||
commandResolverMock.Setup(m => m.ResolveCommand(It.IsAny<IReadOnlyList<string>>())).Returns(command);
|
|
||||||
var commandResolver = commandResolverMock.Object;
|
|
||||||
|
|
||||||
var application = new CliApplication(commandResolver);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var exitCodeValue = await application.RunAsync();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.That(exitCodeValue, Is.EqualTo(expectedExitCode.Value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +1,27 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net45</TargetFramework>
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<IsTestProject>true</IsTestProject>
|
<IsTestProject>true</IsTestProject>
|
||||||
<LangVersion>latest</LangVersion>
|
<CollectCoverage>true</CollectCoverage>
|
||||||
|
<CoverletOutputFormat>opencover</CoverletOutputFormat>
|
||||||
|
<CoverletOutput>bin/$(Configuration)/Coverage.xml</CoverletOutput>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
|
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||||
<PackageReference Include="NUnit" Version="3.11.0" />
|
</ItemGroup>
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
|
|
||||||
<PackageReference Include="Moq" Version="4.11.0" />
|
<ItemGroup>
|
||||||
<PackageReference Include="CliWrap" Version="2.3.0" />
|
<PackageReference Include="CliWrap" Version="3.0.0" />
|
||||||
|
<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="xunit" 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>
|
||||||
@@ -20,4 +29,12 @@
|
|||||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="../CliFx.Tests.Dummy/bin/$(Configuration)/$(TargetFramework)/CliFx.Tests.Dummy.runtimeconfig.json">
|
||||||
|
<Link>CliFx.Tests.Dummy.runtimeconfig.json</Link>
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
<Visible>False</Visible>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestObjects;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandOptionConverterTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetData_ConvertOption()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData("value", typeof(string), "value")
|
|
||||||
.SetName("To string");
|
|
||||||
|
|
||||||
yield return new TestCaseData("value", typeof(object), "value")
|
|
||||||
.SetName("To object");
|
|
||||||
|
|
||||||
yield return new TestCaseData("true", typeof(bool), true)
|
|
||||||
.SetName("To bool (true)");
|
|
||||||
|
|
||||||
yield return new TestCaseData("false", typeof(bool), false)
|
|
||||||
.SetName("To bool (false)");
|
|
||||||
|
|
||||||
yield return new TestCaseData(null, typeof(bool), true)
|
|
||||||
.SetName("To bool (switch)");
|
|
||||||
|
|
||||||
yield return new TestCaseData("123", typeof(int), 123)
|
|
||||||
.SetName("To int");
|
|
||||||
|
|
||||||
yield return new TestCaseData("123.45", typeof(double), 123.45)
|
|
||||||
.SetName("To double");
|
|
||||||
|
|
||||||
yield return new TestCaseData("28 Apr 1995", typeof(DateTime), new DateTime(1995, 04, 28))
|
|
||||||
.SetName("To DateTime");
|
|
||||||
|
|
||||||
yield return new TestCaseData("28 Apr 1995", typeof(DateTimeOffset), new DateTimeOffset(new DateTime(1995, 04, 28)))
|
|
||||||
.SetName("To DateTimeOffset");
|
|
||||||
|
|
||||||
yield return new TestCaseData("00:14:59", typeof(TimeSpan), new TimeSpan(00, 14, 59))
|
|
||||||
.SetName("To TimeSpan");
|
|
||||||
|
|
||||||
yield return new TestCaseData("value2", typeof(TestEnum), TestEnum.Value2)
|
|
||||||
.SetName("To enum");
|
|
||||||
|
|
||||||
yield return new TestCaseData("666", typeof(int?), 666)
|
|
||||||
.SetName("To int? (with value)");
|
|
||||||
|
|
||||||
yield return new TestCaseData(null, typeof(int?), null)
|
|
||||||
.SetName("To int? (no value)");
|
|
||||||
|
|
||||||
yield return new TestCaseData("value3", typeof(TestEnum?), TestEnum.Value3)
|
|
||||||
.SetName("To enum? (with value)");
|
|
||||||
|
|
||||||
yield return new TestCaseData(null, typeof(TestEnum?), null)
|
|
||||||
.SetName("To enum? (no value)");
|
|
||||||
|
|
||||||
yield return new TestCaseData("01:00:00", typeof(TimeSpan?), new TimeSpan(01, 00, 00))
|
|
||||||
.SetName("To TimeSpan? (with value)");
|
|
||||||
|
|
||||||
yield return new TestCaseData(null, typeof(TimeSpan?), null)
|
|
||||||
.SetName("To TimeSpan? (no value)");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetData_ConvertOption))]
|
|
||||||
public void ConvertOption_Test(string value, Type targetType, object expectedConvertedValue)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var converter = new CommandOptionConverter();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var convertedValue = converter.ConvertOption(value, targetType);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.That(convertedValue, Is.EqualTo(expectedConvertedValue));
|
|
||||||
|
|
||||||
if (convertedValue != null)
|
|
||||||
Assert.That(convertedValue, Is.AssignableTo(targetType));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandOptionParserTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetData_ParseOptions()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new string[0],
|
|
||||||
CommandOptionSet.Empty
|
|
||||||
).SetName("No arguments");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--argument", "value"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"argument", "value"}
|
|
||||||
})
|
|
||||||
).SetName("Single argument");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--argument1", "value1", "--argument2", "value2", "--argument3", "value3"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"argument1", "value1"},
|
|
||||||
{"argument2", "value2"},
|
|
||||||
{"argument3", "value3"}
|
|
||||||
})
|
|
||||||
).SetName("Multiple arguments");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"a", "value"}
|
|
||||||
})
|
|
||||||
).SetName("Single short argument");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value1", "-b", "value2", "-c", "value3"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"a", "value1"},
|
|
||||||
{"b", "value2"},
|
|
||||||
{"c", "value3"}
|
|
||||||
})
|
|
||||||
).SetName("Multiple short arguments");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--argument1", "value1", "-b", "value2", "--argument3", "value3"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"argument1", "value1"},
|
|
||||||
{"b", "value2"},
|
|
||||||
{"argument3", "value3"}
|
|
||||||
})
|
|
||||||
).SetName("Multiple mixed arguments");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--switch"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"switch", null}
|
|
||||||
})
|
|
||||||
).SetName("Single switch");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--switch1", "--switch2", "--switch3"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"switch1", null},
|
|
||||||
{"switch2", null},
|
|
||||||
{"switch3", null}
|
|
||||||
})
|
|
||||||
).SetName("Multiple switches");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-s"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"s", null}
|
|
||||||
})
|
|
||||||
).SetName("Single short switch");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "-b", "-c"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"a", null},
|
|
||||||
{"b", null},
|
|
||||||
{"c", null}
|
|
||||||
})
|
|
||||||
).SetName("Multiple short switches");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-abc"},
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"a", null},
|
|
||||||
{"b", null},
|
|
||||||
{"c", null}
|
|
||||||
})
|
|
||||||
).SetName("Multiple stacked short switches");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"command"},
|
|
||||||
new CommandOptionSet("command")
|
|
||||||
).SetName("No arguments (with command name)");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"command", "--argument", "value"},
|
|
||||||
new CommandOptionSet("command", new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"argument", "value"}
|
|
||||||
})
|
|
||||||
).SetName("Single argument (with command name)");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetData_ParseOptions))]
|
|
||||||
public void ParseOptions_Test(IReadOnlyList<string> commandLineArguments, CommandOptionSet expectedCommandOptionSet)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var parser = new CommandOptionParser();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var optionSet = parser.ParseOptions(commandLineArguments);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.That(optionSet.CommandName, Is.EqualTo(expectedCommandOptionSet.CommandName));
|
|
||||||
Assert.That(optionSet.Options, Is.EqualTo(expectedCommandOptionSet.Options));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,116 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
using CliFx.Models;
|
|
||||||
using CliFx.Services;
|
|
||||||
using CliFx.Tests.TestObjects;
|
|
||||||
using Moq;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CommandResolverTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetData_ResolveCommand()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"int", "13"}
|
|
||||||
}),
|
|
||||||
new TestCommand {IntOption = 13}
|
|
||||||
).SetName("Single option");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"int", "13"},
|
|
||||||
{"str", "hello world" }
|
|
||||||
}),
|
|
||||||
new TestCommand { IntOption = 13, StringOption = "hello world"}
|
|
||||||
).SetName("Multiple options");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"i", "13"}
|
|
||||||
}),
|
|
||||||
new TestCommand { IntOption = 13 }
|
|
||||||
).SetName("Single short option");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionSet("command", new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"int", "13"}
|
|
||||||
}),
|
|
||||||
new TestCommand { IntOption = 13 }
|
|
||||||
).SetName("Single option (with command name)");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetData_ResolveCommand))]
|
|
||||||
public void ResolveCommand_Test(CommandOptionSet commandOptionSet, TestCommand expectedCommand)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] {typeof(TestCommand)};
|
|
||||||
|
|
||||||
var typeProviderMock = new Mock<ITypeProvider>();
|
|
||||||
typeProviderMock.Setup(m => m.GetTypes()).Returns(commandTypes);
|
|
||||||
var typeProvider = typeProviderMock.Object;
|
|
||||||
|
|
||||||
var optionParserMock = new Mock<ICommandOptionParser>();
|
|
||||||
optionParserMock.Setup(m => m.ParseOptions(It.IsAny<IReadOnlyList<string>>())).Returns(commandOptionSet);
|
|
||||||
var optionParser = optionParserMock.Object;
|
|
||||||
|
|
||||||
var optionConverter = new CommandOptionConverter();
|
|
||||||
|
|
||||||
var resolver = new CommandResolver(typeProvider, optionParser, optionConverter);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var command = resolver.ResolveCommand() as TestCommand;
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.That(command, Is.Not.Null);
|
|
||||||
Assert.That(command.StringOption, Is.EqualTo(expectedCommand.StringOption), nameof(command.StringOption));
|
|
||||||
Assert.That(command.IntOption, Is.EqualTo(expectedCommand.IntOption), nameof(command.IntOption));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetData_ResolveCommand_IsRequired()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
CommandOptionSet.Empty
|
|
||||||
).SetName("No options");
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new CommandOptionSet(new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{"str", "hello world"}
|
|
||||||
})
|
|
||||||
).SetName("Required option is not set");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetData_ResolveCommand_IsRequired))]
|
|
||||||
public void ResolveCommand_IsRequired_Test(CommandOptionSet commandOptionSet)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var commandTypes = new[] { typeof(TestCommand) };
|
|
||||||
|
|
||||||
var typeProviderMock = new Mock<ITypeProvider>();
|
|
||||||
typeProviderMock.Setup(m => m.GetTypes()).Returns(commandTypes);
|
|
||||||
var typeProvider = typeProviderMock.Object;
|
|
||||||
|
|
||||||
var optionParserMock = new Mock<ICommandOptionParser>();
|
|
||||||
optionParserMock.Setup(m => m.ParseOptions(It.IsAny<IReadOnlyList<string>>())).Returns(commandOptionSet);
|
|
||||||
var optionParser = optionParserMock.Object;
|
|
||||||
|
|
||||||
var optionConverter = new CommandOptionConverter();
|
|
||||||
|
|
||||||
var resolver = new CommandResolver(typeProvider, optionParser, optionConverter);
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
Assert.Throws<CommandResolveException>(() => resolver.ResolveCommand());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
72
CliFx.Tests/ConsoleSpecs.cs
Normal file
72
CliFx.Tests/ConsoleSpecs.cs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliWrap;
|
||||||
|
using CliWrap.Buffered;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class ConsoleSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Real_implementation_of_console_maps_directly_to_system_console()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var command = "Hello world" | Cli.Wrap("dotnet")
|
||||||
|
.WithArguments(a => a
|
||||||
|
.Add(Dummy.Program.Location)
|
||||||
|
.Add("console-test"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await command.ExecuteBufferedAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.StandardOutput.TrimEnd().Should().Be("Hello world");
|
||||||
|
result.StandardError.TrimEnd().Should().Be("Hello world");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Fake_implementation_of_console_can_be_used_to_execute_commands_in_isolation()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stdIn = new MemoryStream(Console.InputEncoding.GetBytes("input"));
|
||||||
|
using var stdOut = new MemoryStream();
|
||||||
|
using var stdErr = new MemoryStream();
|
||||||
|
|
||||||
|
var console = new VirtualConsole(
|
||||||
|
input: stdIn,
|
||||||
|
output: stdOut,
|
||||||
|
error: stdErr);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
console.Output.Write("output");
|
||||||
|
console.Error.Write("error");
|
||||||
|
|
||||||
|
var stdInData = console.Input.ReadToEnd();
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray());
|
||||||
|
|
||||||
|
console.ResetColor();
|
||||||
|
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||||
|
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdInData.Should().Be("input");
|
||||||
|
stdOutData.Should().Be("output");
|
||||||
|
stdErrData.Should().Be("error");
|
||||||
|
|
||||||
|
console.Input.Should().NotBeSameAs(Console.In);
|
||||||
|
console.Output.Should().NotBeSameAs(Console.Out);
|
||||||
|
console.Error.Should().NotBeSameAs(Console.Error);
|
||||||
|
|
||||||
|
console.IsInputRedirected.Should().BeTrue();
|
||||||
|
console.IsOutputRedirected.Should().BeTrue();
|
||||||
|
console.IsErrorRedirected.Should().BeTrue();
|
||||||
|
|
||||||
|
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
|
||||||
|
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
37
CliFx.Tests/DependencyInjectionSpecs.Commands.cs
Normal file
37
CliFx.Tests/DependencyInjectionSpecs.Commands.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DependencyInjectionSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class WithoutDependenciesCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DependencyA
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DependencyB
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class WithDependenciesCommand : ICommand
|
||||||
|
{
|
||||||
|
private readonly DependencyA _dependencyA;
|
||||||
|
private readonly DependencyB _dependencyB;
|
||||||
|
|
||||||
|
public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
|
||||||
|
{
|
||||||
|
_dependencyA = dependencyA;
|
||||||
|
_dependencyB = dependencyB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
63
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
63
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
using CliFx.Exceptions;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DependencyInjectionSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public DependencyInjectionSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Default_type_activator_can_initialize_a_command_if_it_has_a_parameterless_constructor()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DefaultTypeActivator();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var obj = activator.CreateInstance(typeof(WithoutDependenciesCommand));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
obj.Should().BeOfType<WithoutDependenciesCommand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Default_type_activator_cannot_initialize_a_command_if_it_does_not_have_a_parameterless_constructor()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DefaultTypeActivator();
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delegate_type_activator_can_initialize_a_command_using_a_custom_function()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DelegateTypeActivator(_ =>
|
||||||
|
new WithDependenciesCommand(new DependencyA(), new DependencyB()));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var obj = activator.CreateInstance(typeof(WithDependenciesCommand));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
obj.Should().BeOfType<WithDependenciesCommand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delegate_type_activator_throws_if_the_underlying_function_returns_null()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DelegateTypeActivator(_ => null!);
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||||
|
_output.WriteLine(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/DirectivesSpecs.Commands.cs
Normal file
14
CliFx.Tests/DirectivesSpecs.Commands.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DirectivesSpecs
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
private class NamedCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
CliFx.Tests/DirectivesSpecs.cs
Normal file
36
CliFx.Tests/DirectivesSpecs.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DirectivesSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Preview_directive_can_be_enabled_to_print_provided_arguments_as_they_were_parsed()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.AllowPreviewMode()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().ContainAll("cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option foo]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
using System.IO;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliWrap;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class DummyTests
|
|
||||||
{
|
|
||||||
private string DummyFilePath => Path.Combine(TestContext.CurrentContext.TestDirectory, "CliFx.Tests.Dummy.exe");
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCase("", "Hello world")]
|
|
||||||
[TestCase("-t .NET", "Hello .NET")]
|
|
||||||
[TestCase("-e", "Hello world!!!")]
|
|
||||||
[TestCase("add --a 1 --b 2", "3")]
|
|
||||||
[TestCase("add --a 2.75 --b 3.6", "6.35")]
|
|
||||||
[TestCase("log --value 100", "2")]
|
|
||||||
[TestCase("log --value 256 --base 2", "8")]
|
|
||||||
public async Task Execute_Test(string arguments, string expectedOutput)
|
|
||||||
{
|
|
||||||
// Act
|
|
||||||
var result = await Cli.Wrap(DummyFilePath).SetArguments(arguments).ExecuteAsync();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
Assert.That(result.ExitCode, Is.Zero);
|
|
||||||
Assert.That(result.StandardOutput.Trim(), Is.EqualTo(expectedOutput));
|
|
||||||
Assert.That(result.StandardError.Trim(), Is.Empty);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
27
CliFx.Tests/EnvironmentVariablesSpecs.Commands.cs
Normal file
27
CliFx.Tests/EnvironmentVariablesSpecs.Commands.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class EnvironmentVariablesSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class EnvironmentVariableCollectionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
|
||||||
|
public IReadOnlyList<string>? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class EnvironmentVariableCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
|
||||||
|
public string? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
96
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
96
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Domain;
|
||||||
|
using CliWrap;
|
||||||
|
using CliWrap.Buffered;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class EnvironmentVariablesSpecs
|
||||||
|
{
|
||||||
|
// This test uses a real application to make sure environment variables are actually read correctly
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_can_use_a_specific_environment_variable_as_fallback()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var command = Cli.Wrap("dotnet")
|
||||||
|
.WithArguments(a => a
|
||||||
|
.Add(Dummy.Program.Location))
|
||||||
|
.WithEnvironmentVariables(e => e
|
||||||
|
.Set("ENV_TARGET", "Mars"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOut.TrimEnd().Should().Be("Hello Mars!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test uses a real application to make sure environment variables are actually read correctly
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_only_uses_environment_variable_as_fallback_if_the_value_was_not_directly_provided()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var command = Cli.Wrap("dotnet")
|
||||||
|
.WithArguments(a => a
|
||||||
|
.Add(Dummy.Program.Location)
|
||||||
|
.Add("--target")
|
||||||
|
.Add("Jupiter"))
|
||||||
|
.WithEnvironmentVariables(e => e
|
||||||
|
.Set("ENV_TARGET", "Mars"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOut.TrimEnd().Should().Be("Hello Jupiter!");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Option_of_non_scalar_type_can_take_multiple_separated_values_from_an_environment_variable()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCollectionCommand)});
|
||||||
|
|
||||||
|
var input = CommandLineInput.Empty;
|
||||||
|
var envVars = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var command = schema.InitializeEntryPoint(input, envVars);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
command.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
|
||||||
|
{
|
||||||
|
Option = new[] {"foo", "bar"}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Option_of_scalar_type_can_only_take_a_single_value_from_an_environment_variable_even_if_it_contains_separators()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCommand)});
|
||||||
|
|
||||||
|
var input = CommandLineInput.Empty;
|
||||||
|
var envVars = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var command = schema.InitializeEntryPoint(input, envVars);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
command.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||||
|
{
|
||||||
|
Option = $"foo{Path.PathSeparator}bar"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
77
CliFx.Tests/ErrorReportingSpecs.Commands.cs
Normal file
77
CliFx.Tests/ErrorReportingSpecs.Commands.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ErrorReportingSpecs
|
||||||
|
{
|
||||||
|
[Command("exc")]
|
||||||
|
private class GenericExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("msg", 'm')]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("exc")]
|
||||||
|
private class CommandExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("code", 'c')]
|
||||||
|
public int ExitCode { get; set; } = 1337;
|
||||||
|
|
||||||
|
[CommandOption("msg", 'm')]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
238
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
238
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
@@ -0,0 +1,238 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ErrorReportingSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public ErrorReportingSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace()
|
||||||
|
{
|
||||||
|
// 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_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(CommandExceptionCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"exc", "-m", "Kaput", "-c", "69"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(69);
|
||||||
|
stdErrData.Should().Be("Kaput");
|
||||||
|
|
||||||
|
_output.WriteLine(stdErrData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(CommandExceptionCommand))
|
||||||
|
.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().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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
114
CliFx.Tests/HelpTextSpecs.Commands.cs
Normal file
114
CliFx.Tests/HelpTextSpecs.Commands.cs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class HelpTextSpecs
|
||||||
|
{
|
||||||
|
[Command(Description = "DefaultCommand description.")]
|
||||||
|
private class DefaultCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-a", 'a', Description = "OptionA description.")]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-b", 'b', Description = "OptionB description.")]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd", Description = "NamedCommand description.")]
|
||||||
|
private class NamedCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "param-a", Description = "ParameterA description.")]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-c", 'c', Description = "OptionC description.")]
|
||||||
|
public string? OptionC { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-d", 'd', Description = "OptionD description.")]
|
||||||
|
public string? OptionD { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd sub", Description = "NamedSubCommand description.")]
|
||||||
|
private class NamedSubCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "param-b", Description = "ParameterB description.")]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1, Name = "param-c", Description = "ParameterC description.")]
|
||||||
|
public string? ParameterC { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-e", 'e', Description = "OptionE description.")]
|
||||||
|
public string? OptionE { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd-with-params")]
|
||||||
|
private class ParametersCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "first")]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(10)]
|
||||||
|
public int? ParameterB { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(20, Description = "A list of numbers", Name = "third list")]
|
||||||
|
public IEnumerable<int>? ParameterC { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option", 'o')]
|
||||||
|
public string? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd-with-req-opts")]
|
||||||
|
private class RequiredOptionsCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-f", 'f', IsRequired = true)]
|
||||||
|
public string? OptionF { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-g", 'g', IsRequired = true)]
|
||||||
|
public IEnumerable<int>? OptionG { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-h", 'h')]
|
||||||
|
public string? OptionH { get; set; }
|
||||||
|
|
||||||
|
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")]
|
||||||
|
private class EnvironmentVariableCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-a", 'a', IsRequired = true, EnvironmentVariableName = "ENV_OPT_A")]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
297
CliFx.Tests/HelpTextSpecs.cs
Normal file
297
CliFx.Tests/HelpTextSpecs.cs
Normal file
@@ -0,0 +1,297 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class HelpTextSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseVersionText("v6.9")
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"--version"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Be("v6.9");
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_by_providing_the_help_option()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseTitle("AppTitle")
|
||||||
|
.UseVersionText("AppVer")
|
||||||
|
.UseDescription("AppDesc")
|
||||||
|
.UseExecutableName("AppExe")
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"AppTitle", "AppVer",
|
||||||
|
"AppDesc",
|
||||||
|
"Usage",
|
||||||
|
"AppExe", "[command]", "[options]",
|
||||||
|
"Options",
|
||||||
|
"-a|--option-a", "OptionA description.",
|
||||||
|
"-b|--option-b", "OptionB description.",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"--version", "Shows version information.",
|
||||||
|
"Commands",
|
||||||
|
"cmd", "NamedCommand description.",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_on_a_specific_named_command()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Description",
|
||||||
|
"NamedCommand description.",
|
||||||
|
"Usage",
|
||||||
|
"cmd", "[command]", "<param-a>", "[options]",
|
||||||
|
"Parameters",
|
||||||
|
"* param-a", "ParameterA description.",
|
||||||
|
"Options",
|
||||||
|
"-c|--option-c", "OptionC description.",
|
||||||
|
"-d|--option-d", "OptionD description.",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"Commands",
|
||||||
|
"sub", "SubCommand description.",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_on_a_specific_named_sub_command()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd", "sub", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Description",
|
||||||
|
"SubCommand description.",
|
||||||
|
"Usage",
|
||||||
|
"cmd sub", "<param-b>", "<param-c>", "[options]",
|
||||||
|
"Parameters",
|
||||||
|
"* param-b", "ParameterB description.",
|
||||||
|
"* param-c", "ParameterC description.",
|
||||||
|
"Options",
|
||||||
|
"-e|--option-e", "OptionE description.",
|
||||||
|
"-h|--help", "Shows help text."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_without_specifying_command_even_if_default_command_is_not_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"[command]",
|
||||||
|
"Options",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"--version", "Shows version information.",
|
||||||
|
"Commands",
|
||||||
|
"cmd", "NamedCommand description.",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ParametersCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd-with-params", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_all_required_options()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(RequiredOptionsCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd-with-req-opts", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"cmd-with-req-opts", "--option-f <value>", "--option-g <values...>", "[options]",
|
||||||
|
"Options",
|
||||||
|
"* -f|--option-f",
|
||||||
|
"* -g|--option-g",
|
||||||
|
"-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]
|
||||||
|
public async Task Help_text_lists_environment_variable_names_for_options_that_have_them_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(EnvironmentVariableCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd-with-env-vars", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Options",
|
||||||
|
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
||||||
|
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
||||||
|
);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
52
CliFx.Tests/RoutingSpecs.Commands.cs
Normal file
52
CliFx.Tests/RoutingSpecs.Commands.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class RoutingSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class DefaultCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine("Hello world!");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("concat", Description = "Concatenate strings.")]
|
||||||
|
private class ConcatCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
||||||
|
public IReadOnlyList<string> Inputs { get; set; } = Array.Empty<string>();
|
||||||
|
|
||||||
|
[CommandOption('s', Description = "String separator.")]
|
||||||
|
public string Separator { get; set; } = "";
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(string.Join(Separator, Inputs));
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("div", Description = "Divide one number by another.")]
|
||||||
|
private class DivideCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
||||||
|
public double Dividend { get; set; } = 0;
|
||||||
|
|
||||||
|
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
||||||
|
public double Divisor { get; set; } = 0;
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(Dividend / Divisor);
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
90
CliFx.Tests/RoutingSpecs.cs
Normal file
90
CliFx.Tests/RoutingSpecs.cs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class RoutingSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Default_command_is_executed_if_provided_arguments_do_not_match_any_named_command()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(ConcatCommand))
|
||||||
|
.AddCommand(typeof(DivideCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
Array.Empty<string>(),
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Be("Hello world!");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_is_printed_if_no_arguments_were_provided_and_default_command_is_not_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ConcatCommand))
|
||||||
|
.AddCommand(typeof(DivideCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.UseDescription("This will be visible in help")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
Array.Empty<string>(),
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Contain("This will be visible in help");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(ConcatCommand))
|
||||||
|
.AddCommand(typeof(DivideCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"concat", "-i", "foo", "bar", "-s", ", "},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Be("foo, bar");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Models;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestObjects
|
|
||||||
{
|
|
||||||
[DefaultCommand]
|
|
||||||
[Command("command")]
|
|
||||||
public class TestCommand : Command
|
|
||||||
{
|
|
||||||
[CommandOption("int", ShortName = 'i', IsRequired = true)]
|
|
||||||
public int IntOption { get; set; } = 24;
|
|
||||||
|
|
||||||
[CommandOption("str", ShortName = 's')]
|
|
||||||
public string StringOption { get; set; } = "foo bar";
|
|
||||||
|
|
||||||
public override ExitCode Execute() => new ExitCode(IntOption, StringOption);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace CliFx.Tests.TestObjects
|
|
||||||
{
|
|
||||||
public enum TestEnum
|
|
||||||
{
|
|
||||||
Value1,
|
|
||||||
Value2,
|
|
||||||
Value3
|
|
||||||
}
|
|
||||||
}
|
|
||||||
63
CliFx.Tests/UtilitiesSpecs.cs
Normal file
63
CliFx.Tests/UtilitiesSpecs.cs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using CliFx.Utilities;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
using Xunit.Abstractions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class UtilitiesSpecs
|
||||||
|
{
|
||||||
|
private readonly ITestOutputHelper _output;
|
||||||
|
|
||||||
|
public UtilitiesSpecs(ITestOutputHelper output) => _output = output;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Progress_ticker_can_be_used_to_report_progress_to_console()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut, isOutputRedirected: false);
|
||||||
|
|
||||||
|
var ticker = console.CreateProgressTicker();
|
||||||
|
|
||||||
|
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||||
|
var progressStringValues = progressValues.Select(p => p.ToString("P2")).ToArray();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
foreach (var progress in progressValues)
|
||||||
|
ticker.Report(progress);
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(progressStringValues);
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Progress_ticker_does_not_write_to_console_if_output_is_redirected()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var ticker = console.CreateProgressTicker();
|
||||||
|
|
||||||
|
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
foreach (var progress in progressValues)
|
||||||
|
ticker.Report(progress);
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().BeEmpty();
|
||||||
|
|
||||||
|
_output.WriteLine(stdOutData);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
5
CliFx.Tests/xunit.runner.json
Normal file
5
CliFx.Tests/xunit.runner.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||||
|
"methodDisplayOptions": "all",
|
||||||
|
"methodDisplay": "method"
|
||||||
|
}
|
||||||
11
CliFx.props
Normal file
11
CliFx.props
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<Version>1.2</Version>
|
||||||
|
<Company>Tyrrrz</Company>
|
||||||
|
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
||||||
|
<LangVersion>latest</LangVersion>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
85
CliFx.sln
85
CliFx.sln
@@ -7,15 +7,24 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx", "CliFx\CliFx.csproj
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Tests", "CliFx.Tests\CliFx.Tests.csproj", "{268CF863-65A5-49BB-93CF-08972B7756DC}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Tests", "CliFx.Tests\CliFx.Tests.csproj", "{268CF863-65A5-49BB-93CF-08972B7756DC}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{4904B3EB-3286-4F1B-8B74-6FF051C8E787}"
|
|
||||||
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
|
||||||
EndProjectSection
|
EndProjectSection
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Benchmarks", "CliFx.Benchmarks\CliFx.Benchmarks.csproj", "{8ACD6DC2-D768-4850-9223-5B7C83A78513}"
|
||||||
|
EndProject
|
||||||
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Demo", "CliFx.Demo\CliFx.Demo.csproj", "{AAB6844C-BF71-448F-A11B-89AEE459AB15}"
|
||||||
|
EndProject
|
||||||
|
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
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -50,18 +59,66 @@ Global
|
|||||||
{268CF863-65A5-49BB-93CF-08972B7756DC}.Release|x64.Build.0 = Release|Any CPU
|
{268CF863-65A5-49BB-93CF-08972B7756DC}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{268CF863-65A5-49BB-93CF-08972B7756DC}.Release|x86.ActiveCfg = Release|Any CPU
|
{268CF863-65A5-49BB-93CF-08972B7756DC}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{268CF863-65A5-49BB-93CF-08972B7756DC}.Release|x86.Build.0 = Release|Any CPU
|
{268CF863-65A5-49BB-93CF-08972B7756DC}.Release|x86.Build.0 = Release|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Debug|x64.ActiveCfg = Debug|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Debug|x64.Build.0 = Debug|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Debug|x86.ActiveCfg = Debug|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Debug|x86.Build.0 = Debug|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Release|Any CPU.Build.0 = Release|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Release|x64.ActiveCfg = Release|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Release|x64.Build.0 = Release|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Release|x86.ActiveCfg = Release|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{4904B3EB-3286-4F1B-8B74-6FF051C8E787}.Release|x86.Build.0 = Release|Any CPU
|
{8ACD6DC2-D768-4850-9223-5B7C83A78513}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{AAB6844C-BF71-448F-A11B-89AEE459AB15}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x64.ActiveCfg = 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.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
|
||||||
|
|||||||
38
CliFx/ApplicationConfiguration.cs
Normal file
38
CliFx/ApplicationConfiguration.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CliFx
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Configuration of an application.
|
||||||
|
/// </summary>
|
||||||
|
public class ApplicationConfiguration
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Command types defined in this application.
|
||||||
|
/// </summary>
|
||||||
|
public IReadOnlyList<Type> CommandTypes { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether debug mode is allowed in this application.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsDebugModeAllowed { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether preview mode is allowed in this application.
|
||||||
|
/// </summary>
|
||||||
|
public bool IsPreviewModeAllowed { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="ApplicationConfiguration"/>.
|
||||||
|
/// </summary>
|
||||||
|
public ApplicationConfiguration(
|
||||||
|
IReadOnlyList<Type> commandTypes,
|
||||||
|
bool isDebugModeAllowed, bool isPreviewModeAllowed)
|
||||||
|
{
|
||||||
|
CommandTypes = commandTypes;
|
||||||
|
IsDebugModeAllowed = isDebugModeAllowed;
|
||||||
|
IsPreviewModeAllowed = isPreviewModeAllowed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
39
CliFx/ApplicationMetadata.cs
Normal file
39
CliFx/ApplicationMetadata.cs
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
namespace CliFx
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Metadata associated with an application.
|
||||||
|
/// </summary>
|
||||||
|
public class ApplicationMetadata
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Application title.
|
||||||
|
/// </summary>
|
||||||
|
public string Title { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Application executable name.
|
||||||
|
/// </summary>
|
||||||
|
public string ExecutableName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Application version text.
|
||||||
|
/// </summary>
|
||||||
|
public string VersionText { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Application description.
|
||||||
|
/// </summary>
|
||||||
|
public string? Description { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="ApplicationMetadata"/>.
|
||||||
|
/// </summary>
|
||||||
|
public ApplicationMetadata(string title, string executableName, string versionText, string? description)
|
||||||
|
{
|
||||||
|
Title = title;
|
||||||
|
ExecutableName = executableName;
|
||||||
|
VersionText = versionText;
|
||||||
|
Description = description;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,14 +2,38 @@
|
|||||||
|
|
||||||
namespace CliFx.Attributes
|
namespace CliFx.Attributes
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Annotates a type that defines a command.
|
||||||
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||||
public class CommandAttribute : Attribute
|
public class CommandAttribute : Attribute
|
||||||
{
|
{
|
||||||
public string Name { get; }
|
/// <summary>
|
||||||
|
/// Command name.
|
||||||
|
/// If the name is not set, the command is treated as a default command, i.e. the one that gets executed when the user
|
||||||
|
/// does not specify a command name in the arguments.
|
||||||
|
/// All commands in an application must have different names. Likewise, only one command without a name is allowed.
|
||||||
|
/// </summary>
|
||||||
|
public string? Name { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Command description, which is used in help text.
|
||||||
|
/// </summary>
|
||||||
|
public string? Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CommandAttribute"/>.
|
||||||
|
/// </summary>
|
||||||
public CommandAttribute(string name)
|
public CommandAttribute(string name)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CommandAttribute"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CommandAttribute()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,20 +2,72 @@
|
|||||||
|
|
||||||
namespace CliFx.Attributes
|
namespace CliFx.Attributes
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Annotates a property that defines a command option.
|
||||||
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Property)]
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
public class CommandOptionAttribute : Attribute
|
public class CommandOptionAttribute : Attribute
|
||||||
{
|
{
|
||||||
public string Name { get; }
|
/// <summary>
|
||||||
|
/// Option name (must be longer than a single character).
|
||||||
|
/// 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).
|
||||||
|
/// </summary>
|
||||||
|
public string? Name { get; }
|
||||||
|
|
||||||
public char ShortName { get; set; }
|
/// <summary>
|
||||||
|
/// Option short name (single character).
|
||||||
|
/// 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).
|
||||||
|
/// </summary>
|
||||||
|
public char? ShortName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Whether an option is required.
|
||||||
|
/// </summary>
|
||||||
public bool IsRequired { get; set; }
|
public bool IsRequired { get; set; }
|
||||||
|
|
||||||
public string Description { get; set; }
|
/// <summary>
|
||||||
|
/// Option description, which is used in help text.
|
||||||
|
/// </summary>
|
||||||
|
public string? Description { get; set; }
|
||||||
|
|
||||||
public CommandOptionAttribute(string name)
|
/// <summary>
|
||||||
|
/// Environment variable that will be used as fallback if no option value is specified.
|
||||||
|
/// </summary>
|
||||||
|
public string? EnvironmentVariableName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
|
||||||
|
/// </summary>
|
||||||
|
private CommandOptionAttribute(string? name, char? shortName)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
|
ShortName = shortName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CommandOptionAttribute(string name, char shortName)
|
||||||
|
: this(name, (char?) shortName)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CommandOptionAttribute(string name)
|
||||||
|
: this(name, null)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CommandOptionAttribute"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CommandOptionAttribute(char shortName)
|
||||||
|
: this(null, (char?) shortName)
|
||||||
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
37
CliFx/Attributes/CommandParameterAttribute.cs
Normal file
37
CliFx/Attributes/CommandParameterAttribute.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace CliFx.Attributes
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Annotates a property that defines a command parameter.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Property)]
|
||||||
|
public class CommandParameterAttribute : Attribute
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Order of this parameter compared to other parameters.
|
||||||
|
/// All parameters in a command must have different order.
|
||||||
|
/// Parameter whose type is a non-scalar (e.g. array), must be the last in order and only one such parameter is allowed.
|
||||||
|
/// </summary>
|
||||||
|
public int Order { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter name, which is only used in help text.
|
||||||
|
/// If this isn't specified, property name is used instead.
|
||||||
|
/// </summary>
|
||||||
|
public string? Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Parameter description, which is used in help text.
|
||||||
|
/// </summary>
|
||||||
|
public string? Description { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CommandParameterAttribute"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CommandParameterAttribute(int order)
|
||||||
|
{
|
||||||
|
Order = order;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace CliFx.Attributes
|
|
||||||
{
|
|
||||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
|
||||||
public class DefaultCommandAttribute : Attribute
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +1,232 @@
|
|||||||
using System.Collections.Generic;
|
using System;
|
||||||
using System.Reflection;
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CliFx.Services;
|
using CliFx.Domain;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
namespace CliFx
|
namespace CliFx
|
||||||
{
|
{
|
||||||
public partial class CliApplication : ICliApplication
|
/// <summary>
|
||||||
|
/// Command line application facade.
|
||||||
|
/// </summary>
|
||||||
|
public class CliApplication
|
||||||
{
|
{
|
||||||
private readonly ICommandResolver _commandResolver;
|
private readonly ApplicationMetadata _metadata;
|
||||||
|
private readonly ApplicationConfiguration _configuration;
|
||||||
|
private readonly IConsole _console;
|
||||||
|
private readonly ITypeActivator _typeActivator;
|
||||||
|
|
||||||
public CliApplication(ICommandResolver commandResolver)
|
private readonly HelpTextWriter _helpTextWriter;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="CliApplication"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplication(
|
||||||
|
ApplicationMetadata metadata, ApplicationConfiguration configuration,
|
||||||
|
IConsole console, ITypeActivator typeActivator)
|
||||||
{
|
{
|
||||||
_commandResolver = commandResolver;
|
_metadata = metadata;
|
||||||
|
_configuration = configuration;
|
||||||
|
_console = console;
|
||||||
|
_typeActivator = typeActivator;
|
||||||
|
|
||||||
|
_helpTextWriter = new HelpTextWriter(metadata, console);
|
||||||
}
|
}
|
||||||
|
|
||||||
public CliApplication()
|
private async ValueTask<int?> HandleDebugDirectiveAsync(CommandLineInput commandLineInput)
|
||||||
: this(GetDefaultCommandResolver(Assembly.GetCallingAssembly()))
|
|
||||||
{
|
{
|
||||||
|
var isDebugMode = _configuration.IsDebugModeAllowed && commandLineInput.IsDebugDirectiveSpecified;
|
||||||
|
if (!isDebugMode)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
_console.WithForegroundColor(ConsoleColor.Green, () =>
|
||||||
|
_console.Output.WriteLine($"Attach debugger to PID {Process.GetCurrentProcess().Id} to continue."));
|
||||||
|
|
||||||
|
while (!Debugger.IsAttached)
|
||||||
|
await Task.Delay(100);
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> RunAsync(IReadOnlyList<string> commandLineArguments)
|
private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||||
{
|
{
|
||||||
// Resolve and execute command
|
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
|
||||||
var command = _commandResolver.ResolveCommand(commandLineArguments);
|
if (!isPreviewMode)
|
||||||
var exitCode = await command.ExecuteAsync();
|
return null;
|
||||||
|
|
||||||
// TODO: print message if error?
|
var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset);
|
||||||
|
|
||||||
return exitCode.Value;
|
_console.Output.WriteLine("Parser preview:");
|
||||||
|
|
||||||
|
// Command name
|
||||||
|
if (commandSchema != null && argumentOffset > 0)
|
||||||
|
{
|
||||||
|
_console.WithForegroundColor(ConsoleColor.Cyan, () =>
|
||||||
|
_console.Output.Write(commandSchema.Name));
|
||||||
|
|
||||||
|
_console.Output.Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
foreach (var parameter in commandLineInput.UnboundArguments.Skip(argumentOffset))
|
||||||
|
{
|
||||||
|
_console.Output.Write('<');
|
||||||
|
|
||||||
|
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||||
|
_console.Output.Write(parameter));
|
||||||
|
|
||||||
|
_console.Output.Write('>');
|
||||||
|
_console.Output.Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Options
|
||||||
|
foreach (var option in commandLineInput.Options)
|
||||||
|
{
|
||||||
|
_console.Output.Write('[');
|
||||||
|
|
||||||
|
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||||
|
_console.Output.Write(option));
|
||||||
|
|
||||||
|
_console.Output.Write(']');
|
||||||
|
_console.Output.Write(' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
_console.Output.WriteLine();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public partial class CliApplication
|
private int? HandleVersionOption(CommandLineInput commandLineInput)
|
||||||
{
|
|
||||||
private static ICommandResolver GetDefaultCommandResolver(Assembly assembly)
|
|
||||||
{
|
{
|
||||||
var typeProvider = TypeProvider.FromAssembly(assembly);
|
// Version option is available only on the default command (i.e. when arguments are not specified)
|
||||||
var commandOptionParser = new CommandOptionParser();
|
var shouldRenderVersion = !commandLineInput.UnboundArguments.Any() && commandLineInput.IsVersionOptionSpecified;
|
||||||
var commandOptionConverter = new CommandOptionConverter();
|
if (!shouldRenderVersion)
|
||||||
|
return null;
|
||||||
|
|
||||||
return new CommandResolver(typeProvider, commandOptionParser, commandOptionConverter);
|
_console.Output.WriteLine(_metadata.VersionText);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int? HandleHelpOption(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||||
|
{
|
||||||
|
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
|
||||||
|
var shouldRenderHelp =
|
||||||
|
commandLineInput.IsHelpOptionSpecified ||
|
||||||
|
!applicationSchema.Commands.Any(c => c.IsDefault) && !commandLineInput.UnboundArguments.Any() && !commandLineInput.Options.Any();
|
||||||
|
|
||||||
|
if (!shouldRenderHelp)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Get the command schema that matches the input or use a dummy default command as a fallback
|
||||||
|
var commandSchema =
|
||||||
|
applicationSchema.TryFindCommand(commandLineInput) ??
|
||||||
|
CommandSchema.StubDefaultCommand;
|
||||||
|
|
||||||
|
_helpTextWriter.Write(applicationSchema, commandSchema);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async ValueTask<int> HandleCommandExecutionAsync(
|
||||||
|
ApplicationSchema applicationSchema,
|
||||||
|
CommandLineInput commandLineInput,
|
||||||
|
IReadOnlyDictionary<string, string> environmentVariables)
|
||||||
|
{
|
||||||
|
await applicationSchema
|
||||||
|
.InitializeEntryPoint(commandLineInput, environmentVariables, _typeActivator)
|
||||||
|
.ExecuteAsync(_console);
|
||||||
|
|
||||||
|
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>
|
||||||
|
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
||||||
|
/// </summary>
|
||||||
|
public async ValueTask<int> RunAsync(
|
||||||
|
IReadOnlyList<string> commandLineArguments,
|
||||||
|
IReadOnlyDictionary<string, string> environmentVariables)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
|
||||||
|
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
|
||||||
|
|
||||||
|
return
|
||||||
|
await HandleDebugDirectiveAsync(commandLineInput) ??
|
||||||
|
HandlePreviewDirective(applicationSchema, commandLineInput) ??
|
||||||
|
HandleVersionOption(commandLineInput) ??
|
||||||
|
HandleHelpOption(applicationSchema, commandLineInput) ??
|
||||||
|
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
||||||
|
}
|
||||||
|
catch (CliFxException cfe)
|
||||||
|
{
|
||||||
|
// 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.
|
||||||
|
var exitCode = HandleCliFxException(commandLineArguments, cfe);
|
||||||
|
return exitCode;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// For all other errors, we just write the entire thing to stderr.
|
||||||
|
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(ex.ToString()));
|
||||||
|
return ex.HResult;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Runs the application with specified command line arguments and returns the exit code.
|
||||||
|
/// Environment variables are retrieved automatically.
|
||||||
|
/// </summary>
|
||||||
|
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments)
|
||||||
|
{
|
||||||
|
var environmentVariables = Environment.GetEnvironmentVariables()
|
||||||
|
.Cast<DictionaryEntry>()
|
||||||
|
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
return await RunAsync(commandLineArguments, environmentVariables);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Runs the application and returns the exit code.
|
||||||
|
/// Command line arguments and environment variables are retrieved automatically.
|
||||||
|
/// </summary>
|
||||||
|
public async ValueTask<int> RunAsync()
|
||||||
|
{
|
||||||
|
var commandLineArguments = Environment.GetCommandLineArgs()
|
||||||
|
.Skip(1)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
return await RunAsync(commandLineArguments);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
200
CliFx/CliApplicationBuilder.cs
Normal file
200
CliFx/CliApplicationBuilder.cs
Normal file
@@ -0,0 +1,200 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using CliFx.Domain;
|
||||||
|
|
||||||
|
namespace CliFx
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Builds an instance of <see cref="CliApplication"/>.
|
||||||
|
/// </summary>
|
||||||
|
public partial class CliApplicationBuilder
|
||||||
|
{
|
||||||
|
private readonly HashSet<Type> _commandTypes = new HashSet<Type>();
|
||||||
|
|
||||||
|
private bool _isDebugModeAllowed = true;
|
||||||
|
private bool _isPreviewModeAllowed = true;
|
||||||
|
private string? _title;
|
||||||
|
private string? _executableName;
|
||||||
|
private string? _versionText;
|
||||||
|
private string? _description;
|
||||||
|
private IConsole? _console;
|
||||||
|
private ITypeActivator? _typeActivator;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a command of specified type to the application.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AddCommand(Type commandType)
|
||||||
|
{
|
||||||
|
_commandTypes.Add(commandType);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds multiple commands to the application.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AddCommands(IEnumerable<Type> commandTypes)
|
||||||
|
{
|
||||||
|
foreach (var commandType in commandTypes)
|
||||||
|
AddCommand(commandType);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds commands from the specified assembly to the application.
|
||||||
|
/// Only adds public valid command types.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AddCommandsFrom(Assembly commandAssembly)
|
||||||
|
{
|
||||||
|
foreach (var commandType in commandAssembly.ExportedTypes.Where(CommandSchema.IsCommandType))
|
||||||
|
AddCommand(commandType);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds commands from the specified assemblies to the application.
|
||||||
|
/// Only adds public valid command types.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AddCommandsFrom(IEnumerable<Assembly> commandAssemblies)
|
||||||
|
{
|
||||||
|
foreach (var commandAssembly in commandAssemblies)
|
||||||
|
AddCommandsFrom(commandAssembly);
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds commands from the calling assembly to the application.
|
||||||
|
/// Only adds public valid command types.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AddCommandsFromThisAssembly() => AddCommandsFrom(Assembly.GetCallingAssembly());
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies whether debug mode (enabled with [debug] directive) is allowed in the application.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AllowDebugMode(bool isAllowed = true)
|
||||||
|
{
|
||||||
|
_isDebugModeAllowed = isAllowed;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies whether preview mode (enabled with [preview] directive) is allowed in the application.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder AllowPreviewMode(bool isAllowed = true)
|
||||||
|
{
|
||||||
|
_isPreviewModeAllowed = isAllowed;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets application title, which appears in the help text.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder UseTitle(string title)
|
||||||
|
{
|
||||||
|
_title = title;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets application executable name, which appears in the help text.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder UseExecutableName(string executableName)
|
||||||
|
{
|
||||||
|
_executableName = executableName;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets application version text, which appears in the help text and when the user requests version information.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder UseVersionText(string versionText)
|
||||||
|
{
|
||||||
|
_versionText = versionText;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets application description, which appears in the help text.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder UseDescription(string? description)
|
||||||
|
{
|
||||||
|
_description = description;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configures the application to use the specified implementation of <see cref="IConsole"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder UseConsole(IConsole console)
|
||||||
|
{
|
||||||
|
_console = console;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configures the application to use the specified implementation of <see cref="ITypeActivator"/>.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder UseTypeActivator(ITypeActivator typeActivator)
|
||||||
|
{
|
||||||
|
_typeActivator = typeActivator;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Configures the application to use the specified function for activating types.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplicationBuilder UseTypeActivator(Func<Type, object> typeActivator) =>
|
||||||
|
UseTypeActivator(new DelegateTypeActivator(typeActivator));
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates an instance of <see cref="CliApplication"/> using configured parameters.
|
||||||
|
/// Default values are used in place of parameters that were not specified.
|
||||||
|
/// </summary>
|
||||||
|
public CliApplication Build()
|
||||||
|
{
|
||||||
|
_title ??= GetDefaultTitle() ?? "App";
|
||||||
|
_executableName ??= GetDefaultExecutableName() ?? "app";
|
||||||
|
_versionText ??= GetDefaultVersionText() ?? "v1.0";
|
||||||
|
_console ??= new SystemConsole();
|
||||||
|
_typeActivator ??= new DefaultTypeActivator();
|
||||||
|
|
||||||
|
var metadata = new ApplicationMetadata(_title, _executableName, _versionText, _description);
|
||||||
|
var configuration = new ApplicationConfiguration(_commandTypes.ToArray(), _isDebugModeAllowed, _isPreviewModeAllowed);
|
||||||
|
|
||||||
|
return new CliApplication(metadata, configuration, _console, _typeActivator);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class CliApplicationBuilder
|
||||||
|
{
|
||||||
|
private static readonly Lazy<Assembly?> LazyEntryAssembly = new Lazy<Assembly?>(Assembly.GetEntryAssembly);
|
||||||
|
|
||||||
|
// Entry assembly is null in tests
|
||||||
|
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
|
||||||
|
|
||||||
|
private static string? GetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||||
|
|
||||||
|
private static string? GetDefaultExecutableName()
|
||||||
|
{
|
||||||
|
var entryAssemblyLocation = EntryAssembly?.Location;
|
||||||
|
|
||||||
|
// The assembly can be an executable or a dll, depending on how it was packaged
|
||||||
|
var isDll = string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
return isDll
|
||||||
|
? "dotnet " + Path.GetFileName(entryAssemblyLocation)
|
||||||
|
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string? GetDefaultVersionText() =>
|
||||||
|
EntryAssembly != null
|
||||||
|
? $"v{EntryAssembly.GetName().Version}"
|
||||||
|
: null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,23 +1,60 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
<Import Project="../CliFx.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks>net45;netstandard2.0</TargetFrameworks>
|
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
|
||||||
<LangVersion>latest</LangVersion>
|
|
||||||
<Version>0.0.1</Version>
|
|
||||||
<Company>Tyrrrz</Company>
|
|
||||||
<Authors>$(Company)</Authors>
|
<Authors>$(Company)</Authors>
|
||||||
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
|
||||||
<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>
|
||||||
<PackageProjectUrl>https://github.com/Tyrrrz/CliFx</PackageProjectUrl>
|
<PackageProjectUrl>https://github.com/Tyrrrz/CliFx</PackageProjectUrl>
|
||||||
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
|
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
|
||||||
<PackageIconUrl>https://raw.githubusercontent.com/Tyrrrz/CliFx/master/favicon.png</PackageIconUrl>
|
<PackageIcon>favicon.png</PackageIcon>
|
||||||
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
|
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||||
<RepositoryUrl>https://github.com/Tyrrrz/CliFx</RepositoryUrl>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
<RepositoryType>git</RepositoryType>
|
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||||
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
|
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
<IncludeSymbols>true</IncludeSymbols>
|
||||||
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
|
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||||
|
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);CopyAnalyzerToPackage</TargetsForTfmSpecificContentInPackage>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- Disable nullability warnings on older frameworks because there is no nullability info for BCL -->
|
||||||
|
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
|
<Nullable>annotations</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||||
|
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
|
||||||
|
</AssemblyAttribute>
|
||||||
|
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||||
|
<_Parameter1>$(AssemblyName).Analyzers</_Parameter1>
|
||||||
|
</AssemblyAttribute>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" 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" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||||
|
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.3" />
|
||||||
|
</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>
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Models;
|
|
||||||
|
|
||||||
namespace CliFx
|
|
||||||
{
|
|
||||||
public abstract class Command
|
|
||||||
{
|
|
||||||
public virtual ExitCode Execute() => throw new InvalidOperationException(
|
|
||||||
"Can't execute command because its execution method is not defined. " +
|
|
||||||
$"Override Execute or ExecuteAsync on {GetType().Name} in order to make it executable.");
|
|
||||||
|
|
||||||
public virtual Task<ExitCode> ExecuteAsync() => Task.FromResult(Execute());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
25
CliFx/DefaultTypeActivator.cs
Normal file
25
CliFx/DefaultTypeActivator.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Type activator that uses the <see cref="Activator"/> class to instantiate objects.
|
||||||
|
/// </summary>
|
||||||
|
public class DefaultTypeActivator : ITypeActivator
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public object CreateInstance(Type type)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Activator.CreateInstance(type);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
throw CliFxException.DefaultActivatorFailed(type, ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
CliFx/DelegateTypeActivator.cs
Normal file
23
CliFx/DelegateTypeActivator.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Text;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Type activator that uses the specified delegate to instantiate objects.
|
||||||
|
/// </summary>
|
||||||
|
public class DelegateTypeActivator : ITypeActivator
|
||||||
|
{
|
||||||
|
private readonly Func<Type, object> _func;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="DelegateTypeActivator"/>.
|
||||||
|
/// </summary>
|
||||||
|
public DelegateTypeActivator(Func<Type, object> func) => _func = func;
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public object CreateInstance(Type type) =>
|
||||||
|
_func(type) ?? throw CliFxException.DelegateActivatorReceivedNull(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
252
CliFx/Domain/ApplicationSchema.cs
Normal file
252
CliFx/Domain/ApplicationSchema.cs
Normal file
@@ -0,0 +1,252 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal partial class ApplicationSchema
|
||||||
|
{
|
||||||
|
public IReadOnlyList<CommandSchema> Commands { get; }
|
||||||
|
|
||||||
|
public ApplicationSchema(IReadOnlyList<CommandSchema> commands)
|
||||||
|
{
|
||||||
|
Commands = commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandSchema? TryFindParentCommand(string? childCommandName)
|
||||||
|
{
|
||||||
|
// Default command has no parent
|
||||||
|
if (string.IsNullOrWhiteSpace(childCommandName))
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Try to find the parent command by repeatedly biting off chunks of its name
|
||||||
|
var route = childCommandName.Split(' ');
|
||||||
|
for (var i = route.Length - 1; i >= 1; i--)
|
||||||
|
{
|
||||||
|
var potentialParentCommandName = string.Join(" ", route.Take(i));
|
||||||
|
var matchingParentCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialParentCommandName));
|
||||||
|
|
||||||
|
if (matchingParentCommand != null)
|
||||||
|
return matchingParentCommand;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there's no parent - fall back to default command
|
||||||
|
return Commands.FirstOrDefault(c => c.IsDefault);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IReadOnlyList<CommandSchema> GetChildCommands(string? parentCommandName) =>
|
||||||
|
!string.IsNullOrWhiteSpace(parentCommandName) || Commands.Any(c => c.IsDefault)
|
||||||
|
? Commands.Where(c => TryFindParentCommand(c.Name)?.MatchesName(parentCommandName) == true).ToArray()
|
||||||
|
: Commands.Where(c => !string.IsNullOrWhiteSpace(c.Name) && TryFindParentCommand(c.Name) == null).ToArray();
|
||||||
|
|
||||||
|
// TODO: this out parameter is not a really nice design
|
||||||
|
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset)
|
||||||
|
{
|
||||||
|
// Try to find the command that contains the most of the input arguments in its name
|
||||||
|
for (var i = commandLineInput.UnboundArguments.Count; i >= 0; i--)
|
||||||
|
{
|
||||||
|
var potentialCommandName = string.Join(" ", commandLineInput.UnboundArguments.Take(i));
|
||||||
|
var matchingCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialCommandName));
|
||||||
|
|
||||||
|
if (matchingCommand != null)
|
||||||
|
{
|
||||||
|
argumentOffset = i;
|
||||||
|
return matchingCommand;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
argumentOffset = 0;
|
||||||
|
return Commands.FirstOrDefault(c => c.IsDefault);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput) =>
|
||||||
|
TryFindCommand(commandLineInput, out _);
|
||||||
|
|
||||||
|
public ICommand InitializeEntryPoint(
|
||||||
|
CommandLineInput commandLineInput,
|
||||||
|
IReadOnlyDictionary<string, string> environmentVariables,
|
||||||
|
ITypeActivator activator)
|
||||||
|
{
|
||||||
|
var command = TryFindCommand(commandLineInput, out var argumentOffset) ??
|
||||||
|
throw CliFxException.CannotFindMatchingCommand(commandLineInput);
|
||||||
|
|
||||||
|
var parameterInputs = argumentOffset == 0
|
||||||
|
? commandLineInput.UnboundArguments.ToArray()
|
||||||
|
: commandLineInput.UnboundArguments.Skip(argumentOffset).ToArray();
|
||||||
|
|
||||||
|
return command.CreateInstance(parameterInputs, commandLineInput.Options, environmentVariables, activator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICommand InitializeEntryPoint(
|
||||||
|
CommandLineInput commandLineInput,
|
||||||
|
IReadOnlyDictionary<string, string> environmentVariables) =>
|
||||||
|
InitializeEntryPoint(commandLineInput, environmentVariables, new DefaultTypeActivator());
|
||||||
|
|
||||||
|
public ICommand InitializeEntryPoint(CommandLineInput commandLineInput) =>
|
||||||
|
InitializeEntryPoint(commandLineInput, new Dictionary<string, string>());
|
||||||
|
|
||||||
|
public override string ToString() => string.Join(Environment.NewLine, Commands);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class ApplicationSchema
|
||||||
|
{
|
||||||
|
private static void ValidateParameters(CommandSchema command)
|
||||||
|
{
|
||||||
|
var duplicateOrderGroup = command.Parameters
|
||||||
|
.GroupBy(a => a.Order)
|
||||||
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
|
if (duplicateOrderGroup != null)
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandParametersDuplicateOrder(
|
||||||
|
command,
|
||||||
|
duplicateOrderGroup.Key,
|
||||||
|
duplicateOrderGroup.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
var duplicateNameGroup = command.Parameters
|
||||||
|
.Where(a => !string.IsNullOrWhiteSpace(a.Name))
|
||||||
|
.GroupBy(a => a.Name!, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
|
if (duplicateNameGroup != null)
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandParametersDuplicateName(
|
||||||
|
command,
|
||||||
|
duplicateNameGroup.Key,
|
||||||
|
duplicateNameGroup.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
var nonScalarParameters = command.Parameters
|
||||||
|
.Where(p => !p.IsScalar)
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (nonScalarParameters.Length > 1)
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandParametersTooManyNonScalar(
|
||||||
|
command,
|
||||||
|
nonScalarParameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
var nonLastNonScalarParameter = command.Parameters
|
||||||
|
.OrderByDescending(a => a.Order)
|
||||||
|
.Skip(1)
|
||||||
|
.LastOrDefault(p => !p.IsScalar);
|
||||||
|
|
||||||
|
if (nonLastNonScalarParameter != null)
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandParametersNonLastNonScalar(
|
||||||
|
command,
|
||||||
|
nonLastNonScalarParameter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
||||||
|
.GroupBy(o => o.Name!, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
|
if (duplicateNameGroup != null)
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandOptionsDuplicateName(
|
||||||
|
command,
|
||||||
|
duplicateNameGroup.Key,
|
||||||
|
duplicateNameGroup.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
var duplicateShortNameGroup = command.Options
|
||||||
|
.Where(o => o.ShortName != null)
|
||||||
|
.GroupBy(o => o.ShortName!.Value)
|
||||||
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
|
if (duplicateShortNameGroup != null)
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandOptionsDuplicateShortName(
|
||||||
|
command,
|
||||||
|
duplicateShortNameGroup.Key,
|
||||||
|
duplicateShortNameGroup.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
var duplicateEnvironmentVariableNameGroup = command.Options
|
||||||
|
.Where(o => !string.IsNullOrWhiteSpace(o.EnvironmentVariableName))
|
||||||
|
.GroupBy(o => o.EnvironmentVariableName!, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
|
if (duplicateEnvironmentVariableNameGroup != null)
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandOptionsDuplicateEnvironmentVariableName(
|
||||||
|
command,
|
||||||
|
duplicateEnvironmentVariableNameGroup.Key,
|
||||||
|
duplicateEnvironmentVariableNameGroup.ToArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
||||||
|
{
|
||||||
|
if (!commands.Any())
|
||||||
|
{
|
||||||
|
throw CliFxException.CommandsNotRegistered();
|
||||||
|
}
|
||||||
|
|
||||||
|
var duplicateNameGroup = commands
|
||||||
|
.GroupBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
|
||||||
|
.FirstOrDefault(g => g.Count() > 1);
|
||||||
|
|
||||||
|
if (duplicateNameGroup != null)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(duplicateNameGroup.Key))
|
||||||
|
throw CliFxException.CommandsDuplicateName(
|
||||||
|
duplicateNameGroup.Key,
|
||||||
|
duplicateNameGroup.ToArray());
|
||||||
|
|
||||||
|
throw CliFxException.CommandsTooManyDefaults(duplicateNameGroup.ToArray());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ApplicationSchema Resolve(IReadOnlyList<Type> commandTypes)
|
||||||
|
{
|
||||||
|
var commands = new List<CommandSchema>();
|
||||||
|
|
||||||
|
foreach (var commandType in commandTypes)
|
||||||
|
{
|
||||||
|
var command = CommandSchema.TryResolve(commandType) ??
|
||||||
|
throw CliFxException.InvalidCommandType(commandType);
|
||||||
|
|
||||||
|
ValidateParameters(command);
|
||||||
|
ValidateOptions(command);
|
||||||
|
|
||||||
|
commands.Add(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
ValidateCommands(commands);
|
||||||
|
|
||||||
|
return new ApplicationSchema(commands);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
179
CliFx/Domain/CommandArgumentSchema.cs
Normal file
179
CliFx/Domain/CommandArgumentSchema.cs
Normal file
@@ -0,0 +1,179 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
using CliFx.Internal;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal abstract partial class CommandArgumentSchema
|
||||||
|
{
|
||||||
|
public PropertyInfo Property { get; }
|
||||||
|
|
||||||
|
public string? Description { get; }
|
||||||
|
|
||||||
|
public abstract string DisplayName { get; }
|
||||||
|
|
||||||
|
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == null;
|
||||||
|
|
||||||
|
protected CommandArgumentSchema(PropertyInfo property, string? description)
|
||||||
|
{
|
||||||
|
Property = property;
|
||||||
|
Description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
||||||
|
Property.PropertyType != typeof(string)
|
||||||
|
? Property.PropertyType.GetEnumerableUnderlyingType()
|
||||||
|
: null;
|
||||||
|
|
||||||
|
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 enumerableUnderlyingType = TryGetEnumerableArgumentUnderlyingType();
|
||||||
|
|
||||||
|
// Scalar
|
||||||
|
if (enumerableUnderlyingType == null)
|
||||||
|
{
|
||||||
|
return values.Count <= 1
|
||||||
|
? ConvertScalar(values.SingleOrDefault(), targetType)
|
||||||
|
: throw CliFxException.CannotConvertMultipleValuesToNonScalar(this, values);
|
||||||
|
}
|
||||||
|
// Non-scalar
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ConvertNonScalar(values, targetType, enumerableUnderlyingType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Inject(ICommand command, IReadOnlyList<string> values) =>
|
||||||
|
Property.SetValue(command, Convert(values));
|
||||||
|
|
||||||
|
public void Inject(ICommand command, params 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
|
||||||
|
{
|
||||||
|
private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture;
|
||||||
|
|
||||||
|
private static readonly IReadOnlyDictionary<Type, Func<string?, object?>> PrimitiveConverters =
|
||||||
|
new Dictionary<Type, Func<string?, object?>>
|
||||||
|
{
|
||||||
|
[typeof(object)] = v => v,
|
||||||
|
[typeof(string)] = v => v,
|
||||||
|
[typeof(bool)] = v => string.IsNullOrWhiteSpace(v) || bool.Parse(v),
|
||||||
|
[typeof(char)] = v => v.Single(),
|
||||||
|
[typeof(sbyte)] = v => sbyte.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(byte)] = v => byte.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(short)] = v => short.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(ushort)] = v => ushort.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(int)] = v => int.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(uint)] = v => uint.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(long)] = v => long.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(ulong)] = v => ulong.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(float)] = v => float.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(double)] = v => double.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(decimal)] = v => decimal.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(DateTime)] = v => DateTime.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(DateTimeOffset)] = v => DateTimeOffset.Parse(v, ConversionFormatProvider),
|
||||||
|
[typeof(TimeSpan)] = v => TimeSpan.Parse(v, ConversionFormatProvider),
|
||||||
|
};
|
||||||
|
|
||||||
|
private static ConstructorInfo? GetStringConstructor(Type type) =>
|
||||||
|
type.GetConstructor(new[] {typeof(string)});
|
||||||
|
|
||||||
|
private static MethodInfo? GetStaticParseMethod(Type type) =>
|
||||||
|
type.GetMethod("Parse",
|
||||||
|
BindingFlags.Public | BindingFlags.Static,
|
||||||
|
null, new[] {typeof(string)}, null);
|
||||||
|
|
||||||
|
private static MethodInfo? GetStaticParseMethodWithFormatProvider(Type type) =>
|
||||||
|
type.GetMethod("Parse",
|
||||||
|
BindingFlags.Public | BindingFlags.Static,
|
||||||
|
null, new[] {typeof(string), typeof(IFormatProvider)}, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
CliFx/Domain/CommandDirectiveInput.cs
Normal file
20
CliFx/Domain/CommandDirectiveInput.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal class CommandDirectiveInput
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public bool IsDebugDirective => string.Equals(Name, "debug", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public bool IsPreviewDirective => string.Equals(Name, "preview", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public CommandDirectiveInput(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => $"[{Name}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
163
CliFx/Domain/CommandLineInput.cs
Normal file
163
CliFx/Domain/CommandLineInput.cs
Normal file
@@ -0,0 +1,163 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using CliFx.Internal;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal partial class CommandLineInput
|
||||||
|
{
|
||||||
|
public IReadOnlyList<CommandDirectiveInput> Directives { get; }
|
||||||
|
|
||||||
|
public IReadOnlyList<CommandUnboundArgumentInput> UnboundArguments { get; }
|
||||||
|
|
||||||
|
public IReadOnlyList<CommandOptionInput> Options { get; }
|
||||||
|
|
||||||
|
public bool IsDebugDirectiveSpecified => Directives.Any(d => d.IsDebugDirective);
|
||||||
|
|
||||||
|
public bool IsPreviewDirectiveSpecified => Directives.Any(d => d.IsPreviewDirective);
|
||||||
|
|
||||||
|
public bool IsHelpOptionSpecified => Options.Any(o => o.IsHelpOption);
|
||||||
|
|
||||||
|
public bool IsVersionOptionSpecified => Options.Any(o => o.IsVersionOption);
|
||||||
|
|
||||||
|
public CommandLineInput(
|
||||||
|
IReadOnlyList<CommandDirectiveInput> directives,
|
||||||
|
IReadOnlyList<CommandUnboundArgumentInput> unboundArguments,
|
||||||
|
IReadOnlyList<CommandOptionInput> options)
|
||||||
|
{
|
||||||
|
Directives = directives;
|
||||||
|
UnboundArguments = unboundArguments;
|
||||||
|
Options = options;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
var buffer = new StringBuilder();
|
||||||
|
|
||||||
|
foreach (var directive in Directives)
|
||||||
|
{
|
||||||
|
buffer.AppendIfNotEmpty(' ');
|
||||||
|
buffer.Append(directive);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var argument in UnboundArguments)
|
||||||
|
{
|
||||||
|
buffer.AppendIfNotEmpty(' ');
|
||||||
|
buffer.Append(argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var option in Options)
|
||||||
|
{
|
||||||
|
buffer.AppendIfNotEmpty(' ');
|
||||||
|
buffer.Append(option);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class CommandLineInput
|
||||||
|
{
|
||||||
|
public static CommandLineInput Parse(IReadOnlyList<string> commandLineArguments)
|
||||||
|
{
|
||||||
|
var builder = new CommandLineInputBuilder();
|
||||||
|
|
||||||
|
var currentOptionAlias = "";
|
||||||
|
var currentOptionValues = new List<string>();
|
||||||
|
|
||||||
|
bool TryParseDirective(string argument)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!argument.StartsWith("[", StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
!argument.EndsWith("]", StringComparison.OrdinalIgnoreCase))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var directive = argument.Substring(1, argument.Length - 2);
|
||||||
|
builder.AddDirective(directive);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryParseArgument(string argument)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
builder.AddUnboundArgument(argument);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryParseOptionName(string argument)
|
||||||
|
{
|
||||||
|
if (!argument.StartsWith("--", StringComparison.OrdinalIgnoreCase))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||||
|
|
||||||
|
currentOptionAlias = argument.Substring(2);
|
||||||
|
currentOptionValues = new List<string>();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryParseOptionShortName(string argument)
|
||||||
|
{
|
||||||
|
if (!argument.StartsWith("-", StringComparison.OrdinalIgnoreCase))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
foreach (var c in argument.Substring(1))
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||||
|
|
||||||
|
currentOptionAlias = c.AsString();
|
||||||
|
currentOptionValues = new List<string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TryParseOptionValue(string argument)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
currentOptionValues.Add(argument);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var argument in commandLineArguments)
|
||||||
|
{
|
||||||
|
var _ =
|
||||||
|
TryParseOptionName(argument) ||
|
||||||
|
TryParseOptionShortName(argument) ||
|
||||||
|
TryParseDirective(argument) ||
|
||||||
|
TryParseArgument(argument) ||
|
||||||
|
TryParseOptionValue(argument);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||||
|
|
||||||
|
return builder.Build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class CommandLineInput
|
||||||
|
{
|
||||||
|
private static IReadOnlyList<CommandDirectiveInput> EmptyDirectives { get; } = new CommandDirectiveInput[0];
|
||||||
|
|
||||||
|
private static IReadOnlyList<CommandUnboundArgumentInput> EmptyUnboundArguments { get; } = new CommandUnboundArgumentInput[0];
|
||||||
|
|
||||||
|
private static IReadOnlyList<CommandOptionInput> EmptyOptions { get; } = new CommandOptionInput[0];
|
||||||
|
|
||||||
|
public static CommandLineInput Empty { get; } = new CommandLineInput(EmptyDirectives, EmptyUnboundArguments, EmptyOptions);
|
||||||
|
}
|
||||||
|
}
|
||||||
43
CliFx/Domain/CommandLineInputBuilder.cs
Normal file
43
CliFx/Domain/CommandLineInputBuilder.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal class CommandLineInputBuilder
|
||||||
|
{
|
||||||
|
private readonly List<CommandDirectiveInput> _directives = new List<CommandDirectiveInput>();
|
||||||
|
private readonly List<CommandUnboundArgumentInput> _unboundArguments = new List<CommandUnboundArgumentInput>();
|
||||||
|
private readonly List<CommandOptionInput> _options = new List<CommandOptionInput>();
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddDirective(CommandDirectiveInput directive)
|
||||||
|
{
|
||||||
|
_directives.Add(directive);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddDirective(string directive) =>
|
||||||
|
AddDirective(new CommandDirectiveInput(directive));
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddUnboundArgument(CommandUnboundArgumentInput unboundArgument)
|
||||||
|
{
|
||||||
|
_unboundArguments.Add(unboundArgument);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddUnboundArgument(string unboundArgument) =>
|
||||||
|
AddUnboundArgument(new CommandUnboundArgumentInput(unboundArgument));
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddOption(CommandOptionInput option)
|
||||||
|
{
|
||||||
|
_options.Add(option);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddOption(string optionAlias, IReadOnlyList<string> values) =>
|
||||||
|
AddOption(new CommandOptionInput(optionAlias, values));
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddOption(string optionAlias, params string[] values) =>
|
||||||
|
AddOption(optionAlias, (IReadOnlyList<string>) values);
|
||||||
|
|
||||||
|
public CommandLineInput Build() => new CommandLineInput(_directives, _unboundArguments, _options);
|
||||||
|
}
|
||||||
|
}
|
||||||
52
CliFx/Domain/CommandOptionInput.cs
Normal file
52
CliFx/Domain/CommandOptionInput.cs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
using CliFx.Internal;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal class CommandOptionInput
|
||||||
|
{
|
||||||
|
public string Alias { get; }
|
||||||
|
|
||||||
|
public string DisplayAlias =>
|
||||||
|
Alias.Length > 1
|
||||||
|
? $"--{Alias}"
|
||||||
|
: $"-{Alias}";
|
||||||
|
|
||||||
|
public IReadOnlyList<string> Values { get; }
|
||||||
|
|
||||||
|
public bool IsHelpOption => CommandOptionSchema.HelpOption.MatchesNameOrShortName(Alias);
|
||||||
|
|
||||||
|
public bool IsVersionOption => CommandOptionSchema.VersionOption.MatchesNameOrShortName(Alias);
|
||||||
|
|
||||||
|
public CommandOptionInput(string alias, IReadOnlyList<string> values)
|
||||||
|
{
|
||||||
|
Alias = alias;
|
||||||
|
Values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
var buffer = new StringBuilder();
|
||||||
|
|
||||||
|
buffer.Append(DisplayAlias);
|
||||||
|
|
||||||
|
foreach (var value in Values)
|
||||||
|
{
|
||||||
|
buffer.AppendIfNotEmpty(' ');
|
||||||
|
|
||||||
|
var isEscaped = value.Contains(" ");
|
||||||
|
|
||||||
|
if (isEscaped)
|
||||||
|
buffer.Append('"');
|
||||||
|
|
||||||
|
buffer.Append(value);
|
||||||
|
|
||||||
|
if (isEscaped)
|
||||||
|
buffer.Append('"');
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
104
CliFx/Domain/CommandOptionSchema.cs
Normal file
104
CliFx/Domain/CommandOptionSchema.cs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal partial class CommandOptionSchema : CommandArgumentSchema
|
||||||
|
{
|
||||||
|
public string? Name { get; }
|
||||||
|
|
||||||
|
public char? ShortName { get; }
|
||||||
|
|
||||||
|
public override string DisplayName => !string.IsNullOrWhiteSpace(Name)
|
||||||
|
? $"--{Name}"
|
||||||
|
: $"-{ShortName}";
|
||||||
|
|
||||||
|
public string? EnvironmentVariableName { get; }
|
||||||
|
|
||||||
|
public bool IsRequired { get; }
|
||||||
|
|
||||||
|
public CommandOptionSchema(
|
||||||
|
PropertyInfo property,
|
||||||
|
string? name,
|
||||||
|
char? shortName,
|
||||||
|
string? environmentVariableName,
|
||||||
|
bool isRequired,
|
||||||
|
string? description)
|
||||||
|
: base(property, description)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
ShortName = shortName;
|
||||||
|
EnvironmentVariableName = environmentVariableName;
|
||||||
|
IsRequired = isRequired;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool MatchesName(string? name) =>
|
||||||
|
!string.IsNullOrWhiteSpace(Name) &&
|
||||||
|
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public bool MatchesShortName(char? shortName) =>
|
||||||
|
ShortName != null &&
|
||||||
|
ShortName == shortName;
|
||||||
|
|
||||||
|
public bool MatchesNameOrShortName(string alias) =>
|
||||||
|
MatchesName(alias) ||
|
||||||
|
alias.Length == 1 && MatchesShortName(alias.Single());
|
||||||
|
|
||||||
|
public bool MatchesEnvironmentVariableName(string environmentVariableName) =>
|
||||||
|
!string.IsNullOrWhiteSpace(EnvironmentVariableName) &&
|
||||||
|
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
var buffer = new StringBuilder();
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(Name))
|
||||||
|
{
|
||||||
|
buffer.Append("--");
|
||||||
|
buffer.Append(Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(Name) && ShortName != null)
|
||||||
|
buffer.Append('|');
|
||||||
|
|
||||||
|
if (ShortName != null)
|
||||||
|
{
|
||||||
|
buffer.Append('-');
|
||||||
|
buffer.Append(ShortName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class CommandOptionSchema
|
||||||
|
{
|
||||||
|
public static CommandOptionSchema? TryResolve(PropertyInfo property)
|
||||||
|
{
|
||||||
|
var attribute = property.GetCustomAttribute<CommandOptionAttribute>();
|
||||||
|
if (attribute == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new CommandOptionSchema(
|
||||||
|
property,
|
||||||
|
attribute.Name,
|
||||||
|
attribute.ShortName,
|
||||||
|
attribute.EnvironmentVariableName,
|
||||||
|
attribute.IsRequired,
|
||||||
|
attribute.Description
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal partial class CommandOptionSchema
|
||||||
|
{
|
||||||
|
public static CommandOptionSchema HelpOption { get; } =
|
||||||
|
new CommandOptionSchema(null!, "help", 'h', null, false, "Shows help text.");
|
||||||
|
|
||||||
|
public static CommandOptionSchema VersionOption { get; } =
|
||||||
|
new CommandOptionSchema(null!, "version", null, null, false, "Shows version information.");
|
||||||
|
}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user