mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11e3e0f85d | ||
|
|
42f4d7d5a7 | ||
|
|
bed22b6500 | ||
|
|
17449e0794 | ||
|
|
4732166f5f | ||
|
|
f5e37b96fc | ||
|
|
4cef596fe8 | ||
|
|
19b87717c1 | ||
|
|
7e4c6b20ff | ||
|
|
fb2071ed2b | ||
|
|
7d2f934310 | ||
|
|
95a00b0952 | ||
|
|
cb3fee65f3 | ||
|
|
65628b145a | ||
|
|
802bbfccc6 | ||
|
|
6e7742a4f3 | ||
|
|
f6a1a40471 | ||
|
|
33ca4da260 | ||
|
|
cbb72b16ae | ||
|
|
c58629e999 | ||
|
|
387fb72718 | ||
|
|
e04f0da318 | ||
|
|
d25873ee10 | ||
|
|
a28223fc8b | ||
|
|
1dab27de55 | ||
|
|
698629b153 | ||
|
|
65b66b0d27 | ||
|
|
7d3ba612c4 | ||
|
|
8c3b8d1f49 | ||
|
|
fdd39855ad |
2
.github/workflows/CD.yml
vendored
2
.github/workflows/CD.yml
vendored
@@ -22,4 +22,4 @@ jobs:
|
||||
run: dotnet pack CliFx --configuration Release
|
||||
|
||||
- name: Deploy
|
||||
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{secrets.NUGET_TOKEN}}
|
||||
run: dotnet nuget push CliFx/bin/Release/*.nupkg -s nuget.org -k ${{ secrets.NUGET_TOKEN }}
|
||||
|
||||
8
.github/workflows/CI.yml
vendored
8
.github/workflows/CI.yml
vendored
@@ -20,10 +20,16 @@ jobs:
|
||||
dotnet-version: 3.1.100
|
||||
|
||||
- name: Build & test
|
||||
run: dotnet test --configuration Release
|
||||
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
|
||||
|
||||
19
Changelog.md
19
Changelog.md
@@ -1,3 +1,22 @@
|
||||
### v1.3 (23-May-2020)
|
||||
|
||||
- Changed analyzers to report errors instead of warnings. If you find that some analyzer works incorrectly, please report it on GitHub. You can also configure inspection severity overrides in your project if you need to.
|
||||
- Improved help text by showing default values for non-required options. This only works on types that have a custom override for `ToString()` method. Additionally, if the type implements `IFormattable`, the overload with a format provider will be used instead. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||
- Changed default version text to only show 3 version components instead of 4, if the last component (revision) is not specified or is zero. This makes the default version text compliant with semantic versioning.
|
||||
- Fixed an issue where it was possible to define a command with an option that has the same name or short name as built-in help or version options. Previously it would lead to the user-defined option being ignored in favor of the built-in option. Now this will throw an exception instead.
|
||||
- Changed the underlying representation of `StreamReader`/`StreamWriter` objects used in `SystemConsole` and `VirtualConsole` to be thread-safe.
|
||||
|
||||
### 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.
|
||||
|
||||
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.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, 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.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0100 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0100),
|
||||
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
|
||||
"Use the provided IConsole abstraction instead of System.Console to ensure that the command can be tested in isolation",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
}
|
||||
}
|
||||
11
CliFx.Analyzers/Internal/RoslynExtensions.cs
Normal file
11
CliFx.Analyzers/Internal/RoslynExtensions.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Analyzers.Internal
|
||||
{
|
||||
internal static class RoslynExtensions
|
||||
{
|
||||
public static bool DisplayNameMatches(this ISymbol symbol, string name) =>
|
||||
string.Equals(symbol.ToDisplayString(), name, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
37
CliFx.Analyzers/KnownSymbols.cs
Normal file
37
CliFx.Analyzers/KnownSymbols.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using CliFx.Analyzers.Internal;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Analyzers
|
||||
{
|
||||
public static class KnownSymbols
|
||||
{
|
||||
public static bool IsSystemString(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("string") ||
|
||||
symbol.DisplayNameMatches("System.String");
|
||||
|
||||
public static bool IsSystemChar(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("char") ||
|
||||
symbol.DisplayNameMatches("System.Char");
|
||||
|
||||
public static bool IsSystemCollectionsGenericIEnumerable(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("System.Collections.Generic.IEnumerable<T>");
|
||||
|
||||
public static bool IsSystemConsole(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("System.Console");
|
||||
|
||||
public static bool IsConsoleInterface(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.IConsole");
|
||||
|
||||
public static bool IsCommandInterface(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.ICommand");
|
||||
|
||||
public static bool IsCommandAttribute(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.Attributes.CommandAttribute");
|
||||
|
||||
public static bool IsCommandParameterAttribute(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.Attributes.CommandParameterAttribute");
|
||||
|
||||
public static bool IsCommandOptionAttribute(ISymbol symbol) =>
|
||||
symbol.DisplayNameMatches("CliFx.Attributes.CommandOptionAttribute");
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,13 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.2" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
<ProjectReference Include="..\CliFx.Analyzers\CliFx.Analyzers.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -6,6 +6,18 @@ namespace CliFx.Tests
|
||||
{
|
||||
public partial class ApplicationSpecs
|
||||
{
|
||||
[Command]
|
||||
private class DefaultCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class AnotherDefaultCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class NonImplementedCommand
|
||||
{
|
||||
@@ -76,6 +88,24 @@ namespace CliFx.Tests
|
||||
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
|
||||
{
|
||||
@@ -100,6 +130,24 @@ namespace CliFx.Tests
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class ConflictWithHelpOptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-h", 'h')]
|
||||
public string? OptionH { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class ConflictWithVersionOptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("version")]
|
||||
public string? Version { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
|
||||
{
|
||||
@@ -112,12 +160,6 @@ namespace CliFx.Tests
|
||||
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
|
||||
{
|
||||
|
||||
@@ -4,11 +4,16 @@ 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()
|
||||
{
|
||||
@@ -26,10 +31,10 @@ namespace CliFx.Tests
|
||||
{
|
||||
// Act
|
||||
var app = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ValidCommand))
|
||||
.AddCommandsFrom(typeof(ValidCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(ValidCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(ValidCommand).Assembly})
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommandsFrom(typeof(DefaultCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(DefaultCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(DefaultCommand).Assembly})
|
||||
.AddCommandsFromThisAssembly()
|
||||
.AllowDebugMode()
|
||||
.AllowPreviewMode()
|
||||
@@ -38,7 +43,7 @@ namespace CliFx.Tests
|
||||
.UseVersionText("test")
|
||||
.UseDescription("test")
|
||||
.UseConsole(new VirtualConsole(Stream.Null))
|
||||
.UseTypeActivator(Activator.CreateInstance)
|
||||
.UseTypeActivator(Activator.CreateInstance!)
|
||||
.Build();
|
||||
|
||||
// Assert
|
||||
@@ -52,7 +57,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = Array.Empty<Type>();
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -62,7 +68,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -72,7 +79,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -82,7 +90,19 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_can_be_default_but_only_if_it_is_the_only_such_command()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DefaultCommand), typeof(AnotherDefaultCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -92,7 +112,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -102,7 +123,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -112,7 +134,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -122,7 +145,30 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.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>(() => RootSchema.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>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -132,7 +178,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -142,7 +189,30 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_not_have_conflicts_with_the_implicit_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(ConflictWithHelpOptionCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_not_have_conflicts_with_the_implicit_version_option()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(ConflictWithVersionOptionCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -152,7 +222,8 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
var ex = Assert.Throws<CliFxException>(() => RootSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -162,10 +233,10 @@ namespace CliFx.Tests
|
||||
var commandTypes = new[] {typeof(HiddenPropertiesCommand)};
|
||||
|
||||
// Act
|
||||
var schema = ApplicationSchema.Resolve(commandTypes);
|
||||
var schema = RootSchema.Resolve(commandTypes);
|
||||
|
||||
// Assert
|
||||
schema.Should().BeEquivalentTo(new ApplicationSchema(new[]
|
||||
schema.Should().BeEquivalentTo(new RootSchema(new[]
|
||||
{
|
||||
new CommandSchema(
|
||||
typeof(HiddenPropertiesCommand),
|
||||
@@ -174,7 +245,7 @@ namespace CliFx.Tests
|
||||
new[]
|
||||
{
|
||||
new CommandParameterSchema(
|
||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter)),
|
||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter))!,
|
||||
13,
|
||||
"param",
|
||||
"Param description")
|
||||
@@ -182,12 +253,13 @@ namespace CliFx.Tests
|
||||
new[]
|
||||
{
|
||||
new CommandOptionSchema(
|
||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option)),
|
||||
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option))!,
|
||||
"option",
|
||||
'o',
|
||||
"ENV",
|
||||
false,
|
||||
"Option description")
|
||||
"Option description"),
|
||||
CommandOptionSchema.HelpOption
|
||||
})
|
||||
}));
|
||||
|
||||
|
||||
@@ -17,20 +17,14 @@ namespace CliFx.Tests
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public StringConstructable(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
public StringConstructable(string value) => Value = value;
|
||||
}
|
||||
|
||||
private class StringParseable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private StringParseable(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
private StringParseable(string value) => Value = value;
|
||||
|
||||
public static StringParseable Parse(string value) => new StringParseable(value);
|
||||
}
|
||||
@@ -39,10 +33,7 @@ namespace CliFx.Tests
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private StringParseableWithFormatProvider(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
private StringParseableWithFormatProvider(string value) => Value = value;
|
||||
|
||||
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||
new StringParseableWithFormatProvider(value + " " + formatProvider);
|
||||
@@ -54,9 +45,7 @@ namespace CliFx.Tests
|
||||
|
||||
public class CustomEnumerable<T> : IEnumerable<T>
|
||||
{
|
||||
private readonly T[] _arr = new T[0];
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _arr).GetEnumerator();
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Tests.Internal;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
@@ -11,13 +13,14 @@ namespace CliFx.Tests
|
||||
public void Input_is_empty_if_no_arguments_are_provided()
|
||||
{
|
||||
// Arrange
|
||||
var args = Array.Empty<string>();
|
||||
var arguments = Array.Empty<string>();
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandLineInput.Parse(args);
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(CommandLineInput.Empty);
|
||||
input.Should().BeEquivalentTo(CommandInput.Empty);
|
||||
}
|
||||
|
||||
public static object[][] DirectivesTestData => new[]
|
||||
@@ -25,7 +28,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.Build()
|
||||
},
|
||||
@@ -33,7 +36,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]", "[debug]"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.AddDirective("debug")
|
||||
.Build()
|
||||
@@ -42,10 +45,13 @@ namespace CliFx.Tests
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(DirectivesTestData))]
|
||||
internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(string[] arguments, CommandLineInput expectedInput)
|
||||
internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandLineInput.Parse(arguments);
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
@@ -56,7 +62,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option")
|
||||
.Build()
|
||||
},
|
||||
@@ -64,7 +70,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option", "value"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option", "value")
|
||||
.Build()
|
||||
},
|
||||
@@ -72,7 +78,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option", "value1", "value2"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option", "value1", "value2")
|
||||
.Build()
|
||||
},
|
||||
@@ -80,7 +86,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option", "same value"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option", "same value")
|
||||
.Build()
|
||||
},
|
||||
@@ -88,7 +94,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "--option2"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1")
|
||||
.AddOption("option2")
|
||||
.Build()
|
||||
@@ -97,7 +103,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "value1", "--option2", "value2"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1", "value1")
|
||||
.AddOption("option2", "value2")
|
||||
.Build()
|
||||
@@ -106,7 +112,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "value1", "value2", "--option2", "value3", "value4"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1", "value1", "value2")
|
||||
.AddOption("option2", "value3", "value4")
|
||||
.Build()
|
||||
@@ -115,7 +121,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"--option1", "value1", "value2", "--option2"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("option1", "value1", "value2")
|
||||
.AddOption("option2")
|
||||
.Build()
|
||||
@@ -124,10 +130,13 @@ namespace CliFx.Tests
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(OptionsTestData))]
|
||||
internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(string[] arguments, CommandLineInput expectedInput)
|
||||
internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandLineInput.Parse(arguments);
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
@@ -138,7 +147,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o")
|
||||
.Build()
|
||||
},
|
||||
@@ -146,7 +155,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o", "value"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o", "value")
|
||||
.Build()
|
||||
},
|
||||
@@ -154,7 +163,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o", "value1", "value2"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o", "value1", "value2")
|
||||
.Build()
|
||||
},
|
||||
@@ -162,7 +171,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-o", "same value"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("o", "same value")
|
||||
.Build()
|
||||
},
|
||||
@@ -170,7 +179,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "-b"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.Build()
|
||||
@@ -179,7 +188,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "value1", "-b", "value2"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a", "value1")
|
||||
.AddOption("b", "value2")
|
||||
.Build()
|
||||
@@ -188,7 +197,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "value1", "value2", "-b", "value3", "value4"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a", "value1", "value2")
|
||||
.AddOption("b", "value3", "value4")
|
||||
.Build()
|
||||
@@ -197,7 +206,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-a", "value1", "value2", "-b"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a", "value1", "value2")
|
||||
.AddOption("b")
|
||||
.Build()
|
||||
@@ -206,7 +215,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-abc"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c")
|
||||
@@ -216,7 +225,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-abc", "value"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c", "value")
|
||||
@@ -226,7 +235,7 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"-abc", "value1", "value2"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
.AddOption("c", "value1", "value2")
|
||||
@@ -236,48 +245,51 @@ namespace CliFx.Tests
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(ShortOptionsTestData))]
|
||||
internal void Option_can_be_set_by_specifying_its_short_name_after_a_single_dash(string[] arguments, CommandLineInput expectedInput)
|
||||
internal void Option_can_be_set_by_specifying_its_short_name_after_a_single_dash(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandLineInput.Parse(arguments);
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
}
|
||||
|
||||
public static object[][] UnboundArgumentsTestData => new[]
|
||||
public static object[][] ParametersTestData => new[]
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new[] {"foo"},
|
||||
new CommandLineInputBuilder()
|
||||
.AddUnboundArgument("foo")
|
||||
new CommandInputBuilder()
|
||||
.AddParameter("foo")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"foo", "bar"},
|
||||
new CommandLineInputBuilder()
|
||||
.AddUnboundArgument("foo")
|
||||
.AddUnboundArgument("bar")
|
||||
new CommandInputBuilder()
|
||||
.AddParameter("foo")
|
||||
.AddParameter("bar")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]", "foo"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.AddUnboundArgument("foo")
|
||||
.AddParameter("foo")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"foo", "--option", "value", "-abc"},
|
||||
new CommandLineInputBuilder()
|
||||
.AddUnboundArgument("foo")
|
||||
new CommandInputBuilder()
|
||||
.AddParameter("foo")
|
||||
.AddOption("option", "value")
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
@@ -288,11 +300,11 @@ namespace CliFx.Tests
|
||||
new object[]
|
||||
{
|
||||
new[] {"[preview]", "[debug]", "foo", "bar", "--option", "value", "-abc"},
|
||||
new CommandLineInputBuilder()
|
||||
new CommandInputBuilder()
|
||||
.AddDirective("preview")
|
||||
.AddDirective("debug")
|
||||
.AddUnboundArgument("foo")
|
||||
.AddUnboundArgument("bar")
|
||||
.AddParameter("foo")
|
||||
.AddParameter("bar")
|
||||
.AddOption("option", "value")
|
||||
.AddOption("a")
|
||||
.AddOption("b")
|
||||
@@ -302,11 +314,62 @@ namespace CliFx.Tests
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(UnboundArgumentsTestData))]
|
||||
internal void Any_remaining_arguments_are_treated_as_unbound_arguments(string[] arguments, CommandLineInput expectedInput)
|
||||
[MemberData(nameof(ParametersTestData))]
|
||||
internal void Parameter_can_be_set_by_specifying_the_value_directly(IReadOnlyList<string> arguments, CommandInput expectedInput)
|
||||
{
|
||||
// Arrange
|
||||
var commandNames = Array.Empty<string>();
|
||||
|
||||
// Act
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
}
|
||||
|
||||
public static object[][] CommandNameTestData => new[]
|
||||
{
|
||||
new object[]
|
||||
{
|
||||
new[] {"cmd"},
|
||||
new[] {"cmd"},
|
||||
new CommandInputBuilder()
|
||||
.SetCommandName("cmd")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"cmd"},
|
||||
new[] {"cmd", "foo", "bar", "-o", "value"},
|
||||
new CommandInputBuilder()
|
||||
.SetCommandName("cmd")
|
||||
.AddParameter("foo")
|
||||
.AddParameter("bar")
|
||||
.AddOption("o", "value")
|
||||
.Build()
|
||||
},
|
||||
|
||||
new object[]
|
||||
{
|
||||
new[] {"cmd", "cmd sub"},
|
||||
new[] {"cmd", "sub", "foo"},
|
||||
new CommandInputBuilder()
|
||||
.SetCommandName("cmd sub")
|
||||
.AddParameter("foo")
|
||||
.Build()
|
||||
}
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(CommandNameTestData))]
|
||||
internal void Command_name_is_matched_from_arguments_that_come_before_parameters(
|
||||
IReadOnlyList<string> commandNames,
|
||||
IReadOnlyList<string> arguments,
|
||||
CommandInput expectedInput)
|
||||
{
|
||||
// Act
|
||||
var input = CommandLineInput.Parse(arguments);
|
||||
var input = CommandInput.Parse(arguments, commandNames);
|
||||
|
||||
// Assert
|
||||
input.Should().BeEquivalentTo(expectedInput);
|
||||
|
||||
@@ -13,6 +13,8 @@ namespace CliFx.Tests
|
||||
[Fact]
|
||||
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
||||
{
|
||||
// Can't test it with a real console because CliWrap can't send Ctrl+C
|
||||
|
||||
// Arrange
|
||||
using var cts = new CancellationTokenSource();
|
||||
|
||||
|
||||
@@ -17,10 +17,11 @@
|
||||
<ItemGroup>
|
||||
<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="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
||||
<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>
|
||||
|
||||
@@ -51,6 +51,8 @@ namespace CliFx.Tests
|
||||
console.ResetColor();
|
||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||
console.CursorLeft = 42;
|
||||
console.CursorTop = 24;
|
||||
|
||||
// Assert
|
||||
stdInData.Should().Be("input");
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
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()
|
||||
{
|
||||
@@ -26,8 +31,8 @@ namespace CliFx.Tests
|
||||
var activator = new DefaultTypeActivator();
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() =>
|
||||
activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -48,11 +53,11 @@ namespace CliFx.Tests
|
||||
public void Delegate_type_activator_throws_if_the_underlying_function_returns_null()
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DelegateTypeActivator(_ => null);
|
||||
var activator = new DelegateTypeActivator(_ => null!);
|
||||
|
||||
// Act & assert
|
||||
Assert.Throws<CliFxException>(() =>
|
||||
activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Internal;
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
@@ -9,7 +15,31 @@ namespace CliFx.Tests
|
||||
public partial class DirectivesSpecs
|
||||
{
|
||||
[Fact]
|
||||
public async Task Preview_directive_can_be_enabled_to_print_provided_arguments_as_they_were_parsed()
|
||||
public async Task Debug_directive_can_be_specified_to_have_the_application_wait_until_debugger_is_attached()
|
||||
{
|
||||
// We can't actually attach a debugger in tests, so instead just cancel execution after some time
|
||||
|
||||
// Arrange
|
||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
|
||||
var stdOut = new StringBuilder();
|
||||
|
||||
var command = Cli.Wrap("dotnet")
|
||||
.WithArguments(a => a
|
||||
.Add(Dummy.Program.Location)
|
||||
.Add("[debug]"))
|
||||
.WithEnvironmentVariables(e => e
|
||||
.Set("ENV_TARGET", "Mars")) | stdOut;
|
||||
|
||||
// Act
|
||||
await command.ExecuteAsync(cts.Token).Task.IgnoreCancellation();
|
||||
var stdOutData = stdOut.ToString();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().Contain("Attach debugger to");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Preview_directive_can_be_specified_to_print_provided_arguments_as_they_were_parsed()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
@@ -30,7 +60,7 @@ namespace CliFx.Tests
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().ContainAll("cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option foo]");
|
||||
stdOutData.Should().ContainAll("cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Tests.Internal;
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
using FluentAssertions;
|
||||
@@ -53,19 +54,17 @@ namespace CliFx.Tests
|
||||
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 input = CommandInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||
};
|
||||
|
||||
// Act
|
||||
var command = schema.InitializeEntryPoint(input, envVars);
|
||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCollectionCommand>(input, envVars);
|
||||
|
||||
// Assert
|
||||
command.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
|
||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
|
||||
{
|
||||
Option = new[] {"foo", "bar"}
|
||||
});
|
||||
@@ -75,19 +74,17 @@ namespace CliFx.Tests
|
||||
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 input = CommandInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||
};
|
||||
|
||||
// Act
|
||||
var command = schema.InitializeEntryPoint(input, envVars);
|
||||
var instance = CommandHelper.ResolveCommand<EnvironmentVariableCommand>(input, envVars);
|
||||
|
||||
// Assert
|
||||
command.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||
instance.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||
{
|
||||
Option = $"foo{Path.PathSeparator}bar"
|
||||
});
|
||||
|
||||
@@ -25,7 +25,10 @@ namespace CliFx.Tests
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
|
||||
[CommandOption("show-help")]
|
||||
public bool ShowHelp { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode, ShowHelp);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,18 @@ 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_full_error_details()
|
||||
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();
|
||||
@@ -29,8 +34,12 @@ namespace CliFx.Tests
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().Contain("Kaput");
|
||||
stdErrData.Length.Should().BeGreaterThan("Kaput".Length);
|
||||
stdErrData.Should().ContainAll(
|
||||
"System.Exception:",
|
||||
"Kaput", "at",
|
||||
"CliFx.Tests");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -55,6 +64,8 @@ namespace CliFx.Tests
|
||||
// Assert
|
||||
exitCode.Should().Be(69);
|
||||
stdErrData.Should().Be("Kaput");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -71,14 +82,91 @@ namespace CliFx.Tests
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"exc", "-m", "Kaput"},
|
||||
new[] {"exc"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().NotBeEmpty();
|
||||
stdErrData.Should().ContainAll(
|
||||
"CliFx.Exceptions.CommandException:",
|
||||
"at",
|
||||
"CliFx.Tests");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_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(CommandExceptionCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"exc", "-m", "Kaput", "--show-help"},
|
||||
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().Be("Kaput");
|
||||
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_shows_help_text_on_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(CommandExceptionCommand))
|
||||
.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().NotBeNullOrWhiteSpace();
|
||||
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
@@ -69,14 +70,31 @@ namespace CliFx.Tests
|
||||
[Command("cmd-with-req-opts")]
|
||||
private class RequiredOptionsCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-f", 'f', IsRequired = true)]
|
||||
public string? OptionF { get; set; }
|
||||
[CommandOption("option-a", 'a', IsRequired = true)]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-g", 'g', IsRequired = true)]
|
||||
public IEnumerable<int>? OptionG { get; set; }
|
||||
[CommandOption("option-b", 'b', IsRequired = true)]
|
||||
public IEnumerable<int>? OptionB { get; set; }
|
||||
|
||||
[CommandOption("option-h", 'h')]
|
||||
public string? OptionH { get; set; }
|
||||
[CommandOption("option-c", 'c')]
|
||||
public string? OptionC { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-enum-args")]
|
||||
private class EnumArgumentsCommand : ICommand
|
||||
{
|
||||
public enum CustomEnum { Value1, Value2, Value3 };
|
||||
|
||||
[CommandParameter(0, Name = "value", Description = "Enum parameter.")]
|
||||
public CustomEnum ParamA { get; set; }
|
||||
|
||||
[CommandOption("value", Description = "Enum option.", IsRequired = true)]
|
||||
public CustomEnum OptionA { get; set; } = CustomEnum.Value1;
|
||||
|
||||
[CommandOption("nullable-value", Description = "Nullable enum option.")]
|
||||
public CustomEnum? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
@@ -92,5 +110,46 @@ namespace CliFx.Tests
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-defaults")]
|
||||
private class ArgumentsWithDefaultValuesCommand : ICommand
|
||||
{
|
||||
public enum CustomEnum { Value1, Value2, Value3 };
|
||||
|
||||
[CommandOption(nameof(Object))]
|
||||
public object? Object { get; set; } = 42;
|
||||
|
||||
[CommandOption(nameof(String))]
|
||||
public string? String { get; set; } = "foo";
|
||||
|
||||
[CommandOption(nameof(EmptyString))]
|
||||
public string EmptyString { get; set; } = "";
|
||||
|
||||
[CommandOption(nameof(Bool))]
|
||||
public bool Bool { get; set; } = true;
|
||||
|
||||
[CommandOption(nameof(Char))]
|
||||
public char Char { get; set; } = 't';
|
||||
|
||||
[CommandOption(nameof(Int))]
|
||||
public int Int { get; set; } = 1337;
|
||||
|
||||
[CommandOption(nameof(TimeSpan))]
|
||||
public TimeSpan TimeSpan { get; set; } = TimeSpan.FromMinutes(123);
|
||||
|
||||
[CommandOption(nameof(Enum))]
|
||||
public CustomEnum Enum { get; set; } = CustomEnum.Value2;
|
||||
|
||||
[CommandOption(nameof(IntNullable))]
|
||||
public int? IntNullable { get; set; } = 1337;
|
||||
|
||||
[CommandOption(nameof(StringArray))]
|
||||
public string[]? StringArray { get; set; } = { "foo", "bar", "baz" };
|
||||
|
||||
[CommandOption(nameof(IntArray))]
|
||||
public int[]? IntArray { get; set; } = { 1, 2, 3 };
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,19 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
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()
|
||||
{
|
||||
@@ -29,6 +36,8 @@ namespace CliFx.Tests
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Be("v6.9");
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -68,6 +77,8 @@ namespace CliFx.Tests
|
||||
"cmd", "NamedCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -104,6 +115,8 @@ namespace CliFx.Tests
|
||||
"sub", "SubCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -137,6 +150,8 @@ namespace CliFx.Tests
|
||||
"-e|--option-e", "OptionE description.",
|
||||
"-h|--help", "Shows help text."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -167,6 +182,8 @@ namespace CliFx.Tests
|
||||
"cmd", "NamedCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -190,6 +207,8 @@ namespace CliFx.Tests
|
||||
"Usage",
|
||||
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -211,12 +230,44 @@ namespace CliFx.Tests
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-req-opts", "--option-f <value>", "--option-g <values...>", "[options]",
|
||||
"cmd-with-req-opts", "--option-a <value>", "--option-b <values...>", "[options]",
|
||||
"Options",
|
||||
"* -f|--option-f",
|
||||
"* -g|--option-g",
|
||||
"-h|--option-h"
|
||||
"* -a|--option-a",
|
||||
"* -b|--option-b",
|
||||
"-c|--option-c"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_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]
|
||||
@@ -241,6 +292,45 @@ namespace CliFx.Tests
|
||||
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
||||
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_default_values_for_non_required_options()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ArgumentsWithDefaultValuesCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-defaults", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-defaults", "[options]",
|
||||
"Options",
|
||||
"--Object", "Default: \"42\"",
|
||||
"--String", "Default: \"foo\"",
|
||||
"--EmptyString", "Default: \"\"",
|
||||
"--Bool", "Default: \"True\"",
|
||||
"--Char", "Default: \"t\"",
|
||||
"--Int", "Default: \"1337\"",
|
||||
"--TimeSpan", "Default: \"02:03:00\"",
|
||||
"--Enum", "Default: \"Value2\"",
|
||||
"--IntNullable", "Default: \"1337\"",
|
||||
"--StringArray", "Default: \"foo\" \"bar\" \"baz\"",
|
||||
"--IntArray", "Default: \"1\" \"2\" \"3\""
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
}
|
||||
}
|
||||
23
CliFx.Tests/Internal/CommandHelper.cs
Normal file
23
CliFx.Tests/Internal/CommandHelper.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Domain;
|
||||
|
||||
namespace CliFx.Tests.Internal
|
||||
{
|
||||
internal static class CommandHelper
|
||||
{
|
||||
public static TCommand ResolveCommand<TCommand>(CommandInput input, IReadOnlyDictionary<string, string> environmentVariables)
|
||||
where TCommand : ICommand, new()
|
||||
{
|
||||
var schema = CommandSchema.TryResolve(typeof(TCommand))!;
|
||||
|
||||
var instance = new TCommand();
|
||||
schema.Bind(instance, input, environmentVariables);
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static TCommand ResolveCommand<TCommand>(CommandInput input)
|
||||
where TCommand : ICommand, new() =>
|
||||
ResolveCommand<TCommand>(input, new Dictionary<string, string>());
|
||||
}
|
||||
}
|
||||
45
CliFx.Tests/Internal/CommandInputBuilder.cs
Normal file
45
CliFx.Tests/Internal/CommandInputBuilder.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Domain;
|
||||
|
||||
namespace CliFx.Tests.Internal
|
||||
{
|
||||
internal class CommandInputBuilder
|
||||
{
|
||||
private readonly List<CommandDirectiveInput> _directives = new List<CommandDirectiveInput>();
|
||||
private readonly List<CommandParameterInput> _parameters = new List<CommandParameterInput>();
|
||||
private readonly List<CommandOptionInput> _options = new List<CommandOptionInput>();
|
||||
|
||||
private string? _commandName;
|
||||
|
||||
public CommandInputBuilder SetCommandName(string commandName)
|
||||
{
|
||||
_commandName = commandName;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInputBuilder AddDirective(string directive)
|
||||
{
|
||||
_directives.Add(new CommandDirectiveInput(directive));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInputBuilder AddParameter(string parameter)
|
||||
{
|
||||
_parameters.Add(new CommandParameterInput(parameter));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInputBuilder AddOption(string alias, params string[] values)
|
||||
{
|
||||
_options.Add(new CommandOptionInput(alias, values));
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandInput Build() => new CommandInput(
|
||||
_directives,
|
||||
_commandName,
|
||||
_parameters,
|
||||
_options
|
||||
);
|
||||
}
|
||||
}
|
||||
19
CliFx.Tests/Internal/TaskExtensions.cs
Normal file
19
CliFx.Tests/Internal/TaskExtensions.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CliFx.Tests.Internal
|
||||
{
|
||||
internal static class TaskExtensions
|
||||
{
|
||||
public static async Task IgnoreCancellation(this Task task)
|
||||
{
|
||||
try
|
||||
{
|
||||
await task;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
@@ -20,7 +21,7 @@ namespace CliFx.Tests
|
||||
private class ConcatCommand : ICommand
|
||||
{
|
||||
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
||||
public IReadOnlyList<string> Inputs { get; set; }
|
||||
public IReadOnlyList<string> Inputs { get; set; } = Array.Empty<string>();
|
||||
|
||||
[CommandOption('s', Description = "String separator.")]
|
||||
public string Separator { get; set; } = "";
|
||||
@@ -36,10 +37,10 @@ namespace CliFx.Tests
|
||||
private class DivideCommand : ICommand
|
||||
{
|
||||
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
||||
public double Dividend { get; set; }
|
||||
public double Dividend { get; set; } = 0;
|
||||
|
||||
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
||||
public double Divisor { get; set; }
|
||||
public double Divisor { get; set; } = 0;
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
|
||||
@@ -3,11 +3,16 @@ 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()
|
||||
{
|
||||
@@ -28,6 +33,8 @@ namespace CliFx.Tests
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(progressStringValues);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -49,6 +56,8 @@ namespace CliFx.Tests
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().BeEmpty();
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>1.1</Version>
|
||||
<Version>1.3</Version>
|
||||
<Company>Tyrrrz</Company>
|
||||
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
||||
<LangVersion>latest</LangVersion>
|
||||
|
||||
32
CliFx.sln
32
CliFx.sln
@@ -10,16 +10,20 @@ EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3AAE8166-BB8E-49DA-844C-3A0EE6BD40A0}"
|
||||
ProjectSection(SolutionItems) = preProject
|
||||
Changelog.md = Changelog.md
|
||||
CliFx.props = CliFx.props
|
||||
License.txt = License.txt
|
||||
Readme.md = Readme.md
|
||||
CliFx.props = CliFx.props
|
||||
EndProjectSection
|
||||
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("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Tests.Dummy", "CliFx.Tests.Dummy\CliFx.Tests.Dummy.csproj", "{F717347D-8656-44DA-A4A2-BE515E8C4655}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CliFx.Analyzers", "CliFx.Analyzers\CliFx.Analyzers.csproj", "{F8460D69-F8CF-405C-A6ED-BED02A21DB42}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CliFx.Analyzers.Tests", "CliFx.Analyzers.Tests\CliFx.Analyzers.Tests.csproj", "{49878E75-2097-4C79-9151-B98A28FBB973}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@@ -91,6 +95,30 @@ Global
|
||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|x64.Build.0 = Release|Any CPU
|
||||
{F717347D-8656-44DA-A4A2-BE515E8C4655}.Release|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
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -9,14 +9,14 @@ namespace CliFx.Attributes
|
||||
public class CommandOptionAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Option name.
|
||||
/// 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; }
|
||||
|
||||
/// <summary>
|
||||
/// Option short name.
|
||||
/// 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>
|
||||
|
||||
@@ -4,15 +4,17 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Command line application facade.
|
||||
/// </summary>
|
||||
public class CliApplication
|
||||
public partial class CliApplication
|
||||
{
|
||||
private readonly ApplicationMetadata _metadata;
|
||||
private readonly ApplicationConfiguration _configuration;
|
||||
@@ -36,42 +38,33 @@ namespace CliFx
|
||||
_helpTextWriter = new HelpTextWriter(metadata, console);
|
||||
}
|
||||
|
||||
private async ValueTask<int?> HandleDebugDirectiveAsync(CommandLineInput commandLineInput)
|
||||
private void WriteError(string message) => _console.WithForegroundColor(ConsoleColor.Red, () =>
|
||||
_console.Error.WriteLine(message));
|
||||
|
||||
private async ValueTask WaitForDebuggerAsync()
|
||||
{
|
||||
var isDebugMode = _configuration.IsDebugModeAllowed && commandLineInput.IsDebugDirectiveSpecified;
|
||||
if (!isDebugMode)
|
||||
return null;
|
||||
var processId = ProcessEx.GetCurrentProcessId();
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.Green, () =>
|
||||
_console.Output.WriteLine($"Attach debugger to PID {Process.GetCurrentProcess().Id} to continue."));
|
||||
_console.Output.WriteLine($"Attach debugger to PID {processId} to continue."));
|
||||
|
||||
while (!Debugger.IsAttached)
|
||||
await Task.Delay(100);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||
private void WriteCommandLineInput(CommandInput input)
|
||||
{
|
||||
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
|
||||
if (!isPreviewMode)
|
||||
return null;
|
||||
|
||||
var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset);
|
||||
|
||||
_console.Output.WriteLine("Parser preview:");
|
||||
|
||||
// Command name
|
||||
if (commandSchema != null && argumentOffset > 0)
|
||||
if (!string.IsNullOrWhiteSpace(input.CommandName))
|
||||
{
|
||||
_console.WithForegroundColor(ConsoleColor.Cyan, () =>
|
||||
_console.Output.Write(commandSchema.Name));
|
||||
_console.Output.Write(input.CommandName));
|
||||
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
// Parameters
|
||||
foreach (var parameter in commandLineInput.UnboundArguments.Skip(argumentOffset))
|
||||
foreach (var parameter in input.Parameters)
|
||||
{
|
||||
_console.Output.Write('<');
|
||||
|
||||
@@ -83,100 +76,133 @@ namespace CliFx
|
||||
}
|
||||
|
||||
// Options
|
||||
foreach (var option in commandLineInput.Options)
|
||||
foreach (var option in input.Options)
|
||||
{
|
||||
_console.Output.Write('[');
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||
_console.Output.Write(option));
|
||||
{
|
||||
// Alias
|
||||
_console.Output.Write(option.GetRawAlias());
|
||||
|
||||
// Values
|
||||
if (option.Values.Any())
|
||||
{
|
||||
_console.Output.Write(' ');
|
||||
_console.Output.Write(option.GetRawValues());
|
||||
}
|
||||
});
|
||||
|
||||
_console.Output.Write(']');
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
_console.Output.WriteLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int? HandleVersionOption(CommandLineInput commandLineInput)
|
||||
{
|
||||
// Version option is available only on the default command (i.e. when arguments are not specified)
|
||||
var shouldRenderVersion = !commandLineInput.UnboundArguments.Any() && commandLineInput.IsVersionOptionSpecified;
|
||||
if (!shouldRenderVersion)
|
||||
return null;
|
||||
|
||||
_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;
|
||||
}
|
||||
private ICommand GetCommandInstance(CommandSchema command) =>
|
||||
command != StubDefaultCommand.Schema
|
||||
? (ICommand) _typeActivator.CreateInstance(command.Type)
|
||||
: new StubDefaultCommand();
|
||||
|
||||
/// <summary>
|
||||
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
try
|
||||
{
|
||||
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
|
||||
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
|
||||
var root = RootSchema.Resolve(_configuration.CommandTypes);
|
||||
var input = CommandInput.Parse(commandLineArguments, root.GetCommandNames());
|
||||
|
||||
return
|
||||
await HandleDebugDirectiveAsync(commandLineInput) ??
|
||||
HandlePreviewDirective(applicationSchema, commandLineInput) ??
|
||||
HandleVersionOption(commandLineInput) ??
|
||||
HandleHelpOption(applicationSchema, commandLineInput) ??
|
||||
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
||||
}
|
||||
catch (Exception ex)
|
||||
// Debug mode
|
||||
if (_configuration.IsDebugModeAllowed && input.IsDebugDirectiveSpecified)
|
||||
{
|
||||
// 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.
|
||||
// Ensure debugger is attached and continue
|
||||
await WaitForDebuggerAsync();
|
||||
}
|
||||
|
||||
// Prefer showing message without stack trace on exceptions coming from CliFx or on CommandException
|
||||
var errorMessage = !string.IsNullOrWhiteSpace(ex.Message) && (ex is CliFxException || ex is CommandException)
|
||||
? ex.Message
|
||||
: ex.ToString();
|
||||
// Preview mode
|
||||
if (_configuration.IsPreviewModeAllowed && input.IsPreviewDirectiveSpecified)
|
||||
{
|
||||
WriteCommandLineInput(input);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
|
||||
// Try to get the command matching the input or fallback to default
|
||||
var command =
|
||||
root.TryFindCommand(input.CommandName) ??
|
||||
root.TryFindDefaultCommand() ??
|
||||
StubDefaultCommand.Schema;
|
||||
|
||||
return ex is CommandException commandException
|
||||
? commandException.ExitCode
|
||||
: ex.HResult;
|
||||
// Version option
|
||||
if (command.IsVersionOptionAvailable && input.IsVersionOptionSpecified)
|
||||
{
|
||||
_console.Output.WriteLine(_metadata.VersionText);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// Get command instance (also used in help text)
|
||||
var instance = GetCommandInstance(command);
|
||||
|
||||
// To avoid instantiating the command twice, we need to get default values
|
||||
// before the arguments are bound to the properties
|
||||
var defaultValues = command.GetArgumentValues(instance);
|
||||
|
||||
// Help option
|
||||
if (command.IsHelpOptionAvailable && input.IsHelpOptionSpecified ||
|
||||
command == StubDefaultCommand.Schema && !input.Parameters.Any() && !input.Options.Any())
|
||||
{
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// Bind arguments
|
||||
try
|
||||
{
|
||||
command.Bind(instance, input, environmentVariables);
|
||||
}
|
||||
// This may throw exceptions which are useful only to the end-user
|
||||
catch (CliFxException ex)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
|
||||
return ExitCode.FromException(ex);
|
||||
}
|
||||
|
||||
// Execute the command
|
||||
try
|
||||
{
|
||||
await instance.ExecuteAsync(_console);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
// Swallow command exceptions and route them to the console
|
||||
catch (CommandException ex)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
|
||||
if (ex.ShowHelp)
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
|
||||
return ex.ExitCode;
|
||||
}
|
||||
}
|
||||
// To prevent the app from showing the annoying Windows troubleshooting dialog,
|
||||
// we handle all exceptions and route them to the console nicely.
|
||||
// However, we don't want to swallow unhandled exceptions when the debugger is attached,
|
||||
// because we still want the IDE to show them to the developer.
|
||||
catch (Exception ex) when (!Debugger.IsAttached)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
return ExitCode.FromException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +210,11 @@ namespace CliFx
|
||||
/// Runs the application with specified command line arguments and returns the exit code.
|
||||
/// Environment variables are retrieved automatically.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments)
|
||||
{
|
||||
var environmentVariables = Environment.GetEnvironmentVariables()
|
||||
@@ -197,6 +228,11 @@ namespace CliFx
|
||||
/// Runs the application and returns the exit code.
|
||||
/// Command line arguments and environment variables are retrieved automatically.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync()
|
||||
{
|
||||
var commandLineArguments = Environment.GetCommandLineArgs()
|
||||
@@ -206,4 +242,25 @@ namespace CliFx
|
||||
return await RunAsync(commandLineArguments);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class CliApplication
|
||||
{
|
||||
private static class ExitCode
|
||||
{
|
||||
public const int Success = 0;
|
||||
|
||||
public static int FromException(Exception ex) =>
|
||||
ex is CommandException cmdEx
|
||||
? cmdEx.ExitCode
|
||||
: ex.HResult;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class StubDefaultCommand : ICommand
|
||||
{
|
||||
public static CommandSchema Schema { get; } = CommandSchema.TryResolve(typeof(StubDefaultCommand))!;
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
@@ -158,9 +159,9 @@ namespace CliFx
|
||||
/// </summary>
|
||||
public CliApplication Build()
|
||||
{
|
||||
_title ??= GetDefaultTitle() ?? "App";
|
||||
_executableName ??= GetDefaultExecutableName() ?? "app";
|
||||
_versionText ??= GetDefaultVersionText() ?? "v1.0";
|
||||
_title ??= TryGetDefaultTitle() ?? "App";
|
||||
_executableName ??= TryGetDefaultExecutableName() ?? "app";
|
||||
_versionText ??= TryGetDefaultVersionText() ?? "v1.0";
|
||||
_console ??= new SystemConsole();
|
||||
_typeActivator ??= new DefaultTypeActivator();
|
||||
|
||||
@@ -173,30 +174,28 @@ namespace CliFx
|
||||
|
||||
public partial class CliApplicationBuilder
|
||||
{
|
||||
private static readonly Lazy<Assembly> LazyEntryAssembly = new Lazy<Assembly>(Assembly.GetEntryAssembly);
|
||||
private static readonly Lazy<Assembly?> LazyEntryAssembly = new Lazy<Assembly?>(Assembly.GetEntryAssembly);
|
||||
|
||||
// Entry assembly is null in tests
|
||||
private static Assembly EntryAssembly => LazyEntryAssembly.Value;
|
||||
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
|
||||
|
||||
private static string? GetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||
private static string? TryGetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||
|
||||
private static string? GetDefaultExecutableName()
|
||||
private static string? TryGetDefaultExecutableName()
|
||||
{
|
||||
var entryAssemblyLocation = EntryAssembly?.Location;
|
||||
|
||||
// If it's a .dll assembly, prepend 'dotnet' and keep the file extension
|
||||
if (string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return "dotnet " + Path.GetFileName(entryAssemblyLocation);
|
||||
// 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);
|
||||
}
|
||||
|
||||
// Otherwise just use assembly file name without extension
|
||||
return Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
||||
}
|
||||
|
||||
private static string? GetDefaultVersionText() =>
|
||||
private static string? TryGetDefaultVersionText() =>
|
||||
EntryAssembly != null
|
||||
? $"v{EntryAssembly.GetName().Version}"
|
||||
? $"v{EntryAssembly.GetName().Version.ToSemanticString()}"
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<Import Project="../CliFx.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.1;netstandard2.0;net45</TargetFrameworks>
|
||||
<TargetFrameworks>netstandard2.1;netstandard2.0</TargetFrameworks>
|
||||
<Authors>$(Company)</Authors>
|
||||
<Description>Declarative framework for CLI applications</Description>
|
||||
<PackageTags>command line executable interface framework parser arguments net core</PackageTags>
|
||||
@@ -10,12 +10,17 @@
|
||||
<PackageReleaseNotes>https://github.com/Tyrrrz/CliFx/blob/master/Changelog.md</PackageReleaseNotes>
|
||||
<PackageIcon>favicon.png</PackageIcon>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
|
||||
<GenerateDocumentationFile>True</GenerateDocumentationFile>
|
||||
<PublishRepositoryUrl>True</PublishRepositoryUrl>
|
||||
<EmbedUntrackedSources>True</EmbedUntrackedSources>
|
||||
<IncludeSymbols>True</IncludeSymbols>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<PublishRepositoryUrl>true</PublishRepositoryUrl>
|
||||
<EmbedUntrackedSources>true</EmbedUntrackedSources>
|
||||
<IncludeSymbols>true</IncludeSymbols>
|
||||
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
|
||||
<TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);CopyAnalyzerToPackage</TargetsForTfmSpecificContentInPackage>
|
||||
</PropertyGroup>
|
||||
|
||||
<!-- Disable nullability warnings on older frameworks because there is no nullability info for BCL -->
|
||||
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
|
||||
<Nullable>annotations</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -27,18 +32,29 @@
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="../favicon.png" Pack="True" PackagePath="" />
|
||||
</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' or '$(TargetFramework)' == 'net45'">
|
||||
<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>
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx
|
||||
@@ -18,11 +17,7 @@ namespace CliFx
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.Append($"Failed to create an instance of {type.FullName}.").Append(" ")
|
||||
.AppendLine("The type must have a public parameter-less constructor in order to be instantiated by the default activator.")
|
||||
.Append($"To supply a custom activator (for example when using dependency injection), call {nameof(CliApplicationBuilder)}.{nameof(CliApplicationBuilder.UseTypeActivator)}(...).")
|
||||
.ToString(), ex);
|
||||
throw CliFxException.DefaultActivatorFailed(type, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx
|
||||
@@ -18,10 +17,6 @@ namespace CliFx
|
||||
|
||||
/// <inheritdoc />
|
||||
public object CreateInstance(Type type) =>
|
||||
_func(type) ?? throw new CliFxException(new StringBuilder()
|
||||
.Append($"Failed to create an instance of type {type.FullName}, received <null> instead.").Append(" ")
|
||||
.Append("Make sure that the provided type activator was configured correctly.").Append(" ")
|
||||
.Append("If you are using a dependency container, make sure that this type is registered.")
|
||||
.ToString());
|
||||
_func(type) ?? throw CliFxException.DelegateActivatorReturnedNull(type);
|
||||
}
|
||||
}
|
||||
@@ -1,267 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
|
||||
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);
|
||||
if (command == null)
|
||||
{
|
||||
throw new CliFxException(
|
||||
$"Can't find a command that matches arguments [{string.Join(" ", commandLineInput.UnboundArguments)}].");
|
||||
}
|
||||
|
||||
var parameterValues = argumentOffset == 0
|
||||
? commandLineInput.UnboundArguments.Select(a => a.Value).ToArray()
|
||||
: commandLineInput.UnboundArguments.Skip(argumentOffset).Select(a => a.Value).ToArray();
|
||||
|
||||
return command.CreateInstance(parameterValues, 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 new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Command {command.Type.FullName} contains two or more parameters that have the same order ({duplicateOrderGroup.Key}):")
|
||||
.AppendBulletList(duplicateOrderGroup.Select(o => o.Property.Name))
|
||||
.AppendLine()
|
||||
.Append("Parameters in a command must all have unique order.")
|
||||
.ToString());
|
||||
}
|
||||
|
||||
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 new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Command {command.Type.FullName} contains two or more parameters that have the same name ({duplicateNameGroup.Key}):")
|
||||
.AppendBulletList(duplicateNameGroup.Select(o => o.Property.Name))
|
||||
.AppendLine()
|
||||
.Append("Parameters in a command must all have unique names.").Append(" ")
|
||||
.Append("Comparison is NOT case-sensitive.")
|
||||
.ToString());
|
||||
}
|
||||
|
||||
var nonScalarParameters = command.Parameters
|
||||
.Where(p => !p.IsScalar)
|
||||
.ToArray();
|
||||
|
||||
if (nonScalarParameters.Length > 1)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Command [{command.Type.FullName}] contains two or more parameters of an enumerable type:")
|
||||
.AppendBulletList(nonScalarParameters.Select(o => o.Property.Name))
|
||||
.AppendLine()
|
||||
.AppendLine("There can only be one parameter of an enumerable type in a command.")
|
||||
.Append("Note, the string type is not considered enumerable in this context.")
|
||||
.ToString());
|
||||
}
|
||||
|
||||
var nonLastNonScalarParameter = command.Parameters
|
||||
.OrderByDescending(a => a.Order)
|
||||
.Skip(1)
|
||||
.LastOrDefault(p => !p.IsScalar);
|
||||
|
||||
if (nonLastNonScalarParameter != null)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Command {command.Type.FullName} contains a parameter of an enumerable type which doesn't appear last in order:")
|
||||
.AppendLine($"- {nonLastNonScalarParameter.Property.Name}")
|
||||
.AppendLine()
|
||||
.Append("Parameter of an enumerable type must always come last to avoid ambiguity.")
|
||||
.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateOptions(CommandSchema command)
|
||||
{
|
||||
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 new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same name ({duplicateNameGroup.Key}):")
|
||||
.AppendBulletList(duplicateNameGroup.Select(o => o.Property.Name))
|
||||
.AppendLine()
|
||||
.Append("Options in a command must all have unique names.").Append(" ")
|
||||
.Append("Comparison is NOT case-sensitive.")
|
||||
.ToString());
|
||||
}
|
||||
|
||||
var duplicateShortNameGroup = command.Options
|
||||
.Where(o => o.ShortName != null)
|
||||
.GroupBy(o => o.ShortName)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateShortNameGroup != null)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same short name ({duplicateShortNameGroup.Key}):")
|
||||
.AppendBulletList(duplicateShortNameGroup.Select(o => o.Property.Name))
|
||||
.AppendLine()
|
||||
.Append("Options in a command must all have unique short names.").Append(" ")
|
||||
.Append("Comparison is case-sensitive.")
|
||||
.ToString());
|
||||
}
|
||||
|
||||
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 new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Command {command.Type.FullName} contains two or more options that have the same environment variable name ({duplicateEnvironmentVariableNameGroup.Key}):")
|
||||
.AppendBulletList(duplicateEnvironmentVariableNameGroup.Select(o => o.Property.Name))
|
||||
.AppendLine()
|
||||
.Append("Options in a command must all have unique environment variable names.").Append(" ")
|
||||
.Append("Comparison is NOT case-sensitive.")
|
||||
.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
||||
{
|
||||
if (!commands.Any())
|
||||
{
|
||||
throw new CliFxException("There are no commands configured for this application.");
|
||||
}
|
||||
|
||||
var duplicateNameGroup = commands
|
||||
.GroupBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Application contains two or more commands that have the same name ({duplicateNameGroup.Key}):")
|
||||
.AppendBulletList(duplicateNameGroup.Select(o => o.Type.FullName))
|
||||
.AppendLine()
|
||||
.Append("Commands must all have unique names. Likewise, there must not be more than one command without a name.").Append(" ")
|
||||
.Append("Comparison is NOT case-sensitive.")
|
||||
.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public static ApplicationSchema Resolve(IReadOnlyList<Type> commandTypes)
|
||||
{
|
||||
var commands = new List<CommandSchema>();
|
||||
|
||||
foreach (var commandType in commandTypes)
|
||||
{
|
||||
var command = CommandSchema.TryResolve(commandType);
|
||||
if (command == null)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.Append($"Command {commandType.FullName} is not a valid command type.").Append(" ")
|
||||
.AppendLine("In order to be a valid command type it must:")
|
||||
.AppendLine($" - Be annotated with {typeof(CommandAttribute).FullName}")
|
||||
.AppendLine($" - Implement {typeof(ICommand).FullName}")
|
||||
.AppendLine(" - Not be an abstract class")
|
||||
.ToString());
|
||||
}
|
||||
|
||||
ValidateParameters(command);
|
||||
ValidateOptions(command);
|
||||
|
||||
commands.Add(command);
|
||||
}
|
||||
|
||||
ValidateCommands(commands);
|
||||
|
||||
return new ApplicationSchema(commands);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
|
||||
@@ -11,96 +10,25 @@ namespace CliFx.Domain
|
||||
{
|
||||
internal abstract partial class CommandArgumentSchema
|
||||
{
|
||||
public PropertyInfo Property { get; }
|
||||
// Property can be null on built-in arguments (help and version options)
|
||||
public PropertyInfo? Property { get; }
|
||||
|
||||
public string? Description { get; }
|
||||
|
||||
public bool IsScalar => GetEnumerableArgumentUnderlyingType() == null;
|
||||
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == null;
|
||||
|
||||
protected CommandArgumentSchema(PropertyInfo property, string? description)
|
||||
protected CommandArgumentSchema(PropertyInfo? property, string? description)
|
||||
{
|
||||
Property = property;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
private Type? GetEnumerableArgumentUnderlyingType() =>
|
||||
Property.PropertyType != typeof(string)
|
||||
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
||||
Property != null && Property.PropertyType != typeof(string)
|
||||
? Property.PropertyType.GetEnumerableUnderlyingType()
|
||||
: null;
|
||||
|
||||
private object Convert(IReadOnlyList<string> values)
|
||||
{
|
||||
var targetType = Property.PropertyType;
|
||||
var enumerableUnderlyingType = GetEnumerableArgumentUnderlyingType();
|
||||
|
||||
// Scalar
|
||||
if (enumerableUnderlyingType == null)
|
||||
{
|
||||
if (values.Count > 1)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Can't convert a sequence of values [{string.Join(", ", values)}] to type {targetType.FullName}.")
|
||||
.Append("Target type is not enumerable and can't accept more than one value.")
|
||||
.ToString());
|
||||
}
|
||||
|
||||
return ConvertScalar(values.SingleOrDefault(), targetType);
|
||||
}
|
||||
// Non-scalar
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
private static object ConvertScalar(string? value, Type targetType)
|
||||
private object? ConvertScalar(string? value, Type targetType)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -121,35 +49,29 @@ namespace CliFx.Domain
|
||||
: null;
|
||||
|
||||
// String-constructable
|
||||
var stringConstructor = GetStringConstructor(targetType);
|
||||
var stringConstructor = targetType.GetConstructor(new[] {typeof(string)});
|
||||
if (stringConstructor != null)
|
||||
return stringConstructor.Invoke(new object[] {value});
|
||||
return stringConstructor.Invoke(new object[] {value!});
|
||||
|
||||
// String-parseable (with format provider)
|
||||
var parseMethodWithFormatProvider = GetStaticParseMethodWithFormatProvider(targetType);
|
||||
var parseMethodWithFormatProvider = targetType.GetStaticParseMethod(true);
|
||||
if (parseMethodWithFormatProvider != null)
|
||||
return parseMethodWithFormatProvider.Invoke(null, new object[] {value, ConversionFormatProvider});
|
||||
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, FormatProvider});
|
||||
|
||||
// String-parseable (without format provider)
|
||||
var parseMethod = GetStaticParseMethod(targetType);
|
||||
var parseMethod = targetType.GetStaticParseMethod();
|
||||
if (parseMethod != null)
|
||||
return parseMethod.Invoke(null, new object[] {value});
|
||||
return parseMethod.Invoke(null, new object[] {value!});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Failed to convert value '{value ?? "<null>"}' to type {targetType.FullName}.")
|
||||
.Append(ex.Message)
|
||||
.ToString(), ex);
|
||||
throw CliFxException.CannotConvertToType(this, value, targetType, ex);
|
||||
}
|
||||
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Can't convert value '{value ?? "<null>"}' to type {targetType.FullName}.")
|
||||
.Append("Target type is not supported by CliFx.")
|
||||
.ToString());
|
||||
throw CliFxException.CannotConvertToType(this, value, targetType);
|
||||
}
|
||||
|
||||
private static object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
|
||||
private object ConvertNonScalar(IReadOnlyList<string> values, Type targetEnumerableType, Type targetElementType)
|
||||
{
|
||||
var array = values
|
||||
.Select(v => ConvertScalar(v, targetElementType))
|
||||
@@ -166,11 +88,80 @@ namespace CliFx.Domain
|
||||
if (arrayConstructor != null)
|
||||
return arrayConstructor.Invoke(new object[] {array});
|
||||
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine($"Can't convert a sequence of values [{string.Join(", ", values)}] to type {targetEnumerableType.FullName}.")
|
||||
.AppendLine($"Underlying element type is [{targetElementType.FullName}].")
|
||||
.Append("Target type must either be assignable from an array or have a public constructor that takes a single array argument.")
|
||||
.ToString());
|
||||
throw CliFxException.CannotConvertNonScalar(this, values, targetEnumerableType);
|
||||
}
|
||||
|
||||
private object? Convert(IReadOnlyList<string> values)
|
||||
{
|
||||
// Short-circuit built-in arguments
|
||||
if (Property == null)
|
||||
return null;
|
||||
|
||||
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 BindOn(ICommand command, IReadOnlyList<string> values) =>
|
||||
Property?.SetValue(command, Convert(values));
|
||||
|
||||
public void BindOn(ICommand command, params string[] values) =>
|
||||
BindOn(command, (IReadOnlyList<string>) values);
|
||||
|
||||
public IReadOnlyList<string> GetValidValues()
|
||||
{
|
||||
if (Property == null)
|
||||
return Array.Empty<string>();
|
||||
|
||||
var underlyingType =
|
||||
Property.PropertyType.GetNullableUnderlyingType() ??
|
||||
Property.PropertyType;
|
||||
|
||||
// Enum
|
||||
if (underlyingType.IsEnum)
|
||||
return Enum.GetNames(underlyingType);
|
||||
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandArgumentSchema
|
||||
{
|
||||
private static readonly IFormatProvider FormatProvider = 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, FormatProvider),
|
||||
[typeof(byte)] = v => byte.Parse(v, FormatProvider),
|
||||
[typeof(short)] = v => short.Parse(v, FormatProvider),
|
||||
[typeof(ushort)] = v => ushort.Parse(v, FormatProvider),
|
||||
[typeof(int)] = v => int.Parse(v, FormatProvider),
|
||||
[typeof(uint)] = v => uint.Parse(v, FormatProvider),
|
||||
[typeof(long)] = v => long.Parse(v, FormatProvider),
|
||||
[typeof(ulong)] = v => ulong.Parse(v, FormatProvider),
|
||||
[typeof(float)] = v => float.Parse(v, FormatProvider),
|
||||
[typeof(double)] = v => double.Parse(v, FormatProvider),
|
||||
[typeof(decimal)] = v => decimal.Parse(v, FormatProvider),
|
||||
[typeof(DateTime)] = v => DateTime.Parse(v, FormatProvider),
|
||||
[typeof(DateTimeOffset)] = v => DateTimeOffset.Parse(v, FormatProvider),
|
||||
[typeof(TimeSpan)] = v => TimeSpan.Parse(v, FormatProvider),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -10,10 +10,7 @@ namespace CliFx.Domain
|
||||
|
||||
public bool IsPreviewDirective => string.Equals(Name, "preview", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public CommandDirectiveInput(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
public CommandDirectiveInput(string name) => Name = name;
|
||||
|
||||
public override string ToString() => $"[{Name}]";
|
||||
}
|
||||
|
||||
238
CliFx/Domain/CommandInput.cs
Normal file
238
CliFx/Domain/CommandInput.cs
Normal file
@@ -0,0 +1,238 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal partial class CommandInput
|
||||
{
|
||||
public IReadOnlyList<CommandDirectiveInput> Directives { get; }
|
||||
|
||||
public string? CommandName { get; }
|
||||
|
||||
public IReadOnlyList<CommandParameterInput> Parameters { 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 CommandInput(
|
||||
IReadOnlyList<CommandDirectiveInput> directives,
|
||||
string? commandName,
|
||||
IReadOnlyList<CommandParameterInput> parameters,
|
||||
IReadOnlyList<CommandOptionInput> options)
|
||||
{
|
||||
Directives = directives;
|
||||
CommandName = commandName;
|
||||
Parameters = parameters;
|
||||
Options = options;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
foreach (var directive in Directives)
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(directive);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(CommandName))
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(CommandName);
|
||||
}
|
||||
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(parameter);
|
||||
}
|
||||
|
||||
foreach (var option in Options)
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(option);
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandInput
|
||||
{
|
||||
private static IReadOnlyList<CommandDirectiveInput> ParseDirectives(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ref int index)
|
||||
{
|
||||
var result = new List<CommandDirectiveInput>();
|
||||
|
||||
for (; index < commandLineArguments.Count; index++)
|
||||
{
|
||||
var argument = commandLineArguments[index];
|
||||
|
||||
if (!argument.StartsWith('[') || !argument.EndsWith(']'))
|
||||
break;
|
||||
|
||||
var name = argument.Substring(1, argument.Length - 2);
|
||||
result.Add(new CommandDirectiveInput(name));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string? ParseCommandName(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ISet<string> commandNames,
|
||||
ref int index)
|
||||
{
|
||||
var buffer = new List<string>();
|
||||
|
||||
var commandName = default(string?);
|
||||
var lastIndex = index;
|
||||
|
||||
// We need to look ahead to see if we can match as many consecutive arguments to a command name as possible
|
||||
for (var i = index; i < commandLineArguments.Count; i++)
|
||||
{
|
||||
var argument = commandLineArguments[i];
|
||||
buffer.Add(argument);
|
||||
|
||||
var potentialCommandName = buffer.JoinToString(" ");
|
||||
|
||||
if (commandNames.Contains(potentialCommandName))
|
||||
{
|
||||
commandName = potentialCommandName;
|
||||
lastIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the index only if command name was found in the arguments
|
||||
if (!string.IsNullOrWhiteSpace(commandName))
|
||||
index = lastIndex + 1;
|
||||
|
||||
return commandName;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<CommandParameterInput> ParseParameters(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ref int index)
|
||||
{
|
||||
var result = new List<CommandParameterInput>();
|
||||
|
||||
for (; index < commandLineArguments.Count; index++)
|
||||
{
|
||||
var argument = commandLineArguments[index];
|
||||
|
||||
if (argument.StartsWith('-'))
|
||||
break;
|
||||
|
||||
result.Add(new CommandParameterInput(argument));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<CommandOptionInput> ParseOptions(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ref int index)
|
||||
{
|
||||
var result = new List<CommandOptionInput>();
|
||||
|
||||
var currentOptionAlias = default(string?);
|
||||
var currentOptionValues = new List<string>();
|
||||
|
||||
for (; index < commandLineArguments.Count; index++)
|
||||
{
|
||||
var argument = commandLineArguments[index];
|
||||
|
||||
// Name
|
||||
if (argument.StartsWith("--", StringComparison.Ordinal))
|
||||
{
|
||||
// Flush previous
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
|
||||
|
||||
currentOptionAlias = argument.Substring(2);
|
||||
currentOptionValues = new List<string>();
|
||||
}
|
||||
// Short name
|
||||
else if (argument.StartsWith('-'))
|
||||
{
|
||||
foreach (var alias in argument.Substring(1))
|
||||
{
|
||||
// Flush previous
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
|
||||
|
||||
currentOptionAlias = alias.AsString();
|
||||
currentOptionValues = new List<string>();
|
||||
}
|
||||
}
|
||||
// Value
|
||||
else if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
{
|
||||
currentOptionValues.Add(argument);
|
||||
}
|
||||
}
|
||||
|
||||
// Flush last option
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static CommandInput Parse(IReadOnlyList<string> commandLineArguments, IReadOnlyList<string> availableCommandNames)
|
||||
{
|
||||
var availableCommandNamesSet = availableCommandNames.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var index = 0;
|
||||
|
||||
var directives = ParseDirectives(
|
||||
commandLineArguments,
|
||||
ref index
|
||||
);
|
||||
|
||||
var commandName = ParseCommandName(
|
||||
commandLineArguments,
|
||||
availableCommandNamesSet,
|
||||
ref index
|
||||
);
|
||||
|
||||
var parameters = ParseParameters(
|
||||
commandLineArguments,
|
||||
ref index
|
||||
);
|
||||
|
||||
var options = ParseOptions(
|
||||
commandLineArguments,
|
||||
ref index
|
||||
);
|
||||
|
||||
return new CommandInput(directives, commandName, parameters, options);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandInput
|
||||
{
|
||||
public static CommandInput Empty { get; } = new CommandInput(
|
||||
Array.Empty<CommandDirectiveInput>(),
|
||||
null,
|
||||
Array.Empty<CommandParameterInput>(),
|
||||
Array.Empty<CommandOptionInput>()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Linq;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx.Domain
|
||||
@@ -20,29 +20,15 @@ namespace CliFx.Domain
|
||||
Values = values;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GetRawAlias() => Alias switch
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
{Length: 0} => Alias,
|
||||
{Length: 1} => $"-{Alias}",
|
||||
_ => $"--{Alias}"
|
||||
};
|
||||
|
||||
buffer.Append(Alias.Length > 1 ? "--" : "-");
|
||||
buffer.Append(Alias);
|
||||
public string GetRawValues() => Values.Select(v => v.Quote()).JoinToString(" ");
|
||||
|
||||
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();
|
||||
}
|
||||
public override string ToString() => $"{GetRawAlias()} {GetRawValues()}";
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
@@ -13,16 +12,12 @@ namespace CliFx.Domain
|
||||
|
||||
public char? ShortName { get; }
|
||||
|
||||
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
|
||||
? Name
|
||||
: ShortName?.AsString()!;
|
||||
|
||||
public string? EnvironmentVariableName { get; }
|
||||
|
||||
public bool IsRequired { get; }
|
||||
|
||||
public CommandOptionSchema(
|
||||
PropertyInfo property,
|
||||
PropertyInfo? property,
|
||||
string? name,
|
||||
char? shortName,
|
||||
string? environmentVariableName,
|
||||
@@ -40,7 +35,7 @@ namespace CliFx.Domain
|
||||
!string.IsNullOrWhiteSpace(Name) &&
|
||||
string.Equals(Name, name, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public bool MatchesShortName(char shortName) =>
|
||||
public bool MatchesShortName(char? shortName) =>
|
||||
ShortName != null &&
|
||||
ShortName == shortName;
|
||||
|
||||
@@ -52,27 +47,35 @@ namespace CliFx.Domain
|
||||
!string.IsNullOrWhiteSpace(EnvironmentVariableName) &&
|
||||
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public override string ToString()
|
||||
public string GetUserFacingDisplayString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name))
|
||||
{
|
||||
buffer.Append("--");
|
||||
buffer.Append(Name);
|
||||
buffer
|
||||
.Append("--")
|
||||
.Append(Name);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name) && ShortName != null)
|
||||
{
|
||||
buffer.Append('|');
|
||||
}
|
||||
|
||||
if (ShortName != null)
|
||||
{
|
||||
buffer.Append('-');
|
||||
buffer.Append(ShortName);
|
||||
buffer
|
||||
.Append('-')
|
||||
.Append(ShortName);
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ('{GetUserFacingDisplayString()}')";
|
||||
|
||||
public override string ToString() => GetInternalDisplayString();
|
||||
}
|
||||
|
||||
internal partial class CommandOptionSchema
|
||||
@@ -83,9 +86,12 @@ namespace CliFx.Domain
|
||||
if (attribute == null)
|
||||
return null;
|
||||
|
||||
// The user may mistakenly specify dashes, thinking it's required, so trim them
|
||||
var name = attribute.Name?.TrimStart('-');
|
||||
|
||||
return new CommandOptionSchema(
|
||||
property,
|
||||
attribute.Name,
|
||||
name,
|
||||
attribute.ShortName,
|
||||
attribute.EnvironmentVariableName,
|
||||
attribute.IsRequired,
|
||||
@@ -97,9 +103,9 @@ namespace CliFx.Domain
|
||||
internal partial class CommandOptionSchema
|
||||
{
|
||||
public static CommandOptionSchema HelpOption { get; } =
|
||||
new CommandOptionSchema(null!, "help", 'h', null, false, "Shows help text.");
|
||||
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.");
|
||||
new CommandOptionSchema(null, "version", null, null, false, "Shows version information.");
|
||||
}
|
||||
}
|
||||
11
CliFx/Domain/CommandParameterInput.cs
Normal file
11
CliFx/Domain/CommandParameterInput.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal class CommandParameterInput
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public CommandParameterInput(string value) => Value = value;
|
||||
|
||||
public override string ToString() => Value;
|
||||
}
|
||||
}
|
||||
@@ -8,30 +8,30 @@ namespace CliFx.Domain
|
||||
{
|
||||
public int Order { get; }
|
||||
|
||||
public string? Name { get; }
|
||||
public string Name { get; }
|
||||
|
||||
public string DisplayName => !string.IsNullOrWhiteSpace(Name)
|
||||
? Name
|
||||
: Property.Name.ToLowerInvariant();
|
||||
|
||||
public CommandParameterSchema(PropertyInfo property, int order, string? name, string? description)
|
||||
public CommandParameterSchema(PropertyInfo? property, int order, string name, string? description)
|
||||
: base(property, description)
|
||||
{
|
||||
Order = order;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GetUserFacingDisplayString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
buffer
|
||||
.Append('<')
|
||||
.Append(DisplayName)
|
||||
.Append(Name)
|
||||
.Append('>');
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ([{Order}] {GetUserFacingDisplayString()})";
|
||||
|
||||
public override string ToString() => GetInternalDisplayString();
|
||||
}
|
||||
|
||||
internal partial class CommandParameterSchema
|
||||
@@ -42,10 +42,12 @@ namespace CliFx.Domain
|
||||
if (attribute == null)
|
||||
return null;
|
||||
|
||||
var name = attribute.Name ?? property.Name.ToLowerInvariant();
|
||||
|
||||
return new CommandParameterSchema(
|
||||
property,
|
||||
attribute.Order,
|
||||
attribute.Name,
|
||||
name,
|
||||
attribute.Description
|
||||
);
|
||||
}
|
||||
|
||||
@@ -24,6 +24,10 @@ namespace CliFx.Domain
|
||||
|
||||
public IReadOnlyList<CommandOptionSchema> Options { get; }
|
||||
|
||||
public bool IsHelpOptionAvailable => Options.Contains(CommandOptionSchema.HelpOption);
|
||||
|
||||
public bool IsVersionOptionAvailable => Options.Contains(CommandOptionSchema.VersionOption);
|
||||
|
||||
public CommandSchema(
|
||||
Type type,
|
||||
string? name,
|
||||
@@ -34,13 +38,42 @@ namespace CliFx.Domain
|
||||
Type = type;
|
||||
Name = name;
|
||||
Description = description;
|
||||
Options = options;
|
||||
Parameters = parameters;
|
||||
Options = options;
|
||||
}
|
||||
|
||||
public bool MatchesName(string? name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase);
|
||||
public bool MatchesName(string? name) =>
|
||||
!string.IsNullOrWhiteSpace(Name)
|
||||
? string.Equals(name, Name, StringComparison.OrdinalIgnoreCase)
|
||||
: string.IsNullOrWhiteSpace(name);
|
||||
|
||||
private void InjectParameters(ICommand command, IReadOnlyList<string> parameterInputs)
|
||||
public IEnumerable<CommandArgumentSchema> GetArguments()
|
||||
{
|
||||
foreach (var parameter in Parameters)
|
||||
yield return parameter;
|
||||
|
||||
foreach (var option in Options)
|
||||
yield return option;
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<CommandArgumentSchema, object?> GetArgumentValues(ICommand instance)
|
||||
{
|
||||
var result = new Dictionary<CommandArgumentSchema, object?>();
|
||||
|
||||
foreach (var argument in GetArguments())
|
||||
{
|
||||
// Skip built-in arguments
|
||||
if (argument.Property == null)
|
||||
continue;
|
||||
|
||||
var value = argument.Property.GetValue(instance);
|
||||
result[argument] = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void BindParameters(ICommand instance, IReadOnlyList<CommandParameterInput> parameterInputs)
|
||||
{
|
||||
// All inputs must be bound
|
||||
var remainingParameterInputs = parameterInputs.ToList();
|
||||
@@ -53,14 +86,14 @@ namespace CliFx.Domain
|
||||
|
||||
for (var i = 0; i < scalarParameters.Length; i++)
|
||||
{
|
||||
var scalarParameter = scalarParameters[i];
|
||||
var parameter = scalarParameters[i];
|
||||
|
||||
var scalarParameterInput = i < parameterInputs.Count
|
||||
var scalarInput = i < parameterInputs.Count
|
||||
? parameterInputs[i]
|
||||
: throw new CliFxException($"Missing value for parameter <{scalarParameter.DisplayName}>.");
|
||||
: throw CliFxException.ParameterNotSet(parameter);
|
||||
|
||||
scalarParameter.Inject(command, scalarParameterInput);
|
||||
remainingParameterInputs.Remove(scalarParameterInput);
|
||||
parameter.BindOn(instance, scalarInput.Value);
|
||||
remainingParameterInputs.Remove(scalarInput);
|
||||
}
|
||||
|
||||
// Non-scalar parameter (only one is allowed)
|
||||
@@ -70,23 +103,23 @@ namespace CliFx.Domain
|
||||
|
||||
if (nonScalarParameter != null)
|
||||
{
|
||||
var nonScalarParameterInputs = parameterInputs.Skip(scalarParameters.Length).ToArray();
|
||||
nonScalarParameter.Inject(command, nonScalarParameterInputs);
|
||||
// TODO: Should it verify that at least one value is passed?
|
||||
var nonScalarValues = parameterInputs
|
||||
.Skip(scalarParameters.Length)
|
||||
.Select(p => p.Value)
|
||||
.ToArray();
|
||||
|
||||
nonScalarParameter.BindOn(instance, nonScalarValues);
|
||||
remainingParameterInputs.Clear();
|
||||
}
|
||||
|
||||
// Ensure all inputs were bound
|
||||
if (remainingParameterInputs.Any())
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine("Unrecognized parameters provided:")
|
||||
.AppendBulletList(remainingParameterInputs)
|
||||
.ToString());
|
||||
}
|
||||
throw CliFxException.UnrecognizedParametersProvided(remainingParameterInputs);
|
||||
}
|
||||
|
||||
private void InjectOptions(
|
||||
ICommand command,
|
||||
private void BindOptions(
|
||||
ICommand instance,
|
||||
IReadOnlyList<CommandOptionInput> optionInputs,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
@@ -97,22 +130,20 @@ namespace CliFx.Domain
|
||||
var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();
|
||||
|
||||
// Environment variables
|
||||
foreach (var environmentVariable in environmentVariables)
|
||||
foreach (var (name, value) in environmentVariables)
|
||||
{
|
||||
var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(environmentVariable.Key));
|
||||
var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(name));
|
||||
if (option == null)
|
||||
continue;
|
||||
|
||||
if (option != null)
|
||||
{
|
||||
var values = option.IsScalar
|
||||
? new[] {environmentVariable.Value}
|
||||
: environmentVariable.Value.Split(Path.PathSeparator);
|
||||
? new[] {value}
|
||||
: value.Split(Path.PathSeparator);
|
||||
|
||||
option.Inject(command, values);
|
||||
option.BindOn(instance, values);
|
||||
unsetRequiredOptions.Remove(option);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: refactor this part? I wrote this while sick
|
||||
// Direct input
|
||||
foreach (var option in Options)
|
||||
{
|
||||
@@ -120,71 +151,59 @@ namespace CliFx.Domain
|
||||
.Where(i => option.MatchesNameOrShortName(i.Alias))
|
||||
.ToArray();
|
||||
|
||||
if (inputs.Any())
|
||||
{
|
||||
option.Inject(command, inputs.SelectMany(i => i.Values).ToArray());
|
||||
// Skip if the inputs weren't provided for this option
|
||||
if (!inputs.Any())
|
||||
continue;
|
||||
|
||||
foreach (var input in inputs)
|
||||
remainingOptionInputs.Remove(input);
|
||||
var inputValues = inputs.SelectMany(i => i.Values).ToArray();
|
||||
option.BindOn(instance, inputValues);
|
||||
|
||||
remainingOptionInputs.RemoveRange(inputs);
|
||||
|
||||
// Required option implies that the value has to be set and also be non-empty
|
||||
if (inputValues.Any())
|
||||
unsetRequiredOptions.Remove(option);
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure all required options were set
|
||||
if (unsetRequiredOptions.Any())
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine("Missing values for some of the required options:")
|
||||
.AppendBulletList(unsetRequiredOptions.Select(o => o.DisplayName))
|
||||
.ToString());
|
||||
}
|
||||
|
||||
// Ensure all inputs were bound
|
||||
if (remainingOptionInputs.Any())
|
||||
throw CliFxException.UnrecognizedOptionsProvided(remainingOptionInputs);
|
||||
|
||||
// Ensure all required options were set
|
||||
if (unsetRequiredOptions.Any())
|
||||
throw CliFxException.RequiredOptionsNotSet(unsetRequiredOptions);
|
||||
}
|
||||
|
||||
public void Bind(
|
||||
ICommand instance,
|
||||
CommandInput input,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
throw new CliFxException(new StringBuilder()
|
||||
.AppendLine("Unrecognized options provided:")
|
||||
.AppendBulletList(remainingOptionInputs.Select(o => o.Alias).Distinct())
|
||||
.ToString());
|
||||
}
|
||||
BindParameters(instance, input.Parameters);
|
||||
BindOptions(instance, input.Options, environmentVariables);
|
||||
}
|
||||
|
||||
public ICommand CreateInstance(
|
||||
IReadOnlyList<string> parameterInputs,
|
||||
IReadOnlyList<CommandOptionInput> optionInputs,
|
||||
IReadOnlyDictionary<string, string> environmentVariables,
|
||||
ITypeActivator activator)
|
||||
{
|
||||
var command = (ICommand) activator.CreateInstance(Type);
|
||||
|
||||
InjectParameters(command, parameterInputs);
|
||||
InjectOptions(command, optionInputs, environmentVariables);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GetInternalDisplayString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name))
|
||||
buffer.Append(Name);
|
||||
// Type
|
||||
buffer.Append(Type.FullName);
|
||||
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
buffer.Append(parameter);
|
||||
}
|
||||
|
||||
foreach (var option in Options)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
buffer.Append(option);
|
||||
}
|
||||
// Name
|
||||
buffer
|
||||
.Append(' ')
|
||||
.Append('(')
|
||||
.Append(IsDefault
|
||||
? "<default command>"
|
||||
: $"'{Name}'"
|
||||
)
|
||||
.Append(')');
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
public override string ToString() => GetInternalDisplayString();
|
||||
}
|
||||
|
||||
internal partial class CommandSchema
|
||||
@@ -202,6 +221,12 @@ namespace CliFx.Domain
|
||||
|
||||
var attribute = type.GetCustomAttribute<CommandAttribute>();
|
||||
|
||||
var name = attribute?.Name;
|
||||
|
||||
var builtInOptions = string.IsNullOrWhiteSpace(name)
|
||||
? new[] {CommandOptionSchema.HelpOption, CommandOptionSchema.VersionOption}
|
||||
: new[] {CommandOptionSchema.HelpOption};
|
||||
|
||||
var parameters = type.GetProperties()
|
||||
.Select(CommandParameterSchema.TryResolve)
|
||||
.Where(p => p != null)
|
||||
@@ -210,21 +235,16 @@ namespace CliFx.Domain
|
||||
var options = type.GetProperties()
|
||||
.Select(CommandOptionSchema.TryResolve)
|
||||
.Where(o => o != null)
|
||||
.Concat(builtInOptions)
|
||||
.ToArray();
|
||||
|
||||
return new CommandSchema(
|
||||
type,
|
||||
attribute?.Name,
|
||||
name,
|
||||
attribute?.Description,
|
||||
parameters,
|
||||
options
|
||||
parameters!,
|
||||
options!
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandSchema
|
||||
{
|
||||
public static CommandSchema StubDefaultCommand { get; } =
|
||||
new CommandSchema(null!, null, null, new CommandParameterSchema[0], new CommandOptionSchema[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal class CommandUnboundArgumentInput
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public CommandUnboundArgumentInput(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override string ToString() => Value;
|
||||
}
|
||||
}
|
||||
@@ -1,332 +1,388 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal class HelpTextWriter
|
||||
internal partial class HelpTextWriter
|
||||
{
|
||||
private readonly ApplicationMetadata _metadata;
|
||||
private readonly IConsole _console;
|
||||
|
||||
private int _column;
|
||||
private int _row;
|
||||
|
||||
private bool IsEmpty => _column == 0 && _row == 0;
|
||||
|
||||
public HelpTextWriter(ApplicationMetadata metadata, IConsole console)
|
||||
{
|
||||
_metadata = metadata;
|
||||
_console = console;
|
||||
}
|
||||
|
||||
public void Write(ApplicationSchema applicationSchema, CommandSchema command)
|
||||
private void Write(char value)
|
||||
{
|
||||
var column = 0;
|
||||
var row = 0;
|
||||
|
||||
var childCommands = applicationSchema.GetChildCommands(command.Name);
|
||||
|
||||
bool IsEmpty() => column == 0 && row == 0;
|
||||
|
||||
void Render(string text)
|
||||
{
|
||||
_console.Output.Write(text);
|
||||
|
||||
column += text.Length;
|
||||
_console.Output.Write(value);
|
||||
_column++;
|
||||
}
|
||||
|
||||
void RenderNewLine()
|
||||
private void Write(string value)
|
||||
{
|
||||
_console.Output.Write(value);
|
||||
_column += value.Length;
|
||||
}
|
||||
|
||||
private void Write(ConsoleColor foregroundColor, string value)
|
||||
{
|
||||
_console.WithForegroundColor(foregroundColor, () => Write(value));
|
||||
}
|
||||
|
||||
private void WriteLine()
|
||||
{
|
||||
_console.Output.WriteLine();
|
||||
|
||||
column = 0;
|
||||
row++;
|
||||
_column = 0;
|
||||
_row++;
|
||||
}
|
||||
|
||||
void RenderMargin(int lines = 1)
|
||||
private void WriteVerticalMargin(int size = 1)
|
||||
{
|
||||
if (!IsEmpty())
|
||||
{
|
||||
for (var i = 0; i < lines; i++)
|
||||
RenderNewLine();
|
||||
}
|
||||
for (var i = 0; i < size; i++)
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
void RenderIndent(int spaces = 2)
|
||||
private void WriteHorizontalMargin(int size = 2)
|
||||
{
|
||||
Render(' '.Repeat(spaces));
|
||||
for (var i = 0; i < size; i++)
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
void RenderColumnIndent(int spaces = 20, int margin = 2)
|
||||
private void WriteColumnMargin(int columnSize = 20, int offsetSize = 2)
|
||||
{
|
||||
if (column + margin < spaces)
|
||||
{
|
||||
RenderIndent(spaces - column);
|
||||
}
|
||||
if (_column + offsetSize < columnSize)
|
||||
WriteHorizontalMargin(columnSize - _column);
|
||||
else
|
||||
{
|
||||
Render(" ");
|
||||
}
|
||||
WriteHorizontalMargin(offsetSize);
|
||||
}
|
||||
|
||||
void RenderWithColor(string text, ConsoleColor foregroundColor)
|
||||
private void WriteHeader(string text)
|
||||
{
|
||||
_console.WithForegroundColor(foregroundColor, () => Render(text));
|
||||
Write(ConsoleColor.Magenta, text);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
void RenderHeader(string text)
|
||||
private void WriteApplicationInfo()
|
||||
{
|
||||
RenderWithColor(text, ConsoleColor.Magenta);
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
void RenderApplicationInfo()
|
||||
{
|
||||
if (!command.IsDefault)
|
||||
return;
|
||||
|
||||
// Title and version
|
||||
RenderWithColor(_metadata.Title, ConsoleColor.Yellow);
|
||||
Render(" ");
|
||||
RenderWithColor(_metadata.VersionText, ConsoleColor.Yellow);
|
||||
RenderNewLine();
|
||||
Write(ConsoleColor.Yellow, _metadata.Title);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Yellow, _metadata.VersionText);
|
||||
WriteLine();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(_metadata.Description))
|
||||
{
|
||||
Render(_metadata.Description);
|
||||
RenderNewLine();
|
||||
WriteHorizontalMargin();
|
||||
Write(_metadata.Description);
|
||||
WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderDescription()
|
||||
private void WriteCommandDescription(CommandSchema command)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Description))
|
||||
return;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Description");
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
RenderIndent();
|
||||
Render(command.Description);
|
||||
RenderNewLine();
|
||||
WriteHeader("Description");
|
||||
|
||||
WriteHorizontalMargin();
|
||||
Write(command.Description);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
void RenderUsage()
|
||||
private void WriteCommandUsage(CommandSchema command, IReadOnlyList<CommandSchema> childCommands)
|
||||
{
|
||||
RenderMargin();
|
||||
RenderHeader("Usage");
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
WriteHeader("Usage");
|
||||
|
||||
// Exe name
|
||||
RenderIndent();
|
||||
Render(_metadata.ExecutableName);
|
||||
WriteHorizontalMargin();
|
||||
Write(_metadata.ExecutableName);
|
||||
|
||||
// Command name
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor(command.Name, ConsoleColor.Cyan);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, command.Name);
|
||||
}
|
||||
|
||||
// Child command placeholder
|
||||
if (childCommands.Any())
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor("[command]", ConsoleColor.Cyan);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, "[command]");
|
||||
}
|
||||
|
||||
// Parameters
|
||||
foreach (var parameter in command.Parameters)
|
||||
{
|
||||
Render(" ");
|
||||
Render(parameter.IsScalar
|
||||
? $"<{parameter.DisplayName}>"
|
||||
: $"<{parameter.DisplayName}...>");
|
||||
Write(' ');
|
||||
Write(parameter.IsScalar
|
||||
? $"<{parameter.Name}>"
|
||||
: $"<{parameter.Name}...>"
|
||||
);
|
||||
}
|
||||
|
||||
// Required options
|
||||
var requiredOptionSchemas = command.Options
|
||||
.Where(o => o.IsRequired)
|
||||
.ToArray();
|
||||
foreach (var option in command.Options.Where(o => o.IsRequired))
|
||||
{
|
||||
Write(' ');
|
||||
Write(ConsoleColor.White, !string.IsNullOrWhiteSpace(option.Name)
|
||||
? $"--{option.Name}"
|
||||
: $"-{option.ShortName}"
|
||||
);
|
||||
|
||||
foreach (var option in requiredOptionSchemas)
|
||||
{
|
||||
Render(" ");
|
||||
if (!string.IsNullOrWhiteSpace(option.Name))
|
||||
{
|
||||
RenderWithColor($"--{option.Name}", ConsoleColor.White);
|
||||
Render(" ");
|
||||
Render(option.IsScalar
|
||||
Write(' ');
|
||||
Write(option.IsScalar
|
||||
? "<value>"
|
||||
: "<values...>");
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderWithColor($"-{option.ShortName}", ConsoleColor.White);
|
||||
Render(" ");
|
||||
Render(option.IsScalar
|
||||
? "<value>"
|
||||
: "<values...>");
|
||||
}
|
||||
: "<values...>"
|
||||
);
|
||||
}
|
||||
|
||||
// Options placeholder
|
||||
if (command.Options.Count != requiredOptionSchemas.Length)
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor("[options]", ConsoleColor.White);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.White, "[options]");
|
||||
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
void RenderParameters()
|
||||
private void WriteCommandParameters(CommandSchema command)
|
||||
{
|
||||
if (!command.Parameters.Any())
|
||||
return;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Parameters");
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
var parameters = command.Parameters
|
||||
.OrderBy(p => p.Order)
|
||||
.ToArray();
|
||||
WriteHeader("Parameters");
|
||||
|
||||
foreach (var parameter in parameters)
|
||||
foreach (var parameter in command.Parameters.OrderBy(p => p.Order))
|
||||
{
|
||||
RenderWithColor("* ", ConsoleColor.Red);
|
||||
RenderWithColor($"{parameter.DisplayName}", ConsoleColor.White);
|
||||
Write(ConsoleColor.Red, "* ");
|
||||
Write(ConsoleColor.White, $"{parameter.Name}");
|
||||
|
||||
RenderColumnIndent();
|
||||
WriteColumnMargin();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(parameter.Description))
|
||||
{
|
||||
Render(parameter.Description);
|
||||
Write(parameter.Description);
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderOptions()
|
||||
// Valid values
|
||||
var validValues = parameter.GetValidValues();
|
||||
if (validValues.Any())
|
||||
{
|
||||
RenderMargin();
|
||||
RenderHeader("Options");
|
||||
Write($"Valid values: {FormatValidValues(validValues)}.");
|
||||
}
|
||||
|
||||
var options = command.Options
|
||||
.OrderByDescending(o => o.IsRequired)
|
||||
.ToList();
|
||||
WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
// Add built-in options
|
||||
options.Add(CommandOptionSchema.HelpOption);
|
||||
if (command.IsDefault)
|
||||
options.Add(CommandOptionSchema.VersionOption);
|
||||
private void WriteCommandOptions(
|
||||
CommandSchema command,
|
||||
IReadOnlyDictionary<CommandArgumentSchema, object?> argumentDefaultValues)
|
||||
{
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
foreach (var option in options)
|
||||
WriteHeader("Options");
|
||||
|
||||
foreach (var option in command.Options.OrderByDescending(o => o.IsRequired))
|
||||
{
|
||||
if (option.IsRequired)
|
||||
{
|
||||
RenderWithColor("* ", ConsoleColor.Red);
|
||||
Write(ConsoleColor.Red, "* ");
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderIndent();
|
||||
WriteHorizontalMargin();
|
||||
}
|
||||
|
||||
// Short name
|
||||
if (option.ShortName != null)
|
||||
{
|
||||
RenderWithColor($"-{option.ShortName}", ConsoleColor.White);
|
||||
Write(ConsoleColor.White, $"-{option.ShortName}");
|
||||
}
|
||||
|
||||
// Delimiter
|
||||
// Separator
|
||||
if (!string.IsNullOrWhiteSpace(option.Name) && option.ShortName != null)
|
||||
{
|
||||
Render("|");
|
||||
Write('|');
|
||||
}
|
||||
|
||||
// Name
|
||||
if (!string.IsNullOrWhiteSpace(option.Name))
|
||||
{
|
||||
RenderWithColor($"--{option.Name}", ConsoleColor.White);
|
||||
Write(ConsoleColor.White, $"--{option.Name}");
|
||||
}
|
||||
|
||||
RenderColumnIndent();
|
||||
WriteColumnMargin();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(option.Description))
|
||||
{
|
||||
Render(option.Description);
|
||||
Render(" ");
|
||||
Write(option.Description);
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
// Valid values
|
||||
var validValues = option.GetValidValues();
|
||||
if (validValues.Any())
|
||||
{
|
||||
Write($"Valid values: {FormatValidValues(validValues)}.");
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
// Environment variable
|
||||
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
|
||||
{
|
||||
Render($"(Environment variable: {option.EnvironmentVariableName}).");
|
||||
Render(" ");
|
||||
Write($"Environment variable: \"{option.EnvironmentVariableName}\".");
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
// Default value
|
||||
if (!option.IsRequired)
|
||||
{
|
||||
var defaultValue = argumentDefaultValues.GetValueOrDefault(option);
|
||||
var defaultValueFormatted = FormatDefaultValue(defaultValue);
|
||||
if (defaultValueFormatted != null)
|
||||
{
|
||||
Write($"Default: {defaultValueFormatted}.");
|
||||
}
|
||||
}
|
||||
|
||||
void RenderChildCommands()
|
||||
WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteCommandChildren(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandSchema> childCommands)
|
||||
{
|
||||
if (!childCommands.Any())
|
||||
return;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Commands");
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
WriteHeader("Commands");
|
||||
|
||||
foreach (var childCommand in childCommands)
|
||||
{
|
||||
var relativeCommandName =
|
||||
string.IsNullOrWhiteSpace(childCommand.Name) || string.IsNullOrWhiteSpace(command.Name)
|
||||
? childCommand.Name
|
||||
: childCommand.Name.Substring(command.Name.Length + 1);
|
||||
var relativeCommandName = !string.IsNullOrWhiteSpace(command.Name)
|
||||
? childCommand.Name!.Substring(command.Name.Length).Trim()
|
||||
: childCommand.Name!;
|
||||
|
||||
// Name
|
||||
RenderIndent();
|
||||
RenderWithColor(relativeCommandName, ConsoleColor.Cyan);
|
||||
WriteHorizontalMargin();
|
||||
Write(ConsoleColor.Cyan, relativeCommandName);
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(childCommand.Description))
|
||||
{
|
||||
RenderColumnIndent();
|
||||
Render(childCommand.Description);
|
||||
WriteColumnMargin();
|
||||
Write(childCommand.Description);
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
RenderMargin();
|
||||
|
||||
// Child command help tip
|
||||
Render("You can run `");
|
||||
Render(_metadata.ExecutableName);
|
||||
WriteVerticalMargin();
|
||||
Write("You can run `");
|
||||
Write(_metadata.ExecutableName);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor(command.Name, ConsoleColor.Cyan);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, command.Name);
|
||||
}
|
||||
|
||||
Render(" ");
|
||||
RenderWithColor("[command]", ConsoleColor.Cyan);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, "[command]");
|
||||
|
||||
Render(" ");
|
||||
RenderWithColor("--help", ConsoleColor.White);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.White, "--help");
|
||||
|
||||
Render("` to show help on a specific command.");
|
||||
Write("` to show help on a specific command.");
|
||||
|
||||
RenderNewLine();
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
public void Write(
|
||||
RootSchema root,
|
||||
CommandSchema command,
|
||||
IReadOnlyDictionary<CommandArgumentSchema, object?> defaultValues)
|
||||
{
|
||||
var childCommands = root.GetChildCommands(command.Name);
|
||||
|
||||
_console.ResetColor();
|
||||
RenderApplicationInfo();
|
||||
RenderDescription();
|
||||
RenderUsage();
|
||||
RenderParameters();
|
||||
RenderOptions();
|
||||
RenderChildCommands();
|
||||
|
||||
if (command.IsDefault)
|
||||
WriteApplicationInfo();
|
||||
|
||||
WriteCommandDescription(command);
|
||||
WriteCommandUsage(command, childCommands);
|
||||
WriteCommandParameters(command);
|
||||
WriteCommandOptions(command, defaultValues);
|
||||
WriteCommandChildren(command, childCommands);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class HelpTextWriter
|
||||
{
|
||||
private static string FormatValidValues(IReadOnlyList<string> values) =>
|
||||
values.Select(v => v.Quote()).JoinToString(", ");
|
||||
|
||||
private static string? FormatDefaultValue(object? defaultValue)
|
||||
{
|
||||
if (defaultValue == null)
|
||||
return null;
|
||||
|
||||
// Enumerable
|
||||
if (!(defaultValue is string) && defaultValue is IEnumerable defaultValues)
|
||||
{
|
||||
var elementType = defaultValues.GetType().GetEnumerableUnderlyingType() ?? typeof(object);
|
||||
|
||||
// If the ToString() method is not overriden, the default value can't be formatted nicely
|
||||
if (!elementType.IsToStringOverriden())
|
||||
return null;
|
||||
|
||||
return defaultValues
|
||||
.Cast<object?>()
|
||||
.Where(o => o != null)
|
||||
.Select(o => o!.ToFormattableString(CultureInfo.InvariantCulture).Quote())
|
||||
.JoinToString(" ");
|
||||
}
|
||||
// Non-enumerable
|
||||
else
|
||||
{
|
||||
// If the ToString() method is not overriden, the default value can't be formatted nicely
|
||||
if (!defaultValue.GetType().IsToStringOverriden())
|
||||
return null;
|
||||
|
||||
return defaultValue.ToFormattableString(CultureInfo.InvariantCulture).Quote();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
232
CliFx/Domain/RootSchema.cs
Normal file
232
CliFx/Domain/RootSchema.cs
Normal file
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal partial class RootSchema
|
||||
{
|
||||
public IReadOnlyList<CommandSchema> Commands { get; }
|
||||
|
||||
public RootSchema(IReadOnlyList<CommandSchema> commands)
|
||||
{
|
||||
Commands = commands;
|
||||
}
|
||||
|
||||
public IReadOnlyList<string> GetCommandNames() => Commands
|
||||
.Select(c => c.Name)
|
||||
.Where(n => !string.IsNullOrWhiteSpace(n))
|
||||
.ToArray()!;
|
||||
|
||||
public CommandSchema? TryFindDefaultCommand() =>
|
||||
Commands.FirstOrDefault(c => c.IsDefault);
|
||||
|
||||
public CommandSchema? TryFindCommand(string? commandName) =>
|
||||
Commands.FirstOrDefault(c => c.MatchesName(commandName));
|
||||
|
||||
private IReadOnlyList<CommandSchema> GetDescendantCommands(
|
||||
IReadOnlyList<CommandSchema> potentialParentCommands,
|
||||
string? parentCommandName) =>
|
||||
potentialParentCommands
|
||||
// Default commands can't be children of anything
|
||||
.Where(c => !string.IsNullOrWhiteSpace(c.Name))
|
||||
// Command can't be its own child
|
||||
.Where(c => !c.MatchesName(parentCommandName))
|
||||
.Where(c =>
|
||||
string.IsNullOrWhiteSpace(parentCommandName) ||
|
||||
c.Name!.StartsWith(parentCommandName + ' ', StringComparison.OrdinalIgnoreCase))
|
||||
.ToArray();
|
||||
|
||||
public IReadOnlyList<CommandSchema> GetDescendantCommands(string? parentCommandName) =>
|
||||
GetDescendantCommands(Commands, parentCommandName);
|
||||
|
||||
public IReadOnlyList<CommandSchema> GetChildCommands(string? parentCommandName)
|
||||
{
|
||||
var descendants = GetDescendantCommands(parentCommandName);
|
||||
|
||||
// Filter out descendants of descendants, leave only children
|
||||
var result = new List<CommandSchema>(descendants);
|
||||
|
||||
foreach (var descendant in descendants)
|
||||
{
|
||||
var descendantDescendants = GetDescendantCommands(descendants, descendant.Name);
|
||||
result.RemoveRange(descendantDescendants);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class RootSchema
|
||||
{
|
||||
private static void ValidateParameters(CommandSchema command)
|
||||
{
|
||||
var duplicateOrderGroup = command.Parameters
|
||||
.GroupBy(a => a.Order)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateOrderGroup != null)
|
||||
{
|
||||
throw CliFxException.ParametersWithSameOrder(
|
||||
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.ParametersWithSameName(
|
||||
command,
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
var nonScalarParameters = command.Parameters
|
||||
.Where(p => !p.IsScalar)
|
||||
.ToArray();
|
||||
|
||||
if (nonScalarParameters.Length > 1)
|
||||
{
|
||||
throw CliFxException.TooManyNonScalarParameters(
|
||||
command,
|
||||
nonScalarParameters
|
||||
);
|
||||
}
|
||||
|
||||
var nonLastNonScalarParameter = command.Parameters
|
||||
.OrderByDescending(a => a.Order)
|
||||
.Skip(1)
|
||||
.LastOrDefault(p => !p.IsScalar);
|
||||
|
||||
if (nonLastNonScalarParameter != null)
|
||||
{
|
||||
throw CliFxException.NonLastNonScalarParameter(
|
||||
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.OptionsWithNoName(
|
||||
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.OptionsWithInvalidLengthName(
|
||||
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.OptionsWithSameName(
|
||||
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.OptionsWithSameShortName(
|
||||
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.OptionsWithSameEnvironmentVariableName(
|
||||
command,
|
||||
duplicateEnvironmentVariableNameGroup.Key,
|
||||
duplicateEnvironmentVariableNameGroup.ToArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
||||
{
|
||||
if (!commands.Any())
|
||||
{
|
||||
throw CliFxException.NoCommandsDefined();
|
||||
}
|
||||
|
||||
var duplicateNameGroup = commands
|
||||
.GroupBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
throw !string.IsNullOrWhiteSpace(duplicateNameGroup.Key)
|
||||
? CliFxException.CommandsWithSameName(
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray()
|
||||
)
|
||||
: CliFxException.TooManyDefaultCommands(duplicateNameGroup.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public static RootSchema 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 RootSchema(commands);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,393 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx.Exceptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Domain exception thrown within CliFx.
|
||||
/// </summary>
|
||||
public class CliFxException : Exception
|
||||
public partial class CliFxException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CliFxException"/>.
|
||||
/// </summary>
|
||||
public CliFxException(string? message)
|
||||
: base(message)
|
||||
{
|
||||
}
|
||||
private readonly bool _isMessageSet;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CliFxException"/>.
|
||||
/// </summary>
|
||||
public CliFxException(string? message, Exception? innerException)
|
||||
public CliFxException(string? message, Exception? innerException = null)
|
||||
: base(message, innerException)
|
||||
{
|
||||
// Message property has a fallback so it's never empty, hence why we need this check
|
||||
_isMessageSet = !string.IsNullOrWhiteSpace(message);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => _isMessageSet
|
||||
? Message
|
||||
: base.ToString();
|
||||
}
|
||||
|
||||
// Internal exceptions
|
||||
// Provide more diagnostic information here
|
||||
public partial class CliFxException
|
||||
{
|
||||
internal static CliFxException DefaultActivatorFailed(Type type, Exception? innerException = null)
|
||||
{
|
||||
var configureActivatorMethodName = $"{nameof(CliApplicationBuilder)}.{nameof(CliApplicationBuilder.UseTypeActivator)}(...)";
|
||||
|
||||
var message = $@"
|
||||
Failed to create an instance of type '{type.FullName}'.
|
||||
The type must have a public parameterless constructor in order to be instantiated by the default activator.
|
||||
|
||||
To fix this, either make sure this type has a public parameterless constructor, or configure a custom activator using {configureActivatorMethodName}.
|
||||
Refer to the readme to learn how to integrate a dependency container of your choice to act as a type activator.";
|
||||
|
||||
return new CliFxException(message.Trim(), innerException);
|
||||
}
|
||||
|
||||
internal static CliFxException DelegateActivatorReturnedNull(Type type)
|
||||
{
|
||||
var message = $@"
|
||||
Failed to create an instance of type '{type.FullName}', received <null> instead.
|
||||
|
||||
To fix this, ensure that the provided type activator was configured correctly, as it's not expected to return <null>.
|
||||
If you are using a dependency container, this error may signify that the type wasn't registered.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException InvalidCommandType(Type type)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{type.FullName}' is not a valid command type.
|
||||
|
||||
In order to be a valid command type, it must:
|
||||
- Not be an abstract class
|
||||
- Implement {typeof(ICommand).FullName}
|
||||
- Be annotated with {typeof(CommandAttribute).FullName}
|
||||
|
||||
If you're experiencing problems, please refer to the readme for a quickstart example.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException NoCommandsDefined()
|
||||
{
|
||||
var message = $@"
|
||||
There are no commands configured in the application.
|
||||
|
||||
To fix this, ensure that at least one command is added through one of the methods on {nameof(CliApplicationBuilder)}.
|
||||
If you're experiencing problems, please refer to the readme for a quickstart example.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException TooManyDefaultCommands(IReadOnlyList<CommandSchema> invalidCommands)
|
||||
{
|
||||
var message = $@"
|
||||
Application configuration is invalid because there are {invalidCommands.Count} default commands:
|
||||
{invalidCommands.JoinToString(Environment.NewLine)}
|
||||
|
||||
There can only be one default command (i.e. command with no name) in an application.
|
||||
Other commands must have unique non-empty names that identify them.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandsWithSameName(
|
||||
string name,
|
||||
IReadOnlyList<CommandSchema> invalidCommands)
|
||||
{
|
||||
var message = $@"
|
||||
Application configuration is invalid because there are {invalidCommands.Count} commands with the same name ('{name}'):
|
||||
{invalidCommands.JoinToString(Environment.NewLine)}
|
||||
|
||||
Commands must have unique names.
|
||||
Names are not case-sensitive.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException ParametersWithSameOrder(
|
||||
CommandSchema command,
|
||||
int order,
|
||||
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same order ({order}):
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Parameters must have unique order.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException ParametersWithSameName(
|
||||
CommandSchema command,
|
||||
string name,
|
||||
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same name ('{name}'):
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Parameters must have unique names to avoid potential confusion in the help text.
|
||||
Names are not case-sensitive.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException TooManyNonScalarParameters(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} non-scalar parameters:
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Non-scalar parameter is such that is bound from more than one value (e.g. array).
|
||||
Only one parameter in a command may be non-scalar and it must be the last one in order.
|
||||
|
||||
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException NonLastNonScalarParameter(
|
||||
CommandSchema command,
|
||||
CommandParameterSchema invalidParameter)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains a non-scalar parameter which is not the last in order:
|
||||
{invalidParameter}
|
||||
|
||||
Non-scalar parameter is such that is bound from more than one value (e.g. array).
|
||||
Only one parameter in a command may be non-scalar and it must be the last one in order.
|
||||
|
||||
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException OptionsWithNoName(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains one or more options without a name:
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options must have either a name or a short name or both.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException OptionsWithInvalidLengthName(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains one or more options whose names are too short:
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Option names must be at least 2 characters long to avoid confusion with short names.
|
||||
If you intended to set the short name instead, use the attribute overload that accepts a char.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException OptionsWithSameName(
|
||||
CommandSchema command,
|
||||
string name,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same name ('{name}'):
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options must have unique names.
|
||||
Names are not case-sensitive.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException OptionsWithSameShortName(
|
||||
CommandSchema command,
|
||||
char shortName,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same short name ('{shortName}'):
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options must have unique short names.
|
||||
Short names are case-sensitive (i.e. 'a' and 'A' are different short names).";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException OptionsWithSameEnvironmentVariableName(
|
||||
CommandSchema command,
|
||||
string environmentVariableName,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same fallback environment variable name ('{environmentVariableName}'):
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options cannot share the same environment variable as a fallback.
|
||||
Environment variable names are not case-sensitive.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
}
|
||||
|
||||
// End-user-facing exceptions
|
||||
// Avoid internal details and fix recommendations here
|
||||
public partial class CliFxException
|
||||
{
|
||||
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
|
||||
CommandParameterSchema parameter,
|
||||
IReadOnlyList<string> values)
|
||||
{
|
||||
var message = $@"
|
||||
Parameter {parameter.GetUserFacingDisplayString()} expects a single value, but provided with multiple:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
|
||||
CommandOptionSchema option,
|
||||
IReadOnlyList<string> values)
|
||||
{
|
||||
var message = $@"
|
||||
Option {option.GetUserFacingDisplayString()} expects a single value, but provided with multiple:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
|
||||
CommandArgumentSchema argument,
|
||||
IReadOnlyList<string> values) => argument switch
|
||||
{
|
||||
CommandParameterSchema parameter => CannotConvertMultipleValuesToNonScalar(parameter, values),
|
||||
CommandOptionSchema option => CannotConvertMultipleValuesToNonScalar(option, values),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(argument))
|
||||
};
|
||||
|
||||
internal static CliFxException CannotConvertToType(
|
||||
CommandParameterSchema parameter,
|
||||
string? value,
|
||||
Type type,
|
||||
Exception? innerException = null)
|
||||
{
|
||||
var message = $@"
|
||||
Can't convert value ""{value ?? "<null>"}"" to type '{type.Name}' for parameter {parameter.GetUserFacingDisplayString()}.
|
||||
{innerException?.Message ?? "This type is not supported."}";
|
||||
|
||||
return new CliFxException(message.Trim(), innerException);
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertToType(
|
||||
CommandOptionSchema option,
|
||||
string? value,
|
||||
Type type,
|
||||
Exception? innerException = null)
|
||||
{
|
||||
var message = $@"
|
||||
Can't convert value ""{value ?? "<null>"}"" to type '{type.Name}' for option {option.GetUserFacingDisplayString()}.
|
||||
{innerException?.Message ?? "This type is not supported."}";
|
||||
|
||||
return new CliFxException(message.Trim(), innerException);
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertToType(
|
||||
CommandArgumentSchema argument,
|
||||
string? value,
|
||||
Type type,
|
||||
Exception? innerException = null) => argument switch
|
||||
{
|
||||
CommandParameterSchema parameter => CannotConvertToType(parameter, value, type, innerException),
|
||||
CommandOptionSchema option => CannotConvertToType(option, value, type, innerException),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(argument))
|
||||
};
|
||||
|
||||
internal static CliFxException CannotConvertNonScalar(
|
||||
CommandParameterSchema parameter,
|
||||
IReadOnlyList<string> values,
|
||||
Type type)
|
||||
{
|
||||
var message = $@"
|
||||
Can't convert provided values to type '{type.Name}' for parameter {parameter.GetUserFacingDisplayString()}:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}
|
||||
|
||||
Target type is not assignable from array and doesn't have a public constructor that takes an array.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertNonScalar(
|
||||
CommandOptionSchema option,
|
||||
IReadOnlyList<string> values,
|
||||
Type type)
|
||||
{
|
||||
var message = $@"
|
||||
Can't convert provided values to type '{type.Name}' for option {option.GetUserFacingDisplayString()}:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}
|
||||
|
||||
Target type is not assignable from array and doesn't have a public constructor that takes an array.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertNonScalar(
|
||||
CommandArgumentSchema argument,
|
||||
IReadOnlyList<string> values,
|
||||
Type type) => argument switch
|
||||
{
|
||||
CommandParameterSchema parameter => CannotConvertNonScalar(parameter, values, type),
|
||||
CommandOptionSchema option => CannotConvertNonScalar(option, values, type),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(argument))
|
||||
};
|
||||
|
||||
internal static CliFxException ParameterNotSet(CommandParameterSchema parameter)
|
||||
{
|
||||
var message = $@"
|
||||
Missing value for parameter {parameter.GetUserFacingDisplayString()}.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException RequiredOptionsNotSet(IReadOnlyList<CommandOptionSchema> options)
|
||||
{
|
||||
var message = $@"
|
||||
Missing values for one or more required options:
|
||||
{options.Select(o => o.GetUserFacingDisplayString()).JoinToString(Environment.NewLine)}";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException UnrecognizedParametersProvided(IReadOnlyList<CommandParameterInput> parameterInputs)
|
||||
{
|
||||
var message = $@"
|
||||
Unrecognized parameters provided:
|
||||
{parameterInputs.Select(p => p.Value).JoinToString(Environment.NewLine)}";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException UnrecognizedOptionsProvided(IReadOnlyList<CommandOptionInput> optionInputs)
|
||||
{
|
||||
var message = $@"
|
||||
Unrecognized options provided:
|
||||
{optionInputs.Select(o => o.GetRawAlias()).JoinToString(Environment.NewLine)}";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,43 +4,57 @@ namespace CliFx.Exceptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Thrown when a command cannot proceed with normal execution due to an error.
|
||||
/// Use this exception if you want to report an error that occured during execution of a command.
|
||||
/// Use this exception if you want to report an error that occured during the execution of a command.
|
||||
/// This exception also allows specifying exit code which will be returned to the calling process.
|
||||
/// </summary>
|
||||
public class CommandException : Exception
|
||||
{
|
||||
private const int DefaultExitCode = -100;
|
||||
private const int DefaultExitCode = -1;
|
||||
|
||||
private readonly bool _isMessageSet;
|
||||
|
||||
/// <summary>
|
||||
/// Process exit code.
|
||||
/// Returns an exit code associated with this exception.
|
||||
/// </summary>
|
||||
public int ExitCode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show the help text after handling this exception.
|
||||
/// </summary>
|
||||
public bool ShowHelp { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CommandException"/>.
|
||||
/// </summary>
|
||||
public CommandException(string? message, Exception? innerException, int exitCode = DefaultExitCode)
|
||||
public CommandException(string? message, Exception? innerException, int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
: base(message, innerException)
|
||||
{
|
||||
ExitCode = exitCode != 0
|
||||
? exitCode
|
||||
: throw new ArgumentException("Exit code must not be zero in order to signify failure.");
|
||||
ExitCode = exitCode;
|
||||
ShowHelp = showHelp;
|
||||
|
||||
// Message property has a fallback so it's never empty, hence why we need this check
|
||||
_isMessageSet = !string.IsNullOrWhiteSpace(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CommandException"/>.
|
||||
/// </summary>
|
||||
public CommandException(string? message, int exitCode = DefaultExitCode)
|
||||
: this(message, null, exitCode)
|
||||
public CommandException(string? message, int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
: this(message, null, exitCode, showHelp)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CommandException"/>.
|
||||
/// </summary>
|
||||
public CommandException(int exitCode = DefaultExitCode)
|
||||
: this(null, exitCode)
|
||||
public CommandException(int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
: this(null, exitCode, showHelp)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => _isMessageSet
|
||||
? Message
|
||||
: base.ToString();
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="CliFx"/>
|
||||
/// </summary>
|
||||
public static class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
|
||||
/// </summary>
|
||||
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
|
||||
{
|
||||
var lastColor = console.ForegroundColor;
|
||||
console.ForegroundColor = foregroundColor;
|
||||
|
||||
action();
|
||||
|
||||
console.ForegroundColor = lastColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets console background color, executes specified action, and sets the color back to the original value.
|
||||
/// </summary>
|
||||
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
|
||||
{
|
||||
var lastColor = console.BackgroundColor;
|
||||
console.BackgroundColor = backgroundColor;
|
||||
|
||||
action();
|
||||
|
||||
console.BackgroundColor = lastColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets console foreground and background colors, executes specified action, and sets the colors back to the original values.
|
||||
/// </summary>
|
||||
public static void WithColors(this IConsole console, ConsoleColor foregroundColor, ConsoleColor backgroundColor, Action action) =>
|
||||
console.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the command using the specified implementation of <see cref="IConsole"/>.
|
||||
/// This is the method that's called when the command is invoked by a user through command line interface.
|
||||
/// This is the method that's called when the command is invoked by a user through command line.
|
||||
/// </summary>
|
||||
/// <remarks>If the execution of the command is not asynchronous, simply end the method with <code>return default;</code></remarks>
|
||||
ValueTask ExecuteAsync(IConsole console);
|
||||
|
||||
@@ -55,10 +55,63 @@ namespace CliFx
|
||||
void ResetColor();
|
||||
|
||||
/// <summary>
|
||||
/// Provides a token that signals when application cancellation is requested.
|
||||
/// Subsequent calls return the same token.
|
||||
/// When working with system console, the user can request cancellation by issuing an interrupt signal (Ctrl+C).
|
||||
/// Cursor left offset.
|
||||
/// </summary>
|
||||
int CursorLeft { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cursor top offset.
|
||||
/// </summary>
|
||||
int CursorTop { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defers the application termination in case of a cancellation request and returns the token that represents it.
|
||||
/// Subsequent calls to this method return the same token.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When working with <see cref="SystemConsole"/>:<br/>
|
||||
/// - Cancellation can be requested by the user by pressing Ctrl+C.<br/>
|
||||
/// - Cancellation can only be deferred once, subsequent requests to cancel by the user will result in instant termination.<br/>
|
||||
/// - Any code executing prior to calling this method is not cancellation-aware and as such will terminate instantly when cancellation is requested.
|
||||
/// </remarks>
|
||||
CancellationToken GetCancellationToken();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="IConsole"/>.
|
||||
/// </summary>
|
||||
public static class ConsoleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets console foreground color, executes specified action, and sets the color back to the original value.
|
||||
/// </summary>
|
||||
public static void WithForegroundColor(this IConsole console, ConsoleColor foregroundColor, Action action)
|
||||
{
|
||||
var lastColor = console.ForegroundColor;
|
||||
console.ForegroundColor = foregroundColor;
|
||||
|
||||
action();
|
||||
|
||||
console.ForegroundColor = lastColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets console background color, executes specified action, and sets the color back to the original value.
|
||||
/// </summary>
|
||||
public static void WithBackgroundColor(this IConsole console, ConsoleColor backgroundColor, Action action)
|
||||
{
|
||||
var lastColor = console.BackgroundColor;
|
||||
console.BackgroundColor = backgroundColor;
|
||||
|
||||
action();
|
||||
|
||||
console.BackgroundColor = lastColor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets console foreground and background colors, executes specified action, and sets the colors back to the original values.
|
||||
/// </summary>
|
||||
public static void WithColors(this IConsole console, ConsoleColor foregroundColor, ConsoleColor backgroundColor, Action action) =>
|
||||
console.WithForegroundColor(foregroundColor, () => console.WithBackgroundColor(backgroundColor, action));
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,12 @@
|
||||
namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstraction for a service can initialize objects at runtime.
|
||||
/// Abstraction for a service that can initialize objects at runtime.
|
||||
/// </summary>
|
||||
public interface ITypeActivator
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates an instance of specified type.
|
||||
/// Creates an instance of the specified type.
|
||||
/// </summary>
|
||||
object CreateInstance(Type type);
|
||||
}
|
||||
|
||||
13
CliFx/Internal/CollectionExtensions.cs
Normal file
13
CliFx/Internal/CollectionExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CliFx.Internal
|
||||
{
|
||||
internal static class CollectionExtensions
|
||||
{
|
||||
public static void RemoveRange<T>(this ICollection<T> source, IEnumerable<T> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
source.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,18 +1,48 @@
|
||||
// ReSharper disable CheckNamespace
|
||||
|
||||
#if NET45 || NETSTANDARD2_0
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
// Polyfills to bridge the missing APIs in older versions of the framework/standard.
|
||||
|
||||
namespace CliFx.Internal
|
||||
#if NETSTANDARD2_0
|
||||
namespace System
|
||||
{
|
||||
internal static class Polyfills
|
||||
{
|
||||
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> self, TKey key) =>
|
||||
self.TryGetValue(key, out var value) ? value : default;
|
||||
using Linq;
|
||||
|
||||
public static StringBuilder AppendJoin<T>(this StringBuilder self, string separator, IEnumerable<T> items) =>
|
||||
self.Append(string.Join(separator, items));
|
||||
internal static class Extensions
|
||||
{
|
||||
public static bool Contains(this string str, char c) =>
|
||||
str.Any(i => i == c);
|
||||
|
||||
public static bool StartsWith(this string str, char c) =>
|
||||
str.Length > 0 && str[0] == c;
|
||||
|
||||
public static bool EndsWith(this string str, char c) =>
|
||||
str.Length > 0 && str[str.Length - 1] == c;
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Collections.Generic
|
||||
{
|
||||
internal static class Extensions
|
||||
{
|
||||
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey key, out TValue value)
|
||||
{
|
||||
key = pair.Key;
|
||||
value = pair.Value;
|
||||
}
|
||||
|
||||
public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dic, TKey key) =>
|
||||
dic.TryGetValue(key, out var result) ? result! : default!;
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Linq
|
||||
{
|
||||
using Collections.Generic;
|
||||
|
||||
internal static class Extensions
|
||||
{
|
||||
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer) =>
|
||||
new HashSet<T>(source, comparer);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
13
CliFx/Internal/ProcessEx.cs
Normal file
13
CliFx/Internal/ProcessEx.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace CliFx.Internal
|
||||
{
|
||||
internal static class ProcessEx
|
||||
{
|
||||
public static int GetCurrentProcessId()
|
||||
{
|
||||
using var process = Process.GetCurrentProcess();
|
||||
return process.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
CliFx/Internal/StringExtensions.cs
Normal file
26
CliFx/Internal/StringExtensions.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CliFx.Internal
|
||||
{
|
||||
internal static class StringExtensions
|
||||
{
|
||||
public static string Repeat(this char c, int count) => new string(c, count);
|
||||
|
||||
public static string AsString(this char c) => c.Repeat(1);
|
||||
|
||||
public static string Quote(this string str) => $"\"{str}\"";
|
||||
|
||||
public static string JoinToString<T>(this IEnumerable<T> source, string separator) => string.Join(separator, source);
|
||||
|
||||
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
|
||||
builder.Length > 0 ? builder.Append(value) : builder;
|
||||
|
||||
public static string ToFormattableString(this object obj,
|
||||
IFormatProvider? formatProvider = null, string? format = null) =>
|
||||
obj is IFormattable formattable
|
||||
? formattable.ToString(format, formatProvider)
|
||||
: obj.ToString();
|
||||
}
|
||||
}
|
||||
@@ -2,31 +2,12 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Reflection;
|
||||
|
||||
namespace CliFx.Internal
|
||||
{
|
||||
internal static class Extensions
|
||||
internal static class TypeExtensions
|
||||
{
|
||||
public static string Repeat(this char c, int count) => new string(c, count);
|
||||
|
||||
public static string AsString(this char c) => c.Repeat(1);
|
||||
|
||||
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
|
||||
builder.Length > 0 ? builder.Append(value) : builder;
|
||||
|
||||
public static StringBuilder AppendBulletList<T>(this StringBuilder builder, IEnumerable<T> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
builder.Append("- ");
|
||||
builder.Append(item);
|
||||
builder.AppendLine();
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static bool Implements(this Type type, Type interfaceType) => type.GetInterfaces().Contains(interfaceType);
|
||||
|
||||
public static Type? GetNullableUnderlyingType(this Type type) => Nullable.GetUnderlyingType(type);
|
||||
@@ -42,13 +23,30 @@ namespace CliFx.Internal
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
|
||||
return type.GetGenericArguments().FirstOrDefault();
|
||||
|
||||
return type.GetInterfaces()
|
||||
return type
|
||||
.GetInterfaces()
|
||||
.Select(GetEnumerableUnderlyingType)
|
||||
.Where(t => t != null)
|
||||
.OrderByDescending(t => t != typeof(object)) // prioritize more specific types
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public static MethodInfo GetToStringMethod(this Type type) => type.GetMethod(nameof(ToString), Type.EmptyTypes);
|
||||
|
||||
public static bool IsToStringOverriden(this Type type) => type.GetToStringMethod() != typeof(object).GetToStringMethod();
|
||||
|
||||
public static MethodInfo GetStaticParseMethod(this Type type, bool withFormatProvider = false)
|
||||
{
|
||||
var argumentTypes = withFormatProvider
|
||||
? new[] {typeof(string), typeof(IFormatProvider)}
|
||||
: new[] {typeof(string)};
|
||||
|
||||
return type.GetMethod("Parse",
|
||||
BindingFlags.Public | BindingFlags.Static,
|
||||
null, argumentTypes, null
|
||||
);
|
||||
}
|
||||
|
||||
public static Array ToNonGenericArray<T>(this IEnumerable<T> source, Type elementType)
|
||||
{
|
||||
var sourceAsCollection = source as ICollection ?? source.ToArray();
|
||||
10
CliFx/Internal/VersionExtensions.cs
Normal file
10
CliFx/Internal/VersionExtensions.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace CliFx.Internal
|
||||
{
|
||||
internal static class VersionExtensions
|
||||
{
|
||||
public static string ToSemanticString(this Version version) =>
|
||||
version.Revision <= 0 ? version.ToString(3) : version.ToString();
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace CliFx
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="IConsole"/> that wraps the default system console.
|
||||
/// </summary>
|
||||
public class SystemConsole : IConsole
|
||||
public partial class SystemConsole : IConsole
|
||||
{
|
||||
private CancellationTokenSource? _cancellationTokenSource;
|
||||
|
||||
@@ -48,14 +48,28 @@ namespace CliFx
|
||||
/// </summary>
|
||||
public SystemConsole()
|
||||
{
|
||||
Input = new StreamReader(Console.OpenStandardInput(), Console.InputEncoding, false);
|
||||
Output = new StreamWriter(Console.OpenStandardOutput(), Console.OutputEncoding) {AutoFlush = true};
|
||||
Error = new StreamWriter(Console.OpenStandardError(), Console.OutputEncoding) {AutoFlush = true};
|
||||
Input = WrapInput(Console.OpenStandardInput());
|
||||
Output = WrapOutput(Console.OpenStandardOutput());
|
||||
Error = WrapOutput(Console.OpenStandardError());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ResetColor() => Console.ResetColor();
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CursorLeft
|
||||
{
|
||||
get => Console.CursorLeft;
|
||||
set => Console.CursorLeft = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CursorTop
|
||||
{
|
||||
get => Console.CursorTop;
|
||||
set => Console.CursorTop = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public CancellationToken GetCancellationToken()
|
||||
{
|
||||
@@ -77,4 +91,17 @@ namespace CliFx
|
||||
return (_cancellationTokenSource = cts).Token;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class SystemConsole
|
||||
{
|
||||
private static StreamReader WrapInput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamReader(Stream.Synchronized(stream), Console.InputEncoding, false)
|
||||
: StreamReader.Null;
|
||||
|
||||
private static StreamWriter WrapOutput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamWriter(Stream.Synchronized(stream), Console.OutputEncoding) {AutoFlush = true}
|
||||
: StreamWriter.Null;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
namespace CliFx.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="Utilities"/>.
|
||||
/// </summary>
|
||||
public static class Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="ProgressTicker"/> bound to this console.
|
||||
/// </summary>
|
||||
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
|
||||
}
|
||||
}
|
||||
@@ -9,26 +9,29 @@ namespace CliFx.Utilities
|
||||
{
|
||||
private readonly IConsole _console;
|
||||
|
||||
private string _lastOutput = "";
|
||||
private int? _originalCursorLeft;
|
||||
private int? _originalCursorTop;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="ProgressTicker"/>.
|
||||
/// </summary>
|
||||
public ProgressTicker(IConsole console)
|
||||
{
|
||||
_console = console;
|
||||
}
|
||||
|
||||
private void EraseLastOutput()
|
||||
{
|
||||
for (var i = 0; i < _lastOutput.Length; i++)
|
||||
_console.Output.Write('\b');
|
||||
}
|
||||
public ProgressTicker(IConsole console) => _console = console;
|
||||
|
||||
private void RenderProgress(double progress)
|
||||
{
|
||||
_lastOutput = progress.ToString("P2", _console.Output.FormatProvider);
|
||||
_console.Output.Write(_lastOutput);
|
||||
if (_originalCursorLeft != null && _originalCursorTop != null)
|
||||
{
|
||||
_console.CursorLeft = _originalCursorLeft.Value;
|
||||
_console.CursorTop = _originalCursorTop.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_originalCursorLeft = _console.CursorLeft;
|
||||
_originalCursorTop = _console.CursorTop;
|
||||
}
|
||||
|
||||
var str = progress.ToString("P2", _console.Output.FormatProvider);
|
||||
_console.Output.Write(str);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -41,9 +44,19 @@ namespace CliFx.Utilities
|
||||
// when there's no active console window.
|
||||
if (!_console.IsOutputRedirected)
|
||||
{
|
||||
EraseLastOutput();
|
||||
RenderProgress(progress);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extensions for <see cref="ProgressTicker"/>.
|
||||
/// </summary>
|
||||
public static class ProgressTickerExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="ProgressTicker"/> bound to this console.
|
||||
/// </summary>
|
||||
public static ProgressTicker CreateProgressTicker(this IConsole console) => new ProgressTicker(console);
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using System.Threading;
|
||||
namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="IConsole"/> that routes data to specified streams.
|
||||
/// Implementation of <see cref="IConsole"/> that routes all data to preconfigured streams.
|
||||
/// Does not leak to system console in any way.
|
||||
/// Use this class as a substitute for system console when running tests.
|
||||
/// </summary>
|
||||
@@ -44,6 +44,12 @@ namespace CliFx
|
||||
BackgroundColor = ConsoleColor.Black;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CursorLeft { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CursorTop { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public CancellationToken GetCancellationToken() => _cancellationToken;
|
||||
|
||||
@@ -88,12 +94,12 @@ namespace CliFx
|
||||
{
|
||||
private static StreamReader WrapInput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamReader(stream, Console.InputEncoding, false)
|
||||
? new StreamReader(Stream.Synchronized(stream), Console.InputEncoding, false)
|
||||
: StreamReader.Null;
|
||||
|
||||
private static StreamWriter WrapOutput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamWriter(stream, Console.OutputEncoding) {AutoFlush = true}
|
||||
? new StreamWriter(Stream.Synchronized(stream), Console.OutputEncoding) {AutoFlush = true}
|
||||
: StreamWriter.Null;
|
||||
}
|
||||
}
|
||||
18
Readme.md
18
Readme.md
@@ -26,7 +26,8 @@ An important property of CliFx, when compared to some other libraries, is that i
|
||||
- Prints errors and routes exit codes on exceptions
|
||||
- Provides comprehensive and colorful auto-generated help text
|
||||
- Highly testable and easy to debug
|
||||
- Targets .NET Framework 4.5+ and .NET Standard 2.0+
|
||||
- Comes with built-in analyzers to help catch common mistakes
|
||||
- Targets .NET Standard 2.0+
|
||||
- No external dependencies
|
||||
|
||||
## Screenshots
|
||||
@@ -50,6 +51,8 @@ An important property of CliFx, when compared to some other libraries, is that i
|
||||
|
||||
### Quick start
|
||||
|
||||

|
||||
|
||||
To turn your application into a command line interface you need to change your program's `Main` method so that it delegates execution to `CliApplication`.
|
||||
|
||||
The following code will create and run a default `CliApplication` that will resolve commands defined in the calling assembly. Using fluent interface provided by `CliApplicationBuilder` you can easily configure different aspects of your application.
|
||||
@@ -430,6 +433,19 @@ Division by zero is not supported.
|
||||
1337
|
||||
```
|
||||
|
||||
You can also specify the `showHelp` parameter to instruct whether to show the help text after printing the error:
|
||||
|
||||
```c#
|
||||
[Command]
|
||||
public class ExampleCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
throw new CommandException("Something went wrong.", showHelp: true);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Graceful cancellation
|
||||
|
||||
It is possible to gracefully cancel execution of a command and preform any necessary cleanup. By default an app gets forcefully killed when it receives an interrupt signal (Ctrl+C or Ctrl+Break), but you can easily override this behavior.
|
||||
|
||||
Reference in New Issue
Block a user