mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
671532efce | ||
|
|
5b124345b0 | ||
|
|
b812bd1423 | ||
|
|
c854f5fb8d | ||
|
|
f38bd32510 | ||
|
|
765fa5503e | ||
|
|
57f168723b | ||
|
|
79e1a2e3d7 | ||
|
|
f4f6d04857 | ||
|
|
015ede0d15 | ||
|
|
4fd7f7c3ca | ||
|
|
896dd49eb4 |
4
.github/workflows/CD.yml
vendored
4
.github/workflows/CD.yml
vendored
@@ -11,10 +11,10 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Install .NET Core
|
- name: Install .NET Core
|
||||||
uses: actions/setup-dotnet@v1
|
uses: actions/setup-dotnet@v1.4.0
|
||||||
with:
|
with:
|
||||||
dotnet-version: 3.1.100
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
|
|||||||
17
.github/workflows/CI.yml
vendored
17
.github/workflows/CI.yml
vendored
@@ -4,19 +4,26 @@ on: [push, pull_request]
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ${{ matrix.os }}
|
||||||
|
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
os: [ubuntu-latest, windows-latest, macos-latest]
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v1
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
- name: Install .NET Core
|
- name: Install .NET Core
|
||||||
uses: actions/setup-dotnet@v1
|
uses: actions/setup-dotnet@v1.4.0
|
||||||
with:
|
with:
|
||||||
dotnet-version: 3.1.100
|
dotnet-version: 3.1.100
|
||||||
|
|
||||||
- name: Build & test
|
- name: Build & test
|
||||||
run: dotnet test --configuration Release
|
run: dotnet test --configuration Release
|
||||||
|
|
||||||
- name: Coverage
|
- name: Upload coverage
|
||||||
run: curl -s https://codecov.io/bash | bash -s -- -f CliFx.Tests/bin/Release/Coverage.xml -t ${{secrets.CODECOV_TOKEN}} -Z
|
uses: codecov/codecov-action@v1.0.5
|
||||||
|
with:
|
||||||
|
token: ${{ secrets.CODECOV_TOKEN }}
|
||||||
|
file: CliFx.Tests/bin/Release/Coverage.xml
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
### v1.1 (16-Mar-2020)
|
||||||
|
|
||||||
|
- Changed `IConsole` interface (and as a result, `SystemConsole` and `VirtualConsole`) to support writing binary data. Instead of `TextReader`/`TextWriter` instances, the streams are now exposed as `StreamReader`/`StreamWriter` which provide the `BaseStream` property that allows raw access. Existing usages inside commands should remain the same because `StreamReader`/`StreamWriter` are compatible with their base classes `TextReader`/`TextWriter`, but if you were using `VirtualConsole` in tests, you may have to update it to the new API. Refer to the readme for more info.
|
||||||
|
- Changed argument binding behavior so that an error is produced if the user provides an argument that doesn't match with any parameter or option. This is done in order to improve user experience, as otherwise the user may make a typo without knowing that their input wasn't taken into account.
|
||||||
|
- Changed argument binding behavior so that options can be set to multiple argument values while specifying them with mixed naming. For example, `--option value1 -o value2 --option value3` would result in the option being set to corresponding three values, assuming `--option` and `-o` match with the same option.
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using BenchmarkDotNet.Attributes;
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using BenchmarkDotNet.Configs;
|
||||||
using BenchmarkDotNet.Order;
|
using BenchmarkDotNet.Order;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
using CliFx.Benchmarks.Commands;
|
using CliFx.Benchmarks.Commands;
|
||||||
using CommandLine;
|
using CommandLine;
|
||||||
|
|
||||||
@@ -9,13 +12,13 @@ namespace CliFx.Benchmarks
|
|||||||
[SimpleJob]
|
[SimpleJob]
|
||||||
[RankColumn]
|
[RankColumn]
|
||||||
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
|
||||||
public class Benchmark
|
public class Benchmarks
|
||||||
{
|
{
|
||||||
private static readonly string[] Arguments = {"--str", "hello world", "-i", "13", "-b"};
|
private static readonly string[] Arguments = {"--str", "hello world", "-i", "13", "-b"};
|
||||||
|
|
||||||
[Benchmark(Description = "CliFx", Baseline = true)]
|
[Benchmark(Description = "CliFx", Baseline = true)]
|
||||||
public async ValueTask<int> ExecuteWithCliFx() =>
|
public async ValueTask<int> ExecuteWithCliFx() =>
|
||||||
await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments);
|
await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments, new Dictionary<string, string>());
|
||||||
|
|
||||||
[Benchmark(Description = "System.CommandLine")]
|
[Benchmark(Description = "System.CommandLine")]
|
||||||
public async Task<int> ExecuteWithSystemCommandLine() =>
|
public async Task<int> ExecuteWithSystemCommandLine() =>
|
||||||
@@ -42,5 +45,8 @@ namespace CliFx.Benchmarks
|
|||||||
[Benchmark(Description = "Cocona")]
|
[Benchmark(Description = "Cocona")]
|
||||||
public void ExecuteWithCocona() =>
|
public void ExecuteWithCocona() =>
|
||||||
Cocona.CoconaApp.Run<CoconaCommand>(Arguments);
|
Cocona.CoconaApp.Run<CoconaCommand>(Arguments);
|
||||||
|
|
||||||
|
public static void Main() =>
|
||||||
|
BenchmarkRunner.Run<Benchmarks>(DefaultConfig.Instance.With(ConfigOptions.DisableOptimizationsValidator));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,9 +9,9 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
<PackageReference Include="BenchmarkDotNet" Version="0.12.0" />
|
||||||
<PackageReference Include="clipr" Version="1.6.1" />
|
<PackageReference Include="clipr" Version="1.6.1" />
|
||||||
<PackageReference Include="Cocona" Version="1.0.0" />
|
<PackageReference Include="Cocona" Version="1.3.0" />
|
||||||
<PackageReference Include="CommandLineParser" Version="2.7.82" />
|
<PackageReference Include="CommandLineParser" Version="2.7.82" />
|
||||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.5.0" />
|
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />
|
||||||
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
||||||
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
<PackageReference Include="System.CommandLine.Experimental" Version="0.3.0-alpha.19317.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
using BenchmarkDotNet.Configs;
|
|
||||||
using BenchmarkDotNet.Running;
|
|
||||||
|
|
||||||
namespace CliFx.Benchmarks
|
|
||||||
{
|
|
||||||
public static class Program
|
|
||||||
{
|
|
||||||
public static void Main() =>
|
|
||||||
BenchmarkRunner.Run(typeof(Program).Assembly, DefaultConfig.Instance
|
|
||||||
.With(ConfigOptions.DisableOptimizationsValidator));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
23
CliFx.Tests.Dummy/Commands/ConsoleTestCommand.cs
Normal file
23
CliFx.Tests.Dummy/Commands/ConsoleTestCommand.cs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests.Dummy.Commands
|
||||||
|
{
|
||||||
|
[Command("console-test")]
|
||||||
|
public class ConsoleTestCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
var input = console.Input.ReadToEnd();
|
||||||
|
|
||||||
|
console.WithColors(ConsoleColor.Black, ConsoleColor.White, () =>
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(input);
|
||||||
|
console.Error.WriteLine(input);
|
||||||
|
});
|
||||||
|
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,16 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Reflection;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CliFx.Tests.Dummy
|
namespace CliFx.Tests.Dummy
|
||||||
{
|
{
|
||||||
public class Program
|
public static partial class Program
|
||||||
|
{
|
||||||
|
public static Assembly Assembly { get; } = typeof(Program).Assembly;
|
||||||
|
|
||||||
|
public static string Location { get; } = Assembly.Location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static partial class Program
|
||||||
{
|
{
|
||||||
public static async Task Main() =>
|
public static async Task Main() =>
|
||||||
await new CliApplicationBuilder()
|
await new CliApplicationBuilder()
|
||||||
|
|||||||
137
CliFx.Tests/ApplicationSpecs.Commands.cs
Normal file
137
CliFx.Tests/ApplicationSpecs.Commands.cs
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ApplicationSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class NonImplementedCommand
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private class NonAnnotatedCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("dup")]
|
||||||
|
private class DuplicateNameCommandA : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("dup")]
|
||||||
|
private class DuplicateNameCommandB : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateParameterOrderCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(13)]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateParameterNameCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "param")]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1, Name = "param")]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class MultipleNonScalarParametersCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public IReadOnlyList<string>? ParameterB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class NonLastNonScalarParameterCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateOptionNamesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("fruits")]
|
||||||
|
public string? Apples { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("fruits")]
|
||||||
|
public string? Oranges { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateOptionShortNamesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('x')]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class ValidCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("hidden", Description = "Description")]
|
||||||
|
private class HiddenPropertiesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(13, Name = "param", Description = "Param description")]
|
||||||
|
public string? Parameter { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option", 'o', Description = "Option description", EnvironmentVariableName = "ENV")]
|
||||||
|
public string? Option { get; set; }
|
||||||
|
|
||||||
|
public string? HiddenA { get; set; }
|
||||||
|
|
||||||
|
public bool? HiddenB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
197
CliFx.Tests/ApplicationSpecs.cs
Normal file
197
CliFx.Tests/ApplicationSpecs.cs
Normal file
@@ -0,0 +1,197 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using CliFx.Domain;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ApplicationSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Application_can_be_created_with_a_default_configuration()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var app = new CliApplicationBuilder()
|
||||||
|
.AddCommandsFromThisAssembly()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
app.Should().NotBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Application_can_be_created_with_a_custom_configuration()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var app = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ValidCommand))
|
||||||
|
.AddCommandsFrom(typeof(ValidCommand).Assembly)
|
||||||
|
.AddCommands(new[] {typeof(ValidCommand)})
|
||||||
|
.AddCommandsFrom(new[] {typeof(ValidCommand).Assembly})
|
||||||
|
.AddCommandsFromThisAssembly()
|
||||||
|
.AllowDebugMode()
|
||||||
|
.AllowPreviewMode()
|
||||||
|
.UseTitle("test")
|
||||||
|
.UseExecutableName("test")
|
||||||
|
.UseVersionText("test")
|
||||||
|
.UseDescription("test")
|
||||||
|
.UseConsole(new VirtualConsole(Stream.Null))
|
||||||
|
.UseTypeActivator(Activator.CreateInstance)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
app.Should().NotBeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void At_least_one_command_must_be_defined_in_an_application()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = Array.Empty<Type>();
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Commands_must_implement_the_corresponding_interface()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Commands_must_be_annotated_by_an_attribute()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Commands_must_have_unique_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameters_must_have_unique_order()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameters_must_have_unique_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_must_have_unique_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_must_have_unique_short_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_must_have_unique_environment_variable_names()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Command_options_and_parameters_must_be_annotated_by_corresponding_attributes()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var commandTypes = new[] {typeof(HiddenPropertiesCommand)};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var schema = ApplicationSchema.Resolve(commandTypes);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
schema.Should().BeEquivalentTo(new ApplicationSchema(new[]
|
||||||
|
{
|
||||||
|
new CommandSchema(
|
||||||
|
typeof(HiddenPropertiesCommand),
|
||||||
|
"hidden",
|
||||||
|
"Description",
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
new CommandParameterSchema(
|
||||||
|
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Parameter)),
|
||||||
|
13,
|
||||||
|
"param",
|
||||||
|
"Param description")
|
||||||
|
},
|
||||||
|
new[]
|
||||||
|
{
|
||||||
|
new CommandOptionSchema(
|
||||||
|
typeof(HiddenPropertiesCommand).GetProperty(nameof(HiddenPropertiesCommand.Option)),
|
||||||
|
"option",
|
||||||
|
'o',
|
||||||
|
"ENV",
|
||||||
|
false,
|
||||||
|
"Option description")
|
||||||
|
})
|
||||||
|
}));
|
||||||
|
|
||||||
|
schema.ToString().Should().NotBeNullOrWhiteSpace(); // this is only for coverage, I'm sorry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
191
CliFx.Tests/ArgumentBindingSpecs.Commands.cs
Normal file
191
CliFx.Tests/ArgumentBindingSpecs.Commands.cs
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ArgumentBindingSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class AllSupportedTypesCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(Object))]
|
||||||
|
public object? Object { get; set; } = 42;
|
||||||
|
|
||||||
|
[CommandOption(nameof(String))]
|
||||||
|
public string? String { get; set; } = "foo bar";
|
||||||
|
|
||||||
|
[CommandOption(nameof(Bool))]
|
||||||
|
public bool Bool { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Char))]
|
||||||
|
public char Char { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Sbyte))]
|
||||||
|
public sbyte Sbyte { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Byte))]
|
||||||
|
public byte Byte { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Short))]
|
||||||
|
public short Short { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Ushort))]
|
||||||
|
public ushort Ushort { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Int))]
|
||||||
|
public int Int { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Uint))]
|
||||||
|
public uint Uint { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Long))]
|
||||||
|
public long Long { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Ulong))]
|
||||||
|
public ulong Ulong { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Float))]
|
||||||
|
public float Float { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Double))]
|
||||||
|
public double Double { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Decimal))]
|
||||||
|
public decimal Decimal { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(DateTime))]
|
||||||
|
public DateTime DateTime { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(DateTimeOffset))]
|
||||||
|
public DateTimeOffset DateTimeOffset { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TimeSpan))]
|
||||||
|
public TimeSpan TimeSpan { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(CustomEnum))]
|
||||||
|
public CustomEnum CustomEnum { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(IntNullable))]
|
||||||
|
public int? IntNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(CustomEnumNullable))]
|
||||||
|
public CustomEnum? CustomEnumNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TimeSpanNullable))]
|
||||||
|
public TimeSpan? TimeSpanNullable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringConstructable))]
|
||||||
|
public StringConstructable? TestStringConstructable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringParseable))]
|
||||||
|
public StringParseable? TestStringParseable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringParseableWithFormatProvider))]
|
||||||
|
public StringParseableWithFormatProvider? TestStringParseableWithFormatProvider { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(ObjectArray))]
|
||||||
|
public object[]? ObjectArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringArray))]
|
||||||
|
public string[]? StringArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(IntArray))]
|
||||||
|
public int[]? IntArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(CustomEnumArray))]
|
||||||
|
public CustomEnum[]? CustomEnumArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(IntNullableArray))]
|
||||||
|
public int?[]? IntNullableArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(TestStringConstructableArray))]
|
||||||
|
public StringConstructable[]? TestStringConstructableArray { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(Enumerable))]
|
||||||
|
public IEnumerable? Enumerable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringEnumerable))]
|
||||||
|
public IEnumerable<string>? StringEnumerable { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringReadOnlyList))]
|
||||||
|
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringList))]
|
||||||
|
public List<string>? StringList { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(StringHashSet))]
|
||||||
|
public HashSet<string>? StringHashSet { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class ArrayOptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option", 'o')]
|
||||||
|
public IReadOnlyList<string>? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class RequiredOptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(OptionA))]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(OptionB), IsRequired = true)]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class ParametersCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0)]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1)]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(2)]
|
||||||
|
public IReadOnlyList<string>? ParameterC { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class UnsupportedPropertyTypeCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(Option))]
|
||||||
|
public DummyType? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class UnsupportedEnumerablePropertyTypeCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(Option))]
|
||||||
|
public CustomEnumerable<string>? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class NoParameterCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption(nameof(OptionA))]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption(nameof(OptionB))]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
64
CliFx.Tests/ArgumentBindingSpecs.Types.cs
Normal file
64
CliFx.Tests/ArgumentBindingSpecs.Types.cs
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ArgumentBindingSpecs
|
||||||
|
{
|
||||||
|
private enum CustomEnum
|
||||||
|
{
|
||||||
|
Value1 = 1,
|
||||||
|
Value2 = 2,
|
||||||
|
Value3 = 3
|
||||||
|
}
|
||||||
|
|
||||||
|
private class StringConstructable
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
public StringConstructable(string value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class StringParseable
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
private StringParseable(string value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static StringParseable Parse(string value) => new StringParseable(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class StringParseableWithFormatProvider
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
private StringParseableWithFormatProvider(string value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||||
|
new StringParseableWithFormatProvider(value + " " + formatProvider);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DummyType
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public class CustomEnumerable<T> : IEnumerable<T>
|
||||||
|
{
|
||||||
|
private readonly T[] _arr = new T[0];
|
||||||
|
|
||||||
|
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _arr).GetEnumerator();
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
1076
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
1076
CliFx.Tests/ArgumentBindingSpecs.cs
Normal file
File diff suppressed because it is too large
Load Diff
315
CliFx.Tests/ArgumentSyntaxSpecs.cs
Normal file
315
CliFx.Tests/ArgumentSyntaxSpecs.cs
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
using System;
|
||||||
|
using CliFx.Domain;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class ArgumentSyntaxSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Input_is_empty_if_no_arguments_are_provided()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var args = Array.Empty<string>();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(args);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(CommandLineInput.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] DirectivesTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]", "[debug]"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.AddDirective("debug")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(DirectivesTestData))]
|
||||||
|
internal void Directive_can_be_enabled_by_specifying_its_name_in_square_brackets(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] OptionsTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option", "value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option", "value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option", "value1", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option", "value1", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option", "same value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option", "same value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "--option2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1")
|
||||||
|
.AddOption("option2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "value1", "--option2", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1", "value1")
|
||||||
|
.AddOption("option2", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "value1", "value2", "--option2", "value3", "value4"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1", "value1", "value2")
|
||||||
|
.AddOption("option2", "value3", "value4")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"--option1", "value1", "value2", "--option2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("option1", "value1", "value2")
|
||||||
|
.AddOption("option2")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(OptionsTestData))]
|
||||||
|
internal void Option_can_be_set_by_specifying_its_name_after_two_dashes(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] ShortOptionsTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o", "value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o", "value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o", "value1", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o", "value1", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-o", "same value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("o", "same value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "-b"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "value1", "-b", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a", "value1")
|
||||||
|
.AddOption("b", "value2")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "value1", "value2", "-b", "value3", "value4"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a", "value1", "value2")
|
||||||
|
.AddOption("b", "value3", "value4")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-a", "value1", "value2", "-b"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a", "value1", "value2")
|
||||||
|
.AddOption("b")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-abc"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-abc", "value"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c", "value")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"-abc", "value1", "value2"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c", "value1", "value2")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(ShortOptionsTestData))]
|
||||||
|
internal void Option_can_be_set_by_specifying_its_short_name_after_a_single_dash(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object[][] UnboundArgumentsTestData => new[]
|
||||||
|
{
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"foo"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"foo", "bar"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.AddUnboundArgument("bar")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]", "foo"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"foo", "--option", "value", "-abc"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.AddOption("option", "value")
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c")
|
||||||
|
.Build()
|
||||||
|
},
|
||||||
|
|
||||||
|
new object[]
|
||||||
|
{
|
||||||
|
new[] {"[preview]", "[debug]", "foo", "bar", "--option", "value", "-abc"},
|
||||||
|
new CommandLineInputBuilder()
|
||||||
|
.AddDirective("preview")
|
||||||
|
.AddDirective("debug")
|
||||||
|
.AddUnboundArgument("foo")
|
||||||
|
.AddUnboundArgument("bar")
|
||||||
|
.AddOption("option", "value")
|
||||||
|
.AddOption("a")
|
||||||
|
.AddOption("b")
|
||||||
|
.AddOption("c")
|
||||||
|
.Build()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(UnboundArgumentsTestData))]
|
||||||
|
internal void Any_remaining_arguments_are_treated_as_unbound_arguments(string[] arguments, CommandLineInput expectedInput)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var input = CommandLineInput.Parse(arguments);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
input.Should().BeEquivalentTo(expectedInput);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
CliFx.Tests/CancellationSpecs.Commands.cs
Normal file
27
CliFx.Tests/CancellationSpecs.Commands.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class CancellationSpecs
|
||||||
|
{
|
||||||
|
[Command("cancel")]
|
||||||
|
private class CancellableCommand : ICommand
|
||||||
|
{
|
||||||
|
public async ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(3), console.GetCancellationToken());
|
||||||
|
console.Output.WriteLine("Never printed");
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine("Cancellation requested");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
41
CliFx.Tests/CancellationSpecs.cs
Normal file
41
CliFx.Tests/CancellationSpecs.cs
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class CancellationSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(CancellableCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(0.2));
|
||||||
|
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"cancel"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdOutData.Should().Be("Cancellation requested");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
using NUnit.Framework;
|
|
||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CliApplicationBuilderTests
|
|
||||||
{
|
|
||||||
[Test(Description = "All builder methods must return without exceptions")]
|
|
||||||
public void Smoke_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var builder = new CliApplicationBuilder();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
builder
|
|
||||||
.AddCommand(typeof(HelloWorldDefaultCommand))
|
|
||||||
.AddCommandsFrom(typeof(HelloWorldDefaultCommand).Assembly)
|
|
||||||
.AddCommands(new[] {typeof(HelloWorldDefaultCommand)})
|
|
||||||
.AddCommandsFrom(new[] {typeof(HelloWorldDefaultCommand).Assembly})
|
|
||||||
.AddCommandsFromThisAssembly()
|
|
||||||
.AllowDebugMode()
|
|
||||||
.AllowPreviewMode()
|
|
||||||
.UseTitle("test")
|
|
||||||
.UseExecutableName("test")
|
|
||||||
.UseVersionText("test")
|
|
||||||
.UseDescription("test")
|
|
||||||
.UseConsole(new VirtualConsole(TextWriter.Null))
|
|
||||||
.UseTypeActivator(Activator.CreateInstance)
|
|
||||||
.Build();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test(Description = "Builder must be able to produce an application when no parameters are specified")]
|
|
||||||
public void Build_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var builder = new CliApplicationBuilder();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
builder.Build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,451 +0,0 @@
|
|||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Threading;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class CliApplicationTests
|
|
||||||
{
|
|
||||||
private const string TestAppName = "TestApp";
|
|
||||||
private const string TestVersionText = "v1.0";
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new string[0],
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"Hello world."
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "-i", "foo", "-i", "bar", "-s", " "},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"foo bar"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "-i", "one", "two", "three", "-s", ", "},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"one, two, three"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new[] {"div", "-D", "24", "-d", "8"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"3"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new[] {"--version"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
TestVersionText
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"--version"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
TestVersionText
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new string[0],
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"-h"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"--help"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "-h"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ExceptionCommand)},
|
|
||||||
new[] {"exc", "-h"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc", "-h"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"[preview]"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ExceptionCommand)},
|
|
||||||
new[] {"[preview]", "exc"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"[preview]", "concat", "-o", "value"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new Type[0],
|
|
||||||
new string[0],
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"non-existing"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ExceptionCommand)},
|
|
||||||
new[] {"exc"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
null, null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc", "-m", "foo bar"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"foo bar", null
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(CommandExceptionCommand)},
|
|
||||||
new[] {"exc", "-m", "foo bar", "-c", "666"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"foo bar", 666
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync_Help()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
|
||||||
new[] {"--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
TestVersionText,
|
|
||||||
"Description",
|
|
||||||
"HelpDefaultCommand description.",
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "[command]", "[options]",
|
|
||||||
"Options",
|
|
||||||
"-a|--option-a", "OptionA description.",
|
|
||||||
"-b|--option-b", "OptionB description.",
|
|
||||||
"-h|--help", "Shows help text.",
|
|
||||||
"--version", "Shows version information.",
|
|
||||||
"Commands",
|
|
||||||
"cmd", "HelpNamedCommand description.",
|
|
||||||
"You can run", "to show help on a specific command."
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelpSubCommand)},
|
|
||||||
new[] {"--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
TestVersionText,
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "[command]",
|
|
||||||
"Options",
|
|
||||||
"-h|--help", "Shows help text.",
|
|
||||||
"--version", "Shows version information.",
|
|
||||||
"Commands",
|
|
||||||
"cmd sub", "HelpSubCommand description.",
|
|
||||||
"You can run", "to show help on a specific command."
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
|
||||||
new[] {"cmd", "--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"HelpNamedCommand description.",
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "cmd", "[command]", "[options]",
|
|
||||||
"Options",
|
|
||||||
"-c|--option-c", "OptionC description.",
|
|
||||||
"-d|--option-d", "OptionD description.",
|
|
||||||
"-h|--help", "Shows help text.",
|
|
||||||
"Commands",
|
|
||||||
"sub", "HelpSubCommand description.",
|
|
||||||
"You can run", "to show help on a specific command."
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelpDefaultCommand), typeof(HelpNamedCommand), typeof(HelpSubCommand)},
|
|
||||||
new[] {"cmd", "sub", "--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"HelpSubCommand description.",
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "cmd sub", "[options]",
|
|
||||||
"Options",
|
|
||||||
"-e|--option-e", "OptionE description.",
|
|
||||||
"-h|--help", "Shows help text."
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ParameterCommand)},
|
|
||||||
new[] {"param", "cmd", "--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"Command using positional parameters",
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "param cmd", "<first>", "<parameterb>", "<third list...>", "[options]",
|
|
||||||
"Parameters",
|
|
||||||
"* first",
|
|
||||||
"* parameterb",
|
|
||||||
"* third list", "A list of numbers",
|
|
||||||
"Options",
|
|
||||||
"-o|--option",
|
|
||||||
"-h|--help", "Shows help text."
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllRequiredOptionsCommand)},
|
|
||||||
new[] {"allrequired", "--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"AllRequiredOptionsCommand description.",
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "allrequired --option-f <value> --option-g <value>"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(SomeRequiredOptionsCommand)},
|
|
||||||
new[] {"somerequired", "--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Description",
|
|
||||||
"SomeRequiredOptionsCommand description.",
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "somerequired --option-f <value> [options]"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(EnvironmentVariableCommand)},
|
|
||||||
new[] {"--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Environment variable:", "ENV_SINGLE_VALUE"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new[] {"concat", "--help"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
"Usage",
|
|
||||||
TestAppName, "concat", "-i", "<values...>", "[options]",
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_RunAsync))]
|
|
||||||
public async Task RunAsync_Test(
|
|
||||||
IReadOnlyList<Type> commandTypes,
|
|
||||||
IReadOnlyList<string> commandLineArguments,
|
|
||||||
IReadOnlyDictionary<string, string> environmentVariables,
|
|
||||||
string? expectedStdOut = null)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
await using var stdOutStream = new StringWriter();
|
|
||||||
var console = new VirtualConsole(stdOutStream);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
|
||||||
.AddCommands(commandTypes)
|
|
||||||
.UseTitle(TestAppName)
|
|
||||||
.UseExecutableName(TestAppName)
|
|
||||||
.UseVersionText(TestVersionText)
|
|
||||||
.UseConsole(console)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
|
||||||
var stdOut = stdOutStream.ToString().Trim();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
exitCode.Should().Be(0);
|
|
||||||
stdOut.Should().NotBeNullOrWhiteSpace();
|
|
||||||
|
|
||||||
if (expectedStdOut != null)
|
|
||||||
stdOut.Should().Be(expectedStdOut);
|
|
||||||
|
|
||||||
Console.WriteLine(stdOut);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_RunAsync_Negative))]
|
|
||||||
public async Task RunAsync_Negative_Test(
|
|
||||||
IReadOnlyList<Type> commandTypes,
|
|
||||||
IReadOnlyList<string> commandLineArguments,
|
|
||||||
IReadOnlyDictionary<string, string> environmentVariables,
|
|
||||||
string? expectedStdErr = null,
|
|
||||||
int? expectedExitCode = null)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
await using var stdErrStream = new StringWriter();
|
|
||||||
var console = new VirtualConsole(TextWriter.Null, stdErrStream);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
|
||||||
.AddCommands(commandTypes)
|
|
||||||
.UseTitle(TestAppName)
|
|
||||||
.UseExecutableName(TestAppName)
|
|
||||||
.UseVersionText(TestVersionText)
|
|
||||||
.UseConsole(console)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
|
||||||
var stderr = stdErrStream.ToString().Trim();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
exitCode.Should().NotBe(0);
|
|
||||||
stderr.Should().NotBeNullOrWhiteSpace();
|
|
||||||
|
|
||||||
if (expectedExitCode != null)
|
|
||||||
exitCode.Should().Be(expectedExitCode);
|
|
||||||
|
|
||||||
if (expectedStdErr != null)
|
|
||||||
stderr.Should().Be(expectedStdErr);
|
|
||||||
|
|
||||||
Console.WriteLine(stderr);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_RunAsync_Help))]
|
|
||||||
public async Task RunAsync_Help_Test(
|
|
||||||
IReadOnlyList<Type> commandTypes,
|
|
||||||
IReadOnlyList<string> commandLineArguments,
|
|
||||||
IReadOnlyList<string>? expectedSubstrings = null)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
await using var stdOutStream = new StringWriter();
|
|
||||||
var console = new VirtualConsole(stdOutStream);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
|
||||||
.AddCommands(commandTypes)
|
|
||||||
.UseTitle(TestAppName)
|
|
||||||
.UseExecutableName(TestAppName)
|
|
||||||
.UseVersionText(TestVersionText)
|
|
||||||
.UseConsole(console)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
var environmentVariables = new Dictionary<string, string>();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
|
||||||
var stdOut = stdOutStream.ToString().Trim();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
exitCode.Should().Be(0);
|
|
||||||
stdOut.Should().NotBeNullOrWhiteSpace();
|
|
||||||
|
|
||||||
if (expectedSubstrings != null)
|
|
||||||
stdOut.Should().ContainAll(expectedSubstrings);
|
|
||||||
|
|
||||||
Console.WriteLine(stdOut);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public async Task RunAsync_Cancellation_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
using var cancellationTokenSource = new CancellationTokenSource();
|
|
||||||
|
|
||||||
await using var stdOutStream = new StringWriter();
|
|
||||||
await using var stdErrStream = new StringWriter();
|
|
||||||
var console = new VirtualConsole(stdOutStream, stdErrStream, cancellationTokenSource.Token);
|
|
||||||
|
|
||||||
var application = new CliApplicationBuilder()
|
|
||||||
.AddCommand(typeof(CancellableCommand))
|
|
||||||
.UseConsole(console)
|
|
||||||
.Build();
|
|
||||||
|
|
||||||
var commandLineArguments = new[] {"cancel"};
|
|
||||||
var environmentVariables = new Dictionary<string, string>();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(0.2));
|
|
||||||
var exitCode = await application.RunAsync(commandLineArguments, environmentVariables);
|
|
||||||
var stdOut = stdOutStream.ToString().Trim();
|
|
||||||
var stdErr = stdErrStream.ToString().Trim();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
exitCode.Should().NotBe(0);
|
|
||||||
stdOut.Should().BeNullOrWhiteSpace();
|
|
||||||
stdErr.Should().NotBeNullOrWhiteSpace();
|
|
||||||
|
|
||||||
Console.WriteLine(stdErr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,12 +11,16 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="CliWrap" Version="2.5.0" />
|
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
|
||||||
<PackageReference Include="FluentAssertions" Version="5.10.0" />
|
</ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
|
|
||||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
<ItemGroup>
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
|
<PackageReference Include="CliWrap" Version="3.0.0" />
|
||||||
|
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||||
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
<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" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -24,8 +28,12 @@
|
|||||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<Target Name="Copy dummy's runtime config" AfterTargets="AfterBuild">
|
<ItemGroup>
|
||||||
<Copy SourceFiles="../CliFx.Tests.Dummy/bin/$(Configuration)/$(TargetFramework)/CliFx.Tests.Dummy.runtimeconfig.json" DestinationFiles="$(OutputPath)CliFx.Tests.Dummy.runtimeconfig.json" />
|
<None Include="../CliFx.Tests.Dummy/bin/$(Configuration)/$(TargetFramework)/CliFx.Tests.Dummy.runtimeconfig.json">
|
||||||
</Target>
|
<Link>CliFx.Tests.Dummy.runtimeconfig.json</Link>
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
<Visible>False</Visible>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
72
CliFx.Tests/ConsoleSpecs.cs
Normal file
72
CliFx.Tests/ConsoleSpecs.cs
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliWrap;
|
||||||
|
using CliWrap.Buffered;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class ConsoleSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Real_implementation_of_console_maps_directly_to_system_console()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var command = "Hello world" | Cli.Wrap("dotnet")
|
||||||
|
.WithArguments(a => a
|
||||||
|
.Add(Dummy.Program.Location)
|
||||||
|
.Add("console-test"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var result = await command.ExecuteBufferedAsync();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
result.StandardOutput.TrimEnd().Should().Be("Hello world");
|
||||||
|
result.StandardError.TrimEnd().Should().Be("Hello world");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Fake_implementation_of_console_can_be_used_to_execute_commands_in_isolation()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stdIn = new MemoryStream(Console.InputEncoding.GetBytes("input"));
|
||||||
|
using var stdOut = new MemoryStream();
|
||||||
|
using var stdErr = new MemoryStream();
|
||||||
|
|
||||||
|
var console = new VirtualConsole(
|
||||||
|
input: stdIn,
|
||||||
|
output: stdOut,
|
||||||
|
error: stdErr);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
console.Output.Write("output");
|
||||||
|
console.Error.Write("error");
|
||||||
|
|
||||||
|
var stdInData = console.Input.ReadToEnd();
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray());
|
||||||
|
|
||||||
|
console.ResetColor();
|
||||||
|
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||||
|
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdInData.Should().Be("input");
|
||||||
|
stdOutData.Should().Be("output");
|
||||||
|
stdErrData.Should().Be("error");
|
||||||
|
|
||||||
|
console.Input.Should().NotBeSameAs(Console.In);
|
||||||
|
console.Output.Should().NotBeSameAs(Console.Out);
|
||||||
|
console.Error.Should().NotBeSameAs(Console.Error);
|
||||||
|
|
||||||
|
console.IsInputRedirected.Should().BeTrue();
|
||||||
|
console.IsOutputRedirected.Should().BeTrue();
|
||||||
|
console.IsErrorRedirected.Should().BeTrue();
|
||||||
|
|
||||||
|
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
|
||||||
|
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using CliFx.Tests.TestCustomTypes;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class DefaultCommandFactoryTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(typeof(HelloWorldDefaultCommand));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(typeof(TestNonStringParseable));
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_CreateInstance))]
|
|
||||||
public void CreateInstance_Test(Type type)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var activator = new DefaultTypeActivator();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var obj = activator.CreateInstance(type);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
obj.Should().BeOfType(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_CreateInstance_Negative))]
|
|
||||||
public void CreateInstance_Negative_Test(Type type)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var activator = new DefaultTypeActivator();
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(type));
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class DelegateCommandFactoryTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new Func<Type, object>(Activator.CreateInstance),
|
|
||||||
typeof(HelloWorldDefaultCommand)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_CreateInstance_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new Func<Type, object>(_ => null),
|
|
||||||
typeof(HelloWorldDefaultCommand)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_CreateInstance))]
|
|
||||||
public void CreateInstance_Test(Func<Type, object> activatorFunc, Type type)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var activator = new DelegateTypeActivator(activatorFunc);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var obj = activator.CreateInstance(type);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
obj.Should().BeOfType(type);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_CreateInstance_Negative))]
|
|
||||||
public void CreateInstance_Negative_Test(Func<Type, object> activatorFunc, Type type)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var activator = new DelegateTypeActivator(activatorFunc);
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => activator.CreateInstance(type));
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
37
CliFx.Tests/DependencyInjectionSpecs.Commands.cs
Normal file
37
CliFx.Tests/DependencyInjectionSpecs.Commands.cs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DependencyInjectionSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class WithoutDependenciesCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DependencyA
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DependencyB
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class WithDependenciesCommand : ICommand
|
||||||
|
{
|
||||||
|
private readonly DependencyA _dependencyA;
|
||||||
|
private readonly DependencyB _dependencyB;
|
||||||
|
|
||||||
|
public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
|
||||||
|
{
|
||||||
|
_dependencyA = dependencyA;
|
||||||
|
_dependencyB = dependencyB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
58
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
58
CliFx.Tests/DependencyInjectionSpecs.cs
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
using CliFx.Exceptions;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DependencyInjectionSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Default_type_activator_can_initialize_a_command_if_it_has_a_parameterless_constructor()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DefaultTypeActivator();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var obj = activator.CreateInstance(typeof(WithoutDependenciesCommand));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
obj.Should().BeOfType<WithoutDependenciesCommand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Default_type_activator_cannot_initialize_a_command_if_it_does_not_have_a_parameterless_constructor()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DefaultTypeActivator();
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() =>
|
||||||
|
activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delegate_type_activator_can_initialize_a_command_using_a_custom_function()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DelegateTypeActivator(_ =>
|
||||||
|
new WithDependenciesCommand(new DependencyA(), new DependencyB()));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var obj = activator.CreateInstance(typeof(WithDependenciesCommand));
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
obj.Should().BeOfType<WithDependenciesCommand>();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Delegate_type_activator_throws_if_the_underlying_function_returns_null()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var activator = new DelegateTypeActivator(_ => null);
|
||||||
|
|
||||||
|
// Act & assert
|
||||||
|
Assert.Throws<CliFxException>(() =>
|
||||||
|
activator.CreateInstance(typeof(WithDependenciesCommand)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
CliFx.Tests/DirectivesSpecs.Commands.cs
Normal file
14
CliFx.Tests/DirectivesSpecs.Commands.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DirectivesSpecs
|
||||||
|
{
|
||||||
|
[Command("cmd")]
|
||||||
|
private class NamedCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
36
CliFx.Tests/DirectivesSpecs.cs
Normal file
36
CliFx.Tests/DirectivesSpecs.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class DirectivesSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Preview_directive_can_be_enabled_to_print_provided_arguments_as_they_were_parsed()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.AllowPreviewMode()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().ContainAll("cmd", "<param>", "[-a]", "[-b]", "[-c]", "[--option foo]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,888 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using CliFx.Domain;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
using CliFx.Tests.TestCommands;
|
|
||||||
using CliFx.Tests.TestCustomTypes;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Domain
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
internal partial class ApplicationSchemaTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_Resolve()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
typeof(DivideCommand),
|
|
||||||
typeof(ConcatCommand),
|
|
||||||
typeof(EnvironmentVariableCommand)
|
|
||||||
},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandSchema(typeof(DivideCommand), "div", "Divide one number by another.",
|
|
||||||
new CommandParameterSchema[0], new[]
|
|
||||||
{
|
|
||||||
new CommandOptionSchema(typeof(DivideCommand).GetProperty(nameof(DivideCommand.Dividend)),
|
|
||||||
"dividend", 'D', null, true, "The number to divide."),
|
|
||||||
new CommandOptionSchema(typeof(DivideCommand).GetProperty(nameof(DivideCommand.Divisor)),
|
|
||||||
"divisor", 'd', null, true, "The number to divide by.")
|
|
||||||
}),
|
|
||||||
new CommandSchema(typeof(ConcatCommand), "concat", "Concatenate strings.",
|
|
||||||
new CommandParameterSchema[0],
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionSchema(typeof(ConcatCommand).GetProperty(nameof(ConcatCommand.Inputs)),
|
|
||||||
null, 'i', null, true, "Input strings."),
|
|
||||||
new CommandOptionSchema(typeof(ConcatCommand).GetProperty(nameof(ConcatCommand.Separator)),
|
|
||||||
null, 's', null, false, "String separator.")
|
|
||||||
}),
|
|
||||||
new CommandSchema(typeof(EnvironmentVariableCommand), null, "Reads option values from environment variables.",
|
|
||||||
new CommandParameterSchema[0],
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionSchema(typeof(EnvironmentVariableCommand).GetProperty(nameof(EnvironmentVariableCommand.Option)),
|
|
||||||
"opt", null, "ENV_SINGLE_VALUE", false, null)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(SimpleParameterCommand)},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandSchema(typeof(SimpleParameterCommand), "param cmd2", "Command using positional parameters",
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandParameterSchema(typeof(SimpleParameterCommand).GetProperty(nameof(SimpleParameterCommand.ParameterA)),
|
|
||||||
0, "first", null),
|
|
||||||
new CommandParameterSchema(typeof(SimpleParameterCommand).GetProperty(nameof(SimpleParameterCommand.ParameterB)),
|
|
||||||
10, null, null)
|
|
||||||
},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionSchema(typeof(SimpleParameterCommand).GetProperty(nameof(SimpleParameterCommand.OptionA)),
|
|
||||||
"option", 'o', null, false, null)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(HelloWorldDefaultCommand)},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandSchema(typeof(HelloWorldDefaultCommand), null, null,
|
|
||||||
new CommandParameterSchema[0],
|
|
||||||
new CommandOptionSchema[0])
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_Resolve_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new Type[0]
|
|
||||||
});
|
|
||||||
|
|
||||||
// Command validation failure
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(NonImplementedCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
// Same name
|
|
||||||
new[] {typeof(ExceptionCommand), typeof(CommandExceptionCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(NonAnnotatedCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Parameter validation failure
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(DuplicateParameterOrderCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(DuplicateParameterNameCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(MultipleNonScalarParametersCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(NonLastNonScalarParameterCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Option validation failure
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(DuplicateOptionNamesCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(DuplicateOptionShortNamesCommand)}
|
|
||||||
});
|
|
||||||
|
|
||||||
yield return new TestCaseData(new object[]
|
|
||||||
{
|
|
||||||
new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_Resolve))]
|
|
||||||
public void Resolve_Test(
|
|
||||||
IReadOnlyList<Type> commandTypes,
|
|
||||||
IReadOnlyList<CommandSchema> expectedCommandSchemas)
|
|
||||||
{
|
|
||||||
// Act
|
|
||||||
var applicationSchema = ApplicationSchema.Resolve(commandTypes);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
applicationSchema.Commands.Should().BeEquivalentTo(expectedCommandSchemas);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_Resolve_Negative))]
|
|
||||||
public void Resolve_Negative_Test(IReadOnlyList<Type> commandTypes)
|
|
||||||
{
|
|
||||||
// Act & Assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal partial class ApplicationSchemaTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_InitializeEntryPoint()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Object), "value")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Object = "value"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.String), "value")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {String = "value"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Bool), "true")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Bool = true}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Bool), "false")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Bool = false}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Bool))
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Bool = true}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Char), "a")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Char = 'a'}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Sbyte), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Sbyte = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Byte), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Byte = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Short), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Short = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Ushort), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Ushort = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Int), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Int = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Uint), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Uint = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Long), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Long = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Ulong), "15")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Ulong = 15}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Float), "123.45")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Float = 123.45f}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Double), "123.45")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Double = 123.45}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Decimal), "123.45")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Decimal = 123.45m}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.DateTime), "28 Apr 1995")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {DateTime = new DateTime(1995, 04, 28)}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.DateTimeOffset), "28 Apr 1995")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {DateTimeOffset = new DateTime(1995, 04, 28)}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TimeSpan), "00:14:59")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TimeSpan = new TimeSpan(00, 14, 59)}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnum), "value2")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TestEnum = TestEnum.Value2}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntNullable), "666")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {IntNullable = 666}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntNullable))
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {IntNullable = null}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnumNullable), "value3")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TestEnumNullable = TestEnum.Value3}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnumNullable))
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TestEnumNullable = null}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TimeSpanNullable), "01:00:00")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TimeSpanNullable = new TimeSpan(01, 00, 00)}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TimeSpanNullable))
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TimeSpanNullable = null}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringConstructable), "value")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TestStringConstructable = new TestStringConstructable("value")}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringParseable), "value")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TestStringParseable = TestStringParseable.Parse("value")}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringParseableWithFormatProvider), "value")
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand
|
|
||||||
{
|
|
||||||
TestStringParseableWithFormatProvider =
|
|
||||||
TestStringParseableWithFormatProvider.Parse("value", CultureInfo.InvariantCulture)
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.ObjectArray), new[] {"value1", "value2"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {ObjectArray = new object[] {"value1", "value2"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringArray), new[] {"value1", "value2"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {StringArray = new[] {"value1", "value2"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringArray))
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {StringArray = new string[0]}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntArray), new[] {"47", "69"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {IntArray = new[] {47, 69}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestEnumArray), new[] {"value1", "value3"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {TestEnumArray = new[] {TestEnum.Value1, TestEnum.Value3}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.IntNullableArray), new[] {"1337", "2441"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {IntNullableArray = new int?[] {1337, 2441}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.TestStringConstructableArray), new[] {"value1", "value2"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand
|
|
||||||
{
|
|
||||||
TestStringConstructableArray = new[]
|
|
||||||
{
|
|
||||||
new TestStringConstructable("value1"),
|
|
||||||
new TestStringConstructable("value2")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.Enumerable), new[] {"value1", "value3"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {Enumerable = new[] {"value1", "value3"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringEnumerable), new[] {"value1", "value3"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {StringEnumerable = new[] {"value1", "value3"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringReadOnlyList), new[] {"value1", "value3"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {StringReadOnlyList = new[] {"value1", "value3"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringList), new[] {"value1", "value3"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {StringList = new List<string> {"value1", "value3"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput(nameof(AllSupportedTypesCommand.StringHashSet), new[] {"value1", "value3"})
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new AllSupportedTypesCommand {StringHashSet = new HashSet<string> {"value1", "value3"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"div"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("dividend", "13"),
|
|
||||||
new CommandOptionInput("divisor", "8"),
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"div"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("D", "13"),
|
|
||||||
new CommandOptionInput("d", "8"),
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"div"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("dividend", "13"),
|
|
||||||
new CommandOptionInput("d", "8"),
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new DivideCommand {Dividend = 13, Divisor = 8}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"concat"},
|
|
||||||
new[] {new CommandOptionInput("i", new[] {"foo", " ", "bar"}),}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new ConcatCommand {Inputs = new[] {"foo", " ", "bar"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"concat"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("i", new[] {"foo", "bar"}),
|
|
||||||
new CommandOptionInput("s", " "),
|
|
||||||
}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new ConcatCommand {Inputs = new[] {"foo", "bar"}, Separator = " "}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(EnvironmentVariableCommand)},
|
|
||||||
CommandLineInput.Empty,
|
|
||||||
new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
["ENV_SINGLE_VALUE"] = "A"
|
|
||||||
},
|
|
||||||
new EnvironmentVariableCommand {Option = "A"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(EnvironmentVariableWithMultipleValuesCommand)},
|
|
||||||
CommandLineInput.Empty,
|
|
||||||
new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
["ENV_MULTIPLE_VALUES"] = string.Join(Path.PathSeparator, "A", "B", "C")
|
|
||||||
},
|
|
||||||
new EnvironmentVariableWithMultipleValuesCommand {Option = new[] {"A", "B", "C"}}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(EnvironmentVariableCommand)},
|
|
||||||
new CommandLineInput(new[] {new CommandOptionInput("opt", "X")}),
|
|
||||||
new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
["ENV_SINGLE_VALUE"] = "A"
|
|
||||||
},
|
|
||||||
new EnvironmentVariableCommand {Option = "X"}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(EnvironmentVariableWithoutCollectionPropertyCommand)},
|
|
||||||
CommandLineInput.Empty,
|
|
||||||
new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
["ENV_MULTIPLE_VALUES"] = string.Join(Path.PathSeparator, "A", "B", "C")
|
|
||||||
},
|
|
||||||
new EnvironmentVariableWithoutCollectionPropertyCommand {Option = string.Join(Path.PathSeparator, "A", "B", "C")}
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ParameterCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"param", "cmd", "abc", "123", "1", "2"},
|
|
||||||
new[] {new CommandOptionInput("o", "option value")}),
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
new ParameterCommand
|
|
||||||
{
|
|
||||||
ParameterA = "abc",
|
|
||||||
ParameterB = 123,
|
|
||||||
ParameterC = new[] {1, 2},
|
|
||||||
OptionA = "option value"
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_InitializeEntryPoint_Negative()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.Int), "1234.5")}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.Int), new[] {"123", "456"})}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.Int))}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(AllSupportedTypesCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {new CommandOptionInput(nameof(AllSupportedTypesCommand.NonConvertible), "123")}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new CommandLineInput(new[] {"div"}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new CommandLineInput(new[] {"div", "-D", "13"}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new CommandLineInput(new[] {"concat"}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ConcatCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"concat"},
|
|
||||||
new[] {new CommandOptionInput("s", "_")}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ParameterCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"param", "cmd"},
|
|
||||||
new[] {new CommandOptionInput("o", "option value")}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(ParameterCommand)},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"param", "cmd", "abc", "123", "invalid"},
|
|
||||||
new[] {new CommandOptionInput("o", "option value")}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(DivideCommand)},
|
|
||||||
new CommandLineInput(new[] {"non-existing"}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {typeof(BrokenEnumerableCommand)},
|
|
||||||
new CommandLineInput(new[] {"value1", "value2"}),
|
|
||||||
new Dictionary<string, string>()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_InitializeEntryPoint))]
|
|
||||||
public void InitializeEntryPoint_Test(
|
|
||||||
IReadOnlyList<Type> commandTypes,
|
|
||||||
CommandLineInput commandLineInput,
|
|
||||||
IReadOnlyDictionary<string, string> environmentVariables,
|
|
||||||
ICommand expectedResult)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var applicationSchema = ApplicationSchema.Resolve(commandTypes);
|
|
||||||
var typeActivator = new DefaultTypeActivator();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var command = applicationSchema.InitializeEntryPoint(commandLineInput, environmentVariables, typeActivator);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
command.Should().BeEquivalentTo(expectedResult, o => o.RespectingRuntimeTypes());
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_InitializeEntryPoint_Negative))]
|
|
||||||
public void InitializeEntryPoint_Negative_Test(
|
|
||||||
IReadOnlyList<Type> commandTypes,
|
|
||||||
CommandLineInput commandLineInput,
|
|
||||||
IReadOnlyDictionary<string, string> environmentVariables)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var applicationSchema = ApplicationSchema.Resolve(commandTypes);
|
|
||||||
var typeActivator = new DefaultTypeActivator();
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
var ex = Assert.Throws<CliFxException>(() =>
|
|
||||||
applicationSchema.InitializeEntryPoint(commandLineInput, environmentVariables, typeActivator));
|
|
||||||
Console.WriteLine(ex.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,264 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using CliFx.Domain;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Domain
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
internal class CommandLineInputTests
|
|
||||||
{
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_Parse()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new string[0],
|
|
||||||
CommandLineInput.Empty
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"param"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"param"})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"cmd", "param"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"cmd", "param"})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option1", "value1", "--option2", "value2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option1", "value1"),
|
|
||||||
new CommandOptionInput("option2", "value2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option", "value1", "value2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option", "value1", "--option", "value2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value1", "-b", "value2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", "value1"),
|
|
||||||
new CommandOptionInput("b", "value2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value1", "value2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "value1", "-a", "value2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a", new[] {"value1", "value2"})
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--option1", "value1", "-b", "value2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option1", "value1"),
|
|
||||||
new CommandOptionInput("b", "value2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--switch"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("switch")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--switch1", "--switch2"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("switch1"),
|
|
||||||
new CommandOptionInput("switch2")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-s"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("s")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-a", "-b"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a"),
|
|
||||||
new CommandOptionInput("b")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-ab"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a"),
|
|
||||||
new CommandOptionInput("b")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"-ab", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("a"),
|
|
||||||
new CommandOptionInput("b", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"cmd", "--option", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"cmd"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"[debug]"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"debug"},
|
|
||||||
new string[0],
|
|
||||||
new CommandOptionInput[0])
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"[debug]", "[preview]"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new string[0],
|
|
||||||
new CommandOptionInput[0])
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"cmd", "param1", "param2", "--option", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"cmd", "param1", "param2"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("option", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"[debug]", "[preview]", "-o", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new string[0],
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("o", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"cmd", "[debug]", "[preview]", "-o", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new[] {"cmd"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("o", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"cmd", "[debug]", "[preview]", "-o", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new[] {"cmd"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("o", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"cmd", "param", "[debug]", "[preview]", "-o", "value"},
|
|
||||||
new CommandLineInput(
|
|
||||||
new[] {"debug", "preview"},
|
|
||||||
new[] {"cmd", "param"},
|
|
||||||
new[]
|
|
||||||
{
|
|
||||||
new CommandOptionInput("o", "value")
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
[TestCaseSource(nameof(GetTestCases_Parse))]
|
|
||||||
public void Parse_Test(IReadOnlyList<string> commandLineArguments, CommandLineInput expectedResult)
|
|
||||||
{
|
|
||||||
// Act
|
|
||||||
var result = CommandLineInput.Parse(commandLineArguments);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
result.Should().BeEquivalentTo(expectedResult);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,82 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliWrap;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class DummyTests
|
|
||||||
{
|
|
||||||
private static Assembly DummyAssembly { get; } = typeof(Dummy.Program).Assembly;
|
|
||||||
|
|
||||||
private static IEnumerable<TestCaseData> GetTestCases_RunAsync()
|
|
||||||
{
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--version"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
$"v{DummyAssembly.GetName().Version}"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new string[0],
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"Hello World!"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--target", "Earth"},
|
|
||||||
new Dictionary<string, string>(),
|
|
||||||
"Hello Earth!"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new string[0],
|
|
||||||
new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
["ENV_TARGET"] = "Mars"
|
|
||||||
},
|
|
||||||
"Hello Mars!"
|
|
||||||
);
|
|
||||||
|
|
||||||
yield return new TestCaseData(
|
|
||||||
new[] {"--target", "Earth"},
|
|
||||||
new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
["ENV_TARGET"] = "Mars"
|
|
||||||
},
|
|
||||||
"Hello Earth!"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
[TestCaseSource(nameof(GetTestCases_RunAsync))]
|
|
||||||
public async Task RunAsync_Test(
|
|
||||||
IReadOnlyList<string> arguments,
|
|
||||||
IReadOnlyDictionary<string, string> environmentVariables,
|
|
||||||
string expectedStdOut)
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var cli = Cli.Wrap("dotnet")
|
|
||||||
.SetArguments(arguments.Prepend(DummyAssembly.Location).ToArray())
|
|
||||||
.EnableExitCodeValidation()
|
|
||||||
.EnableStandardErrorValidation()
|
|
||||||
.SetStandardOutputCallback(Console.WriteLine)
|
|
||||||
.SetStandardErrorCallback(Console.WriteLine);
|
|
||||||
|
|
||||||
foreach (var (key, value) in environmentVariables)
|
|
||||||
cli.SetEnvironmentVariable(key, value);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var result = await cli.ExecuteAsync();
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
result.ExitCode.Should().Be(0);
|
|
||||||
result.StandardError.Should().BeNullOrWhiteSpace();
|
|
||||||
result.StandardOutput.TrimEnd().Should().Be(expectedStdOut);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
27
CliFx.Tests/EnvironmentVariablesSpecs.Commands.cs
Normal file
27
CliFx.Tests/EnvironmentVariablesSpecs.Commands.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class EnvironmentVariablesSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class EnvironmentVariableCollectionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
|
||||||
|
public IReadOnlyList<string>? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command]
|
||||||
|
private class EnvironmentVariableCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("opt", EnvironmentVariableName = "ENV_OPT")]
|
||||||
|
public string? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
96
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
96
CliFx.Tests/EnvironmentVariablesSpecs.cs
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Domain;
|
||||||
|
using CliWrap;
|
||||||
|
using CliWrap.Buffered;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class EnvironmentVariablesSpecs
|
||||||
|
{
|
||||||
|
// This test uses a real application to make sure environment variables are actually read correctly
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_can_use_a_specific_environment_variable_as_fallback()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var command = Cli.Wrap("dotnet")
|
||||||
|
.WithArguments(a => a
|
||||||
|
.Add(Dummy.Program.Location))
|
||||||
|
.WithEnvironmentVariables(e => e
|
||||||
|
.Set("ENV_TARGET", "Mars"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOut.TrimEnd().Should().Be("Hello Mars!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test uses a real application to make sure environment variables are actually read correctly
|
||||||
|
[Fact]
|
||||||
|
public async Task Option_only_uses_environment_variable_as_fallback_if_the_value_was_not_directly_provided()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var command = Cli.Wrap("dotnet")
|
||||||
|
.WithArguments(a => a
|
||||||
|
.Add(Dummy.Program.Location)
|
||||||
|
.Add("--target")
|
||||||
|
.Add("Jupiter"))
|
||||||
|
.WithEnvironmentVariables(e => e
|
||||||
|
.Set("ENV_TARGET", "Mars"));
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOut.TrimEnd().Should().Be("Hello Jupiter!");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Option_of_non_scalar_type_can_take_multiple_separated_values_from_an_environment_variable()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCollectionCommand)});
|
||||||
|
|
||||||
|
var input = CommandLineInput.Empty;
|
||||||
|
var envVars = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var command = schema.InitializeEntryPoint(input, envVars);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
command.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
|
||||||
|
{
|
||||||
|
Option = new[] {"foo", "bar"}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Option_of_scalar_type_can_only_take_a_single_value_from_an_environment_variable_even_if_it_contains_separators()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCommand)});
|
||||||
|
|
||||||
|
var input = CommandLineInput.Empty;
|
||||||
|
var envVars = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var command = schema.InitializeEntryPoint(input, envVars);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
command.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||||
|
{
|
||||||
|
Option = $"foo{Path.PathSeparator}bar"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
31
CliFx.Tests/ErrorReportingSpecs.Commands.cs
Normal file
31
CliFx.Tests/ErrorReportingSpecs.Commands.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
using CliFx.Exceptions;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ErrorReportingSpecs
|
||||||
|
{
|
||||||
|
[Command("exc")]
|
||||||
|
private class GenericExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("msg", 'm')]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("exc")]
|
||||||
|
private class CommandExceptionCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("code", 'c')]
|
||||||
|
public int ExitCode { get; set; } = 1337;
|
||||||
|
|
||||||
|
[CommandOption("msg", 'm')]
|
||||||
|
public string? Message { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
84
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
84
CliFx.Tests/ErrorReportingSpecs.cs
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class ErrorReportingSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_full_error_details()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(GenericExceptionCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"exc", "-m", "Kaput"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErrData.Should().Contain("Kaput");
|
||||||
|
stdErrData.Length.Should().BeGreaterThan("Kaput".Length);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_which_exits_with_custom_code_and_prints_minimal_error_details()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(CommandExceptionCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"exc", "-m", "Kaput", "-c", "69"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(69);
|
||||||
|
stdErrData.Should().Be("Kaput");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Command_may_throw_a_specialized_exception_without_error_message_which_exits_and_prints_full_error_details()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdErr = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(error: stdErr);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(CommandExceptionCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"exc", "-m", "Kaput"},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().NotBe(0);
|
||||||
|
stdErrData.Should().NotBeEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
96
CliFx.Tests/HelpTextSpecs.Commands.cs
Normal file
96
CliFx.Tests/HelpTextSpecs.Commands.cs
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class HelpTextSpecs
|
||||||
|
{
|
||||||
|
[Command(Description = "DefaultCommand description.")]
|
||||||
|
private class DefaultCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-a", 'a', Description = "OptionA description.")]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-b", 'b', Description = "OptionB description.")]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd", Description = "NamedCommand description.")]
|
||||||
|
private class NamedCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "param-a", Description = "ParameterA description.")]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-c", 'c', Description = "OptionC description.")]
|
||||||
|
public string? OptionC { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-d", 'd', Description = "OptionD description.")]
|
||||||
|
public string? OptionD { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd sub", Description = "NamedSubCommand description.")]
|
||||||
|
private class NamedSubCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "param-b", Description = "ParameterB description.")]
|
||||||
|
public string? ParameterB { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(1, Name = "param-c", Description = "ParameterC description.")]
|
||||||
|
public string? ParameterC { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-e", 'e', Description = "OptionE description.")]
|
||||||
|
public string? OptionE { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd-with-params")]
|
||||||
|
private class ParametersCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandParameter(0, Name = "first")]
|
||||||
|
public string? ParameterA { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(10)]
|
||||||
|
public int? ParameterB { get; set; }
|
||||||
|
|
||||||
|
[CommandParameter(20, Description = "A list of numbers", Name = "third list")]
|
||||||
|
public IEnumerable<int>? ParameterC { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option", 'o')]
|
||||||
|
public string? Option { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd-with-req-opts")]
|
||||||
|
private class RequiredOptionsCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-f", 'f', IsRequired = true)]
|
||||||
|
public string? OptionF { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-g", 'g', IsRequired = true)]
|
||||||
|
public IEnumerable<int>? OptionG { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-h", 'h')]
|
||||||
|
public string? OptionH { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("cmd-with-env-vars")]
|
||||||
|
private class EnvironmentVariableCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("option-a", 'a', IsRequired = true, EnvironmentVariableName = "ENV_OPT_A")]
|
||||||
|
public string? OptionA { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("option-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
|
||||||
|
public string? OptionB { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
246
CliFx.Tests/HelpTextSpecs.cs
Normal file
246
CliFx.Tests/HelpTextSpecs.cs
Normal file
@@ -0,0 +1,246 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class HelpTextSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseVersionText("v6.9")
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(new[] {"--version"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Be("v6.9");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_by_providing_the_help_option()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseTitle("AppTitle")
|
||||||
|
.UseVersionText("AppVer")
|
||||||
|
.UseDescription("AppDesc")
|
||||||
|
.UseExecutableName("AppExe")
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"AppTitle", "AppVer",
|
||||||
|
"AppDesc",
|
||||||
|
"Usage",
|
||||||
|
"AppExe", "[command]", "[options]",
|
||||||
|
"Options",
|
||||||
|
"-a|--option-a", "OptionA description.",
|
||||||
|
"-b|--option-b", "OptionB description.",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"--version", "Shows version information.",
|
||||||
|
"Commands",
|
||||||
|
"cmd", "NamedCommand description.",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_on_a_specific_named_command()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Description",
|
||||||
|
"NamedCommand description.",
|
||||||
|
"Usage",
|
||||||
|
"cmd", "[command]", "<param-a>", "[options]",
|
||||||
|
"Parameters",
|
||||||
|
"* param-a", "ParameterA description.",
|
||||||
|
"Options",
|
||||||
|
"-c|--option-c", "OptionC description.",
|
||||||
|
"-d|--option-d", "OptionD description.",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"Commands",
|
||||||
|
"sub", "SubCommand description.",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_on_a_specific_named_sub_command()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd", "sub", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Description",
|
||||||
|
"SubCommand description.",
|
||||||
|
"Usage",
|
||||||
|
"cmd sub", "<param-b>", "<param-c>", "[options]",
|
||||||
|
"Parameters",
|
||||||
|
"* param-b", "ParameterB description.",
|
||||||
|
"* param-c", "ParameterC description.",
|
||||||
|
"Options",
|
||||||
|
"-e|--option-e", "OptionE description.",
|
||||||
|
"-h|--help", "Shows help text."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_can_be_requested_without_specifying_command_even_if_default_command_is_not_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(NamedCommand))
|
||||||
|
.AddCommand(typeof(NamedSubCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"[command]",
|
||||||
|
"Options",
|
||||||
|
"-h|--help", "Shows help text.",
|
||||||
|
"--version", "Shows version information.",
|
||||||
|
"Commands",
|
||||||
|
"cmd", "NamedCommand description.",
|
||||||
|
"You can run", "to show help on a specific command."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ParametersCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd-with-params", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_shows_usage_format_which_lists_all_required_options()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(RequiredOptionsCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd-with-req-opts", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Usage",
|
||||||
|
"cmd-with-req-opts", "--option-f <value>", "--option-g <values...>", "[options]",
|
||||||
|
"Options",
|
||||||
|
"* -f|--option-f",
|
||||||
|
"* -g|--option-g",
|
||||||
|
"-h|--option-h"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_lists_environment_variable_names_for_options_that_have_them_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(EnvironmentVariableCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
await application.RunAsync(new[] {"cmd-with-env-vars", "--help"});
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(
|
||||||
|
"Options",
|
||||||
|
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
||||||
|
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
51
CliFx.Tests/RoutingSpecs.Commands.cs
Normal file
51
CliFx.Tests/RoutingSpecs.Commands.cs
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using CliFx.Attributes;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class RoutingSpecs
|
||||||
|
{
|
||||||
|
[Command]
|
||||||
|
private class DefaultCommand : ICommand
|
||||||
|
{
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine("Hello world!");
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("concat", Description = "Concatenate strings.")]
|
||||||
|
private class ConcatCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
||||||
|
public IReadOnlyList<string> Inputs { get; set; }
|
||||||
|
|
||||||
|
[CommandOption('s', Description = "String separator.")]
|
||||||
|
public string Separator { get; set; } = "";
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(string.Join(Separator, Inputs));
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Command("div", Description = "Divide one number by another.")]
|
||||||
|
private class DivideCommand : ICommand
|
||||||
|
{
|
||||||
|
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
||||||
|
public double Dividend { get; set; }
|
||||||
|
|
||||||
|
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
||||||
|
public double Divisor { get; set; }
|
||||||
|
|
||||||
|
public ValueTask ExecuteAsync(IConsole console)
|
||||||
|
{
|
||||||
|
console.Output.WriteLine(Dividend / Divisor);
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
90
CliFx.Tests/RoutingSpecs.cs
Normal file
90
CliFx.Tests/RoutingSpecs.cs
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public partial class RoutingSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public async Task Default_command_is_executed_if_provided_arguments_do_not_match_any_named_command()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(ConcatCommand))
|
||||||
|
.AddCommand(typeof(DivideCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
Array.Empty<string>(),
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Be("Hello world!");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Help_text_is_printed_if_no_arguments_were_provided_and_default_command_is_not_defined()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(ConcatCommand))
|
||||||
|
.AddCommand(typeof(DivideCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.UseDescription("This will be visible in help")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
Array.Empty<string>(),
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Contain("This will be visible in help");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Specific_named_command_is_executed_if_provided_arguments_match_its_name()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
await using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var application = new CliApplicationBuilder()
|
||||||
|
.AddCommand(typeof(DefaultCommand))
|
||||||
|
.AddCommand(typeof(ConcatCommand))
|
||||||
|
.AddCommand(typeof(DivideCommand))
|
||||||
|
.UseConsole(console)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var exitCode = await application.RunAsync(
|
||||||
|
new[] {"concat", "-i", "foo", "bar", "-s", ", "},
|
||||||
|
new Dictionary<string, string>());
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
exitCode.Should().Be(0);
|
||||||
|
stdOutData.Should().Be("foo, bar");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
using System;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class SystemConsoleTests
|
|
||||||
{
|
|
||||||
[TearDown]
|
|
||||||
public void TearDown()
|
|
||||||
{
|
|
||||||
// Reset console color so it doesn't carry on into the next tests
|
|
||||||
Console.ResetColor();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test(Description = "Must be in sync with system console")]
|
|
||||||
public void Smoke_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var console = new SystemConsole();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
console.ResetColor();
|
|
||||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
|
||||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
console.Input.Should().BeSameAs(Console.In);
|
|
||||||
console.IsInputRedirected.Should().Be(Console.IsInputRedirected);
|
|
||||||
console.Output.Should().BeSameAs(Console.Out);
|
|
||||||
console.IsOutputRedirected.Should().Be(Console.IsOutputRedirected);
|
|
||||||
console.Error.Should().BeSameAs(Console.Error);
|
|
||||||
console.IsErrorRedirected.Should().Be(Console.IsErrorRedirected);
|
|
||||||
console.ForegroundColor.Should().Be(Console.ForegroundColor);
|
|
||||||
console.BackgroundColor.Should().Be(Console.BackgroundColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("allrequired", Description = "AllRequiredOptionsCommand description.")]
|
|
||||||
public class AllRequiredOptionsCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("option-f", 'f', IsRequired = true, Description = "OptionF description.")]
|
|
||||||
public string? OptionF { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option-g", 'g', IsRequired = true, Description = "OptionG description.")]
|
|
||||||
public string? OptionFG { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,126 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Tests.TestCustomTypes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class AllSupportedTypesCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption(nameof(Object))]
|
|
||||||
public object? Object { get; set; } = 42;
|
|
||||||
|
|
||||||
[CommandOption(nameof(String))]
|
|
||||||
public string? String { get; set; } = "foo bar";
|
|
||||||
|
|
||||||
[CommandOption(nameof(Bool))]
|
|
||||||
public bool Bool { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Char))]
|
|
||||||
public char Char { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Sbyte))]
|
|
||||||
public sbyte Sbyte { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Byte))]
|
|
||||||
public byte Byte { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Short))]
|
|
||||||
public short Short { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Ushort))]
|
|
||||||
public ushort Ushort { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Int))]
|
|
||||||
public int Int { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Uint))]
|
|
||||||
public uint Uint { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Long))]
|
|
||||||
public long Long { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Ulong))]
|
|
||||||
public ulong Ulong { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Float))]
|
|
||||||
public float Float { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Double))]
|
|
||||||
public double Double { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Decimal))]
|
|
||||||
public decimal Decimal { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(DateTime))]
|
|
||||||
public DateTime DateTime { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(DateTimeOffset))]
|
|
||||||
public DateTimeOffset DateTimeOffset { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TimeSpan))]
|
|
||||||
public TimeSpan TimeSpan { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TestEnum))]
|
|
||||||
public TestEnum TestEnum { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(IntNullable))]
|
|
||||||
public int? IntNullable { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TestEnumNullable))]
|
|
||||||
public TestEnum? TestEnumNullable { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TimeSpanNullable))]
|
|
||||||
public TimeSpan? TimeSpanNullable { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TestStringConstructable))]
|
|
||||||
public TestStringConstructable? TestStringConstructable { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TestStringParseable))]
|
|
||||||
public TestStringParseable? TestStringParseable { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TestStringParseableWithFormatProvider))]
|
|
||||||
public TestStringParseableWithFormatProvider? TestStringParseableWithFormatProvider { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(ObjectArray))]
|
|
||||||
public object[]? ObjectArray { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(StringArray))]
|
|
||||||
public string[]? StringArray { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(IntArray))]
|
|
||||||
public int[]? IntArray { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TestEnumArray))]
|
|
||||||
public TestEnum[]? TestEnumArray { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(IntNullableArray))]
|
|
||||||
public int?[]? IntNullableArray { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(TestStringConstructableArray))]
|
|
||||||
public TestStringConstructable[]? TestStringConstructableArray { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(Enumerable))]
|
|
||||||
public IEnumerable? Enumerable { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(StringEnumerable))]
|
|
||||||
public IEnumerable<string>? StringEnumerable { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(StringReadOnlyList))]
|
|
||||||
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(StringList))]
|
|
||||||
public List<string>? StringList { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(StringHashSet))]
|
|
||||||
public HashSet<string>? StringHashSet { get; set; }
|
|
||||||
|
|
||||||
[CommandOption(nameof(NonConvertible))]
|
|
||||||
public TestNonStringParseable? NonConvertible { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Tests.TestCustomTypes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class BrokenEnumerableCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandParameter(0)]
|
|
||||||
public TestCustomEnumerable<string>? Test { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("cancel")]
|
|
||||||
public class CancellableCommand : ICommand
|
|
||||||
{
|
|
||||||
public async ValueTask ExecuteAsync(IConsole console)
|
|
||||||
{
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(3), console.GetCancellationToken());
|
|
||||||
console.Output.WriteLine("Never printed");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
using CliFx.Exceptions;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("exc")]
|
|
||||||
public class CommandExceptionCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("code", 'c')]
|
|
||||||
public int ExitCode { get; set; } = 1337;
|
|
||||||
|
|
||||||
[CommandOption("msg", 'm')]
|
|
||||||
public string? Message { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("concat", Description = "Concatenate strings.")]
|
|
||||||
public class ConcatCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
|
||||||
public IReadOnlyList<string> Inputs { get; set; }
|
|
||||||
|
|
||||||
[CommandOption('s', Description = "String separator.")]
|
|
||||||
public string Separator { get; set; } = "";
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console)
|
|
||||||
{
|
|
||||||
console.Output.WriteLine(string.Join(Separator, Inputs));
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("div", Description = "Divide one number by another.")]
|
|
||||||
public class DivideCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
|
||||||
public double Dividend { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
|
||||||
public double Divisor { get; set; }
|
|
||||||
|
|
||||||
// This property should be ignored by resolver
|
|
||||||
public bool NotAnOption { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console)
|
|
||||||
{
|
|
||||||
console.Output.WriteLine(Dividend / Divisor);
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
|
||||||
public string? OptionA { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
|
||||||
public string? OptionB { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class DuplicateOptionNamesCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("fruits")]
|
|
||||||
public string? Apples { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("fruits")]
|
|
||||||
public string? Oranges { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class DuplicateOptionShortNamesCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption('x')]
|
|
||||||
public string? OptionA { get; set; }
|
|
||||||
|
|
||||||
[CommandOption('x')]
|
|
||||||
public string? OptionB { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class DuplicateParameterNameCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandParameter(0, Name = "param")]
|
|
||||||
public string? ParameterA { get; set; }
|
|
||||||
|
|
||||||
[CommandParameter(1, Name = "param")]
|
|
||||||
public string? ParameterB { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class DuplicateParameterOrderCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandParameter(13)]
|
|
||||||
public string? ParameterA { get; set; }
|
|
||||||
|
|
||||||
[CommandParameter(13)]
|
|
||||||
public string? ParameterB { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command(Description = "Reads option values from environment variables.")]
|
|
||||||
public class EnvironmentVariableCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("opt", EnvironmentVariableName = "ENV_SINGLE_VALUE")]
|
|
||||||
public string? Option { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command(Description = "Reads multiple option values from environment variables.")]
|
|
||||||
public class EnvironmentVariableWithMultipleValuesCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("opt", EnvironmentVariableName = "ENV_MULTIPLE_VALUES")]
|
|
||||||
public IEnumerable<string>? Option { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command(Description = "Reads one option value from environment variables because target property is not a collection.")]
|
|
||||||
public class EnvironmentVariableWithoutCollectionPropertyCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("opt", EnvironmentVariableName = "ENV_MULTIPLE_VALUES")]
|
|
||||||
public string? Option { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("exc")]
|
|
||||||
public class ExceptionCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("msg", 'm')]
|
|
||||||
public string? Message { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class HelloWorldDefaultCommand : ICommand
|
|
||||||
{
|
|
||||||
public ValueTask ExecuteAsync(IConsole console)
|
|
||||||
{
|
|
||||||
console.Output.WriteLine("Hello world.");
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command(Description = "HelpDefaultCommand description.")]
|
|
||||||
public class HelpDefaultCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("option-a", 'a', Description = "OptionA description.")]
|
|
||||||
public string? OptionA { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option-b", 'b', Description = "OptionB description.")]
|
|
||||||
public string? OptionB { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("cmd", Description = "HelpNamedCommand description.")]
|
|
||||||
public class HelpNamedCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("option-c", 'c', Description = "OptionC description.")]
|
|
||||||
public string? OptionC { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option-d", 'd', Description = "OptionD description.")]
|
|
||||||
public string? OptionD { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("cmd sub", Description = "HelpSubCommand description.")]
|
|
||||||
public class HelpSubCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("option-e", 'e', Description = "OptionE description.")]
|
|
||||||
public string? OptionE { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class MultipleNonScalarParametersCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandParameter(0)]
|
|
||||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
|
||||||
|
|
||||||
[CommandParameter(1)]
|
|
||||||
public IReadOnlyList<string>? ParameterB { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
public class NonAnnotatedCommand : ICommand
|
|
||||||
{
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class NonImplementedCommand
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command]
|
|
||||||
public class NonLastNonScalarParameterCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandParameter(0)]
|
|
||||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
|
||||||
|
|
||||||
[CommandParameter(1)]
|
|
||||||
public string? ParameterB { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("param cmd", Description = "Command using positional parameters")]
|
|
||||||
public class ParameterCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandParameter(0, Name = "first")]
|
|
||||||
public string? ParameterA { get; set; }
|
|
||||||
|
|
||||||
[CommandParameter(10)]
|
|
||||||
public int? ParameterB { get; set; }
|
|
||||||
|
|
||||||
[CommandParameter(20, Description = "A list of numbers", Name = "third list")]
|
|
||||||
public IEnumerable<int>? ParameterC { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option", 'o')]
|
|
||||||
public string? OptionA { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("param cmd2", Description = "Command using positional parameters")]
|
|
||||||
public class SimpleParameterCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandParameter(0, Name = "first")]
|
|
||||||
public string? ParameterA { get; set; }
|
|
||||||
|
|
||||||
[CommandParameter(10)]
|
|
||||||
public int? ParameterB { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option", 'o')]
|
|
||||||
public string? OptionA { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System.Threading.Tasks;
|
|
||||||
using CliFx.Attributes;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCommands
|
|
||||||
{
|
|
||||||
[Command("somerequired", Description = "SomeRequiredOptionsCommand description.")]
|
|
||||||
public class SomeRequiredOptionsCommand : ICommand
|
|
||||||
{
|
|
||||||
[CommandOption("option-f", 'f', IsRequired = true, Description = "OptionF description.")]
|
|
||||||
public string? OptionF { get; set; }
|
|
||||||
|
|
||||||
[CommandOption("option-g", 'g', Description = "OptionG description.")]
|
|
||||||
public string? OptionFG { get; set; }
|
|
||||||
|
|
||||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCustomTypes
|
|
||||||
{
|
|
||||||
public class TestCustomEnumerable<T> : IEnumerable<T>
|
|
||||||
{
|
|
||||||
private readonly T[] _arr = new T[0];
|
|
||||||
|
|
||||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) _arr).GetEnumerator();
|
|
||||||
|
|
||||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
namespace CliFx.Tests.TestCustomTypes
|
|
||||||
{
|
|
||||||
public enum TestEnum
|
|
||||||
{
|
|
||||||
Value1,
|
|
||||||
Value2,
|
|
||||||
Value3
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace CliFx.Tests.TestCustomTypes
|
|
||||||
{
|
|
||||||
public class TestNonStringParseable
|
|
||||||
{
|
|
||||||
public int Value { get; }
|
|
||||||
|
|
||||||
public TestNonStringParseable(int value)
|
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
namespace CliFx.Tests.TestCustomTypes
|
|
||||||
{
|
|
||||||
public class TestStringConstructable
|
|
||||||
{
|
|
||||||
public string Value { get; }
|
|
||||||
|
|
||||||
public TestStringConstructable(string value)
|
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
namespace CliFx.Tests.TestCustomTypes
|
|
||||||
{
|
|
||||||
public class TestStringParseable
|
|
||||||
{
|
|
||||||
public string Value { get; }
|
|
||||||
|
|
||||||
private TestStringParseable(string value)
|
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TestStringParseable Parse(string value) => new TestStringParseable(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.TestCustomTypes
|
|
||||||
{
|
|
||||||
public class TestStringParseableWithFormatProvider
|
|
||||||
{
|
|
||||||
public string Value { get; }
|
|
||||||
|
|
||||||
private TestStringParseableWithFormatProvider(string value)
|
|
||||||
{
|
|
||||||
Value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TestStringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
|
||||||
new TestStringParseableWithFormatProvider(value + " " + formatProvider);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using CliFx.Utilities;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests.Utilities
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class ProgressTickerTests
|
|
||||||
{
|
|
||||||
[Test]
|
|
||||||
public void Report_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
var formatProvider = CultureInfo.InvariantCulture;
|
|
||||||
|
|
||||||
using var stdout = new StringWriter(formatProvider);
|
|
||||||
|
|
||||||
var console = new VirtualConsole(TextReader.Null, false, stdout, false, TextWriter.Null, false);
|
|
||||||
var ticker = console.CreateProgressTicker();
|
|
||||||
|
|
||||||
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
|
||||||
var progressStringValues = progressValues.Select(p => p.ToString("P2", formatProvider)).ToArray();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
foreach (var progress in progressValues)
|
|
||||||
ticker.Report(progress);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
stdout.ToString().Should().ContainAll(progressStringValues);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void Report_Redirected_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
using var stdout = new StringWriter();
|
|
||||||
|
|
||||||
var console = new VirtualConsole(stdout);
|
|
||||||
var ticker = console.CreateProgressTicker();
|
|
||||||
|
|
||||||
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
|
||||||
|
|
||||||
// Act
|
|
||||||
foreach (var progress in progressValues)
|
|
||||||
ticker.Report(progress);
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
stdout.ToString().Should().BeEmpty();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
54
CliFx.Tests/UtilitiesSpecs.cs
Normal file
54
CliFx.Tests/UtilitiesSpecs.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using CliFx.Utilities;
|
||||||
|
using FluentAssertions;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace CliFx.Tests
|
||||||
|
{
|
||||||
|
public class UtilitiesSpecs
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Progress_ticker_can_be_used_to_report_progress_to_console()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut, isOutputRedirected: false);
|
||||||
|
|
||||||
|
var ticker = console.CreateProgressTicker();
|
||||||
|
|
||||||
|
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||||
|
var progressStringValues = progressValues.Select(p => p.ToString("P2")).ToArray();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
foreach (var progress in progressValues)
|
||||||
|
ticker.Report(progress);
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().ContainAll(progressStringValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Progress_ticker_does_not_write_to_console_if_output_is_redirected()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
using var stdOut = new MemoryStream();
|
||||||
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
|
var ticker = console.CreateProgressTicker();
|
||||||
|
|
||||||
|
var progressValues = Enumerable.Range(0, 100).Select(p => p / 100.0).ToArray();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
foreach (var progress in progressValues)
|
||||||
|
ticker.Report(progress);
|
||||||
|
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
stdOutData.Should().BeEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.IO;
|
|
||||||
using FluentAssertions;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace CliFx.Tests
|
|
||||||
{
|
|
||||||
[TestFixture]
|
|
||||||
public class VirtualConsoleTests
|
|
||||||
{
|
|
||||||
[Test(Description = "Must not leak to system console")]
|
|
||||||
public void Smoke_Test()
|
|
||||||
{
|
|
||||||
// Arrange
|
|
||||||
using var stdin = new StringReader("hello world");
|
|
||||||
using var stdout = new StringWriter();
|
|
||||||
using var stderr = new StringWriter();
|
|
||||||
|
|
||||||
var console = new VirtualConsole(stdin, stdout, stderr);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
console.ResetColor();
|
|
||||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
|
||||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
|
||||||
|
|
||||||
// Assert
|
|
||||||
console.Input.Should().BeSameAs(stdin);
|
|
||||||
console.Input.Should().NotBeSameAs(Console.In);
|
|
||||||
console.IsInputRedirected.Should().BeTrue();
|
|
||||||
console.Output.Should().BeSameAs(stdout);
|
|
||||||
console.Output.Should().NotBeSameAs(Console.Out);
|
|
||||||
console.IsOutputRedirected.Should().BeTrue();
|
|
||||||
console.Error.Should().BeSameAs(stderr);
|
|
||||||
console.Error.Should().NotBeSameAs(Console.Error);
|
|
||||||
console.IsErrorRedirected.Should().BeTrue();
|
|
||||||
console.ForegroundColor.Should().NotBe(Console.ForegroundColor);
|
|
||||||
console.BackgroundColor.Should().NotBe(Console.BackgroundColor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
5
CliFx.Tests/xunit.runner.json
Normal file
5
CliFx.Tests/xunit.runner.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://xunit.net/schema/current/xunit.runner.schema.json",
|
||||||
|
"methodDisplayOptions": "all",
|
||||||
|
"methodDisplay": "method"
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project>
|
<Project>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<Version>1.0</Version>
|
<Version>1.1</Version>
|
||||||
<Company>Tyrrrz</Company>
|
<Company>Tyrrrz</Company>
|
||||||
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
|
|||||||
@@ -12,13 +12,15 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Command line application facade.
|
/// Command line application facade.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public partial class CliApplication
|
public class CliApplication
|
||||||
{
|
{
|
||||||
private readonly ApplicationMetadata _metadata;
|
private readonly ApplicationMetadata _metadata;
|
||||||
private readonly ApplicationConfiguration _configuration;
|
private readonly ApplicationConfiguration _configuration;
|
||||||
private readonly IConsole _console;
|
private readonly IConsole _console;
|
||||||
private readonly ITypeActivator _typeActivator;
|
private readonly ITypeActivator _typeActivator;
|
||||||
|
|
||||||
|
private readonly HelpTextWriter _helpTextWriter;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes an instance of <see cref="CliApplication"/>.
|
/// Initializes an instance of <see cref="CliApplication"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -30,6 +32,8 @@ namespace CliFx
|
|||||||
_configuration = configuration;
|
_configuration = configuration;
|
||||||
_console = console;
|
_console = console;
|
||||||
_typeActivator = typeActivator;
|
_typeActivator = typeActivator;
|
||||||
|
|
||||||
|
_helpTextWriter = new HelpTextWriter(metadata, console);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async ValueTask<int?> HandleDebugDirectiveAsync(CommandLineInput commandLineInput)
|
private async ValueTask<int?> HandleDebugDirectiveAsync(CommandLineInput commandLineInput)
|
||||||
@@ -67,7 +71,7 @@ namespace CliFx
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parameters
|
// Parameters
|
||||||
foreach (var parameter in commandLineInput.Arguments.Skip(argumentOffset))
|
foreach (var parameter in commandLineInput.UnboundArguments.Skip(argumentOffset))
|
||||||
{
|
{
|
||||||
_console.Output.Write('<');
|
_console.Output.Write('<');
|
||||||
|
|
||||||
@@ -98,7 +102,7 @@ namespace CliFx
|
|||||||
private int? HandleVersionOption(CommandLineInput commandLineInput)
|
private int? HandleVersionOption(CommandLineInput commandLineInput)
|
||||||
{
|
{
|
||||||
// Version option is available only on the default command (i.e. when arguments are not specified)
|
// Version option is available only on the default command (i.e. when arguments are not specified)
|
||||||
var shouldRenderVersion = !commandLineInput.Arguments.Any() && commandLineInput.IsVersionOptionSpecified;
|
var shouldRenderVersion = !commandLineInput.UnboundArguments.Any() && commandLineInput.IsVersionOptionSpecified;
|
||||||
if (!shouldRenderVersion)
|
if (!shouldRenderVersion)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
@@ -112,7 +116,7 @@ namespace CliFx
|
|||||||
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
|
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
|
||||||
var shouldRenderHelp =
|
var shouldRenderHelp =
|
||||||
commandLineInput.IsHelpOptionSpecified ||
|
commandLineInput.IsHelpOptionSpecified ||
|
||||||
!applicationSchema.Commands.Any(c => c.IsDefault) && !commandLineInput.Arguments.Any() && !commandLineInput.Options.Any();
|
!applicationSchema.Commands.Any(c => c.IsDefault) && !commandLineInput.UnboundArguments.Any() && !commandLineInput.Options.Any();
|
||||||
|
|
||||||
if (!shouldRenderHelp)
|
if (!shouldRenderHelp)
|
||||||
return null;
|
return null;
|
||||||
@@ -122,7 +126,7 @@ namespace CliFx
|
|||||||
applicationSchema.TryFindCommand(commandLineInput) ??
|
applicationSchema.TryFindCommand(commandLineInput) ??
|
||||||
CommandSchema.StubDefaultCommand;
|
CommandSchema.StubDefaultCommand;
|
||||||
|
|
||||||
RenderHelp(applicationSchema, commandSchema);
|
_helpTextWriter.Write(applicationSchema, commandSchema);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace CliFx
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds commands from the specified assembly to the application.
|
/// Adds commands from the specified assembly to the application.
|
||||||
/// Only the public types are added.
|
/// Only adds public valid command types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CliApplicationBuilder AddCommandsFrom(Assembly commandAssembly)
|
public CliApplicationBuilder AddCommandsFrom(Assembly commandAssembly)
|
||||||
{
|
{
|
||||||
@@ -58,7 +58,7 @@ namespace CliFx
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds commands from the specified assemblies to the application.
|
/// Adds commands from the specified assemblies to the application.
|
||||||
/// Only the public types are added.
|
/// Only adds public valid command types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CliApplicationBuilder AddCommandsFrom(IEnumerable<Assembly> commandAssemblies)
|
public CliApplicationBuilder AddCommandsFrom(IEnumerable<Assembly> commandAssemblies)
|
||||||
{
|
{
|
||||||
@@ -70,7 +70,7 @@ namespace CliFx
|
|||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds commands from the calling assembly to the application.
|
/// Adds commands from the calling assembly to the application.
|
||||||
/// Only the public types are added.
|
/// Only adds public valid command types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public CliApplicationBuilder AddCommandsFromThisAssembly() => AddCommandsFrom(Assembly.GetCallingAssembly());
|
public CliApplicationBuilder AddCommandsFromThisAssembly() => AddCommandsFrom(Assembly.GetCallingAssembly());
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,9 @@
|
|||||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||||
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
|
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
|
||||||
</AssemblyAttribute>
|
</AssemblyAttribute>
|
||||||
|
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||||
|
<_Parameter1>$(AssemblyName).Analyzers</_Parameter1>
|
||||||
|
</AssemblyAttribute>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -31,7 +34,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
|
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
|
||||||
<PackageReference Include="Nullable" Version="1.2.0" PrivateAssets="all" />
|
<PackageReference Include="Nullable" Version="1.2.1" PrivateAssets="all" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net45'">
|
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' or '$(TargetFramework)' == 'net45'">
|
||||||
|
|||||||
@@ -47,9 +47,9 @@ namespace CliFx.Domain
|
|||||||
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset)
|
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset)
|
||||||
{
|
{
|
||||||
// Try to find the command that contains the most of the input arguments in its name
|
// Try to find the command that contains the most of the input arguments in its name
|
||||||
for (var i = commandLineInput.Arguments.Count; i >= 0; i--)
|
for (var i = commandLineInput.UnboundArguments.Count; i >= 0; i--)
|
||||||
{
|
{
|
||||||
var potentialCommandName = string.Join(" ", commandLineInput.Arguments.Take(i));
|
var potentialCommandName = string.Join(" ", commandLineInput.UnboundArguments.Take(i));
|
||||||
var matchingCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialCommandName));
|
var matchingCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialCommandName));
|
||||||
|
|
||||||
if (matchingCommand != null)
|
if (matchingCommand != null)
|
||||||
@@ -75,15 +75,25 @@ namespace CliFx.Domain
|
|||||||
if (command == null)
|
if (command == null)
|
||||||
{
|
{
|
||||||
throw new CliFxException(
|
throw new CliFxException(
|
||||||
$"Can't find a command that matches arguments [{string.Join(" ", commandLineInput.Arguments)}].");
|
$"Can't find a command that matches arguments [{string.Join(" ", commandLineInput.UnboundArguments)}].");
|
||||||
}
|
}
|
||||||
|
|
||||||
var parameterInputs = argumentOffset == 0
|
var parameterValues = argumentOffset == 0
|
||||||
? commandLineInput.Arguments
|
? commandLineInput.UnboundArguments.Select(a => a.Value).ToArray()
|
||||||
: commandLineInput.Arguments.Skip(argumentOffset).ToArray();
|
: commandLineInput.UnboundArguments.Skip(argumentOffset).Select(a => a.Value).ToArray();
|
||||||
|
|
||||||
return command.CreateInstance(parameterInputs, commandLineInput.Options, environmentVariables, activator);
|
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
|
internal partial class ApplicationSchema
|
||||||
|
|||||||
20
CliFx/Domain/CommandDirectiveInput.cs
Normal file
20
CliFx/Domain/CommandDirectiveInput.cs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal class CommandDirectiveInput
|
||||||
|
{
|
||||||
|
public string Name { get; }
|
||||||
|
|
||||||
|
public bool IsDebugDirective => string.Equals(Name, "debug", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public bool IsPreviewDirective => string.Equals(Name, "preview", StringComparison.OrdinalIgnoreCase);
|
||||||
|
|
||||||
|
public CommandDirectiveInput(string name)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => $"[{Name}]";
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,49 +8,30 @@ namespace CliFx.Domain
|
|||||||
{
|
{
|
||||||
internal partial class CommandLineInput
|
internal partial class CommandLineInput
|
||||||
{
|
{
|
||||||
public IReadOnlyList<string> Directives { get; }
|
public IReadOnlyList<CommandDirectiveInput> Directives { get; }
|
||||||
|
|
||||||
public IReadOnlyList<string> Arguments { get; }
|
public IReadOnlyList<CommandUnboundArgumentInput> UnboundArguments { get; }
|
||||||
|
|
||||||
public IReadOnlyList<CommandOptionInput> Options { get; }
|
public IReadOnlyList<CommandOptionInput> Options { get; }
|
||||||
|
|
||||||
public bool IsDebugDirectiveSpecified => Directives.Contains("debug", StringComparer.OrdinalIgnoreCase);
|
public bool IsDebugDirectiveSpecified => Directives.Any(d => d.IsDebugDirective);
|
||||||
|
|
||||||
public bool IsPreviewDirectiveSpecified => Directives.Contains("preview", StringComparer.OrdinalIgnoreCase);
|
public bool IsPreviewDirectiveSpecified => Directives.Any(d => d.IsPreviewDirective);
|
||||||
|
|
||||||
public bool IsHelpOptionSpecified =>
|
public bool IsHelpOptionSpecified => Options.Any(o => o.IsHelpOption);
|
||||||
Options.Any(o => CommandOptionSchema.HelpOption.MatchesNameOrShortName(o.Alias));
|
|
||||||
|
|
||||||
public bool IsVersionOptionSpecified =>
|
public bool IsVersionOptionSpecified => Options.Any(o => o.IsVersionOption);
|
||||||
Options.Any(o => CommandOptionSchema.VersionOption.MatchesNameOrShortName(o.Alias));
|
|
||||||
|
|
||||||
public CommandLineInput(
|
public CommandLineInput(
|
||||||
IReadOnlyList<string> directives,
|
IReadOnlyList<CommandDirectiveInput> directives,
|
||||||
IReadOnlyList<string> arguments,
|
IReadOnlyList<CommandUnboundArgumentInput> unboundArguments,
|
||||||
IReadOnlyList<CommandOptionInput> options)
|
IReadOnlyList<CommandOptionInput> options)
|
||||||
{
|
{
|
||||||
Directives = directives;
|
Directives = directives;
|
||||||
Arguments = arguments;
|
UnboundArguments = unboundArguments;
|
||||||
Options = options;
|
Options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandLineInput(
|
|
||||||
IReadOnlyList<string> arguments,
|
|
||||||
IReadOnlyList<CommandOptionInput> options)
|
|
||||||
: this(new string[0], arguments, options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandLineInput(IReadOnlyList<string> arguments)
|
|
||||||
: this(arguments, new CommandOptionInput[0])
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandLineInput(IReadOnlyList<CommandOptionInput> options)
|
|
||||||
: this(new string[0], options)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
var buffer = new StringBuilder();
|
var buffer = new StringBuilder();
|
||||||
@@ -58,13 +39,10 @@ namespace CliFx.Domain
|
|||||||
foreach (var directive in Directives)
|
foreach (var directive in Directives)
|
||||||
{
|
{
|
||||||
buffer.AppendIfNotEmpty(' ');
|
buffer.AppendIfNotEmpty(' ');
|
||||||
buffer
|
buffer.Append(directive);
|
||||||
.Append('[')
|
|
||||||
.Append(directive)
|
|
||||||
.Append(']');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var argument in Arguments)
|
foreach (var argument in UnboundArguments)
|
||||||
{
|
{
|
||||||
buffer.AppendIfNotEmpty(' ');
|
buffer.AppendIfNotEmpty(' ');
|
||||||
buffer.Append(argument);
|
buffer.Append(argument);
|
||||||
@@ -84,16 +62,14 @@ namespace CliFx.Domain
|
|||||||
{
|
{
|
||||||
public static CommandLineInput Parse(IReadOnlyList<string> commandLineArguments)
|
public static CommandLineInput Parse(IReadOnlyList<string> commandLineArguments)
|
||||||
{
|
{
|
||||||
var directives = new List<string>();
|
var builder = new CommandLineInputBuilder();
|
||||||
var arguments = new List<string>();
|
|
||||||
var optionsDic = new Dictionary<string, List<string>>();
|
|
||||||
|
|
||||||
// Option aliases and values are parsed in pairs so we need to keep track of last alias
|
var currentOptionAlias = "";
|
||||||
var lastOptionAlias = "";
|
var currentOptionValues = new List<string>();
|
||||||
|
|
||||||
bool TryParseDirective(string argument)
|
bool TryParseDirective(string argument)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(lastOptionAlias))
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!argument.StartsWith("[", StringComparison.OrdinalIgnoreCase) ||
|
if (!argument.StartsWith("[", StringComparison.OrdinalIgnoreCase) ||
|
||||||
@@ -101,17 +77,17 @@ namespace CliFx.Domain
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
var directive = argument.Substring(1, argument.Length - 2);
|
var directive = argument.Substring(1, argument.Length - 2);
|
||||||
directives.Add(directive);
|
builder.AddDirective(directive);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TryParseArgument(string argument)
|
bool TryParseArgument(string argument)
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrWhiteSpace(lastOptionAlias))
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
arguments.Add(argument);
|
builder.AddUnboundArgument(argument);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -121,10 +97,11 @@ namespace CliFx.Domain
|
|||||||
if (!argument.StartsWith("--", StringComparison.OrdinalIgnoreCase))
|
if (!argument.StartsWith("--", StringComparison.OrdinalIgnoreCase))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lastOptionAlias = argument.Substring(2);
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||||
|
|
||||||
if (!optionsDic.ContainsKey(lastOptionAlias))
|
currentOptionAlias = argument.Substring(2);
|
||||||
optionsDic[lastOptionAlias] = new List<string>();
|
currentOptionValues = new List<string>();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -136,10 +113,11 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
foreach (var c in argument.Substring(1))
|
foreach (var c in argument.Substring(1))
|
||||||
{
|
{
|
||||||
lastOptionAlias = c.AsString();
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||||
|
|
||||||
if (!optionsDic.ContainsKey(lastOptionAlias))
|
currentOptionAlias = c.AsString();
|
||||||
optionsDic[lastOptionAlias] = new List<string>();
|
currentOptionValues = new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -147,10 +125,10 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
bool TryParseOptionValue(string argument)
|
bool TryParseOptionValue(string argument)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(lastOptionAlias))
|
if (string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
optionsDic[lastOptionAlias].Add(argument);
|
currentOptionValues.Add(argument);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -165,15 +143,21 @@ namespace CliFx.Domain
|
|||||||
TryParseOptionValue(argument);
|
TryParseOptionValue(argument);
|
||||||
}
|
}
|
||||||
|
|
||||||
var options = optionsDic.Select(p => new CommandOptionInput(p.Key, p.Value)).ToArray();
|
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||||
|
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||||
|
|
||||||
return new CommandLineInput(directives, arguments, options);
|
return builder.Build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal partial class CommandLineInput
|
internal partial class CommandLineInput
|
||||||
{
|
{
|
||||||
public static CommandLineInput Empty { get; } =
|
private static IReadOnlyList<CommandDirectiveInput> EmptyDirectives { get; } = new CommandDirectiveInput[0];
|
||||||
new CommandLineInput(new string[0], new string[0], new CommandOptionInput[0]);
|
|
||||||
|
private static IReadOnlyList<CommandUnboundArgumentInput> EmptyUnboundArguments { get; } = new CommandUnboundArgumentInput[0];
|
||||||
|
|
||||||
|
private static IReadOnlyList<CommandOptionInput> EmptyOptions { get; } = new CommandOptionInput[0];
|
||||||
|
|
||||||
|
public static CommandLineInput Empty { get; } = new CommandLineInput(EmptyDirectives, EmptyUnboundArguments, EmptyOptions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
43
CliFx/Domain/CommandLineInputBuilder.cs
Normal file
43
CliFx/Domain/CommandLineInputBuilder.cs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal class CommandLineInputBuilder
|
||||||
|
{
|
||||||
|
private readonly List<CommandDirectiveInput> _directives = new List<CommandDirectiveInput>();
|
||||||
|
private readonly List<CommandUnboundArgumentInput> _unboundArguments = new List<CommandUnboundArgumentInput>();
|
||||||
|
private readonly List<CommandOptionInput> _options = new List<CommandOptionInput>();
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddDirective(CommandDirectiveInput directive)
|
||||||
|
{
|
||||||
|
_directives.Add(directive);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddDirective(string directive) =>
|
||||||
|
AddDirective(new CommandDirectiveInput(directive));
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddUnboundArgument(CommandUnboundArgumentInput unboundArgument)
|
||||||
|
{
|
||||||
|
_unboundArguments.Add(unboundArgument);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddUnboundArgument(string unboundArgument) =>
|
||||||
|
AddUnboundArgument(new CommandUnboundArgumentInput(unboundArgument));
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddOption(CommandOptionInput option)
|
||||||
|
{
|
||||||
|
_options.Add(option);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddOption(string optionAlias, IReadOnlyList<string> values) =>
|
||||||
|
AddOption(new CommandOptionInput(optionAlias, values));
|
||||||
|
|
||||||
|
public CommandLineInputBuilder AddOption(string optionAlias, params string[] values) =>
|
||||||
|
AddOption(optionAlias, (IReadOnlyList<string>) values);
|
||||||
|
|
||||||
|
public CommandLineInput Build() => new CommandLineInput(_directives, _unboundArguments, _options);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,22 +10,16 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
public IReadOnlyList<string> Values { get; }
|
public IReadOnlyList<string> Values { get; }
|
||||||
|
|
||||||
|
public bool IsHelpOption => CommandOptionSchema.HelpOption.MatchesNameOrShortName(Alias);
|
||||||
|
|
||||||
|
public bool IsVersionOption => CommandOptionSchema.VersionOption.MatchesNameOrShortName(Alias);
|
||||||
|
|
||||||
public CommandOptionInput(string alias, IReadOnlyList<string> values)
|
public CommandOptionInput(string alias, IReadOnlyList<string> values)
|
||||||
{
|
{
|
||||||
Alias = alias;
|
Alias = alias;
|
||||||
Values = values;
|
Values = values;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CommandOptionInput(string alias, string value)
|
|
||||||
: this(alias, new[] {value})
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public CommandOptionInput(string alias)
|
|
||||||
: this(alias, new string[0])
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
var buffer = new StringBuilder();
|
var buffer = new StringBuilder();
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ namespace CliFx.Domain
|
|||||||
|
|
||||||
private void InjectParameters(ICommand command, IReadOnlyList<string> parameterInputs)
|
private void InjectParameters(ICommand command, IReadOnlyList<string> parameterInputs)
|
||||||
{
|
{
|
||||||
|
// All inputs must be bound
|
||||||
|
var remainingParameterInputs = parameterInputs.ToList();
|
||||||
|
|
||||||
// Scalar parameters
|
// Scalar parameters
|
||||||
var scalarParameters = Parameters
|
var scalarParameters = Parameters
|
||||||
.OrderBy(p => p.Order)
|
.OrderBy(p => p.Order)
|
||||||
@@ -57,6 +60,7 @@ namespace CliFx.Domain
|
|||||||
: throw new CliFxException($"Missing value for parameter <{scalarParameter.DisplayName}>.");
|
: throw new CliFxException($"Missing value for parameter <{scalarParameter.DisplayName}>.");
|
||||||
|
|
||||||
scalarParameter.Inject(command, scalarParameterInput);
|
scalarParameter.Inject(command, scalarParameterInput);
|
||||||
|
remainingParameterInputs.Remove(scalarParameterInput);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-scalar parameter (only one is allowed)
|
// Non-scalar parameter (only one is allowed)
|
||||||
@@ -68,6 +72,16 @@ namespace CliFx.Domain
|
|||||||
{
|
{
|
||||||
var nonScalarParameterInputs = parameterInputs.Skip(scalarParameters.Length).ToArray();
|
var nonScalarParameterInputs = parameterInputs.Skip(scalarParameters.Length).ToArray();
|
||||||
nonScalarParameter.Inject(command, nonScalarParameterInputs);
|
nonScalarParameter.Inject(command, nonScalarParameterInputs);
|
||||||
|
remainingParameterInputs.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure all inputs were bound
|
||||||
|
if (remainingParameterInputs.Any())
|
||||||
|
{
|
||||||
|
throw new CliFxException(new StringBuilder()
|
||||||
|
.AppendLine("Unrecognized parameters provided:")
|
||||||
|
.AppendBulletList(remainingParameterInputs)
|
||||||
|
.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,7 +90,10 @@ namespace CliFx.Domain
|
|||||||
IReadOnlyList<CommandOptionInput> optionInputs,
|
IReadOnlyList<CommandOptionInput> optionInputs,
|
||||||
IReadOnlyDictionary<string, string> environmentVariables)
|
IReadOnlyDictionary<string, string> environmentVariables)
|
||||||
{
|
{
|
||||||
// Keep track of required options so that we can raise an error if any of them are not set
|
// All inputs must be bound
|
||||||
|
var remainingOptionInputs = optionInputs.ToList();
|
||||||
|
|
||||||
|
// All required options must be set
|
||||||
var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();
|
var unsetRequiredOptions = Options.Where(o => o.IsRequired).ToList();
|
||||||
|
|
||||||
// Environment variables
|
// Environment variables
|
||||||
@@ -95,18 +112,26 @@ namespace CliFx.Domain
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: refactor this part? I wrote this while sick
|
||||||
// Direct input
|
// Direct input
|
||||||
foreach (var optionInput in optionInputs)
|
foreach (var option in Options)
|
||||||
{
|
{
|
||||||
var option = Options.FirstOrDefault(o => o.MatchesNameOrShortName(optionInput.Alias));
|
var inputs = optionInputs
|
||||||
|
.Where(i => option.MatchesNameOrShortName(i.Alias))
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
if (option != null)
|
if (inputs.Any())
|
||||||
{
|
{
|
||||||
option.Inject(command, optionInput.Values);
|
option.Inject(command, inputs.SelectMany(i => i.Values).ToArray());
|
||||||
|
|
||||||
|
foreach (var input in inputs)
|
||||||
|
remainingOptionInputs.Remove(input);
|
||||||
|
|
||||||
unsetRequiredOptions.Remove(option);
|
unsetRequiredOptions.Remove(option);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure all required options were set
|
||||||
if (unsetRequiredOptions.Any())
|
if (unsetRequiredOptions.Any())
|
||||||
{
|
{
|
||||||
throw new CliFxException(new StringBuilder()
|
throw new CliFxException(new StringBuilder()
|
||||||
@@ -114,6 +139,15 @@ namespace CliFx.Domain
|
|||||||
.AppendBulletList(unsetRequiredOptions.Select(o => o.DisplayName))
|
.AppendBulletList(unsetRequiredOptions.Select(o => o.DisplayName))
|
||||||
.ToString());
|
.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ensure all inputs were bound
|
||||||
|
if (remainingOptionInputs.Any())
|
||||||
|
{
|
||||||
|
throw new CliFxException(new StringBuilder()
|
||||||
|
.AppendLine("Unrecognized options provided:")
|
||||||
|
.AppendBulletList(remainingOptionInputs.Select(o => o.Alias).Distinct())
|
||||||
|
.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICommand CreateInstance(
|
public ICommand CreateInstance(
|
||||||
|
|||||||
14
CliFx/Domain/CommandUnboundArgumentInput.cs
Normal file
14
CliFx/Domain/CommandUnboundArgumentInput.cs
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
namespace CliFx.Domain
|
||||||
|
{
|
||||||
|
internal class CommandUnboundArgumentInput
|
||||||
|
{
|
||||||
|
public string Value { get; }
|
||||||
|
|
||||||
|
public CommandUnboundArgumentInput(string value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString() => Value;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,21 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using CliFx.Domain;
|
|
||||||
using CliFx.Internal;
|
using CliFx.Internal;
|
||||||
|
|
||||||
namespace CliFx
|
namespace CliFx.Domain
|
||||||
{
|
{
|
||||||
public partial class CliApplication
|
internal class HelpTextWriter
|
||||||
{
|
{
|
||||||
private void RenderHelp(ApplicationSchema applicationSchema, CommandSchema command)
|
private readonly ApplicationMetadata _metadata;
|
||||||
|
private readonly IConsole _console;
|
||||||
|
|
||||||
|
public HelpTextWriter(ApplicationMetadata metadata, IConsole console)
|
||||||
|
{
|
||||||
|
_metadata = metadata;
|
||||||
|
_console = console;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Write(ApplicationSchema applicationSchema, CommandSchema command)
|
||||||
{
|
{
|
||||||
var column = 0;
|
var column = 0;
|
||||||
var row = 0;
|
var row = 0;
|
||||||
@@ -12,7 +12,7 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Input stream (stdin).
|
/// Input stream (stdin).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
TextReader Input { get; }
|
StreamReader Input { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether the input stream is redirected.
|
/// Whether the input stream is redirected.
|
||||||
@@ -22,7 +22,7 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Output stream (stdout).
|
/// Output stream (stdout).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
TextWriter Output { get; }
|
StreamWriter Output { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether the output stream is redirected.
|
/// Whether the output stream is redirected.
|
||||||
@@ -32,7 +32,7 @@ namespace CliFx
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Error stream (stderr).
|
/// Error stream (stderr).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
TextWriter Error { get; }
|
StreamWriter Error { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Whether the error stream is redirected.
|
/// Whether the error stream is redirected.
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
#if NET45 || NETSTANDARD2_0
|
// ReSharper disable CheckNamespace
|
||||||
|
|
||||||
|
#if NET45 || NETSTANDARD2_0
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
|
|||||||
@@ -12,19 +12,19 @@ namespace CliFx
|
|||||||
private CancellationTokenSource? _cancellationTokenSource;
|
private CancellationTokenSource? _cancellationTokenSource;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public TextReader Input => Console.In;
|
public StreamReader Input { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsInputRedirected => Console.IsInputRedirected;
|
public bool IsInputRedirected => Console.IsInputRedirected;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public TextWriter Output => Console.Out;
|
public StreamWriter Output { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsOutputRedirected => Console.IsOutputRedirected;
|
public bool IsOutputRedirected => Console.IsOutputRedirected;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public TextWriter Error => Console.Error;
|
public StreamWriter Error { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsErrorRedirected => Console.IsErrorRedirected;
|
public bool IsErrorRedirected => Console.IsErrorRedirected;
|
||||||
@@ -43,6 +43,16 @@ namespace CliFx
|
|||||||
set => Console.BackgroundColor = value;
|
set => Console.BackgroundColor = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="SystemConsole"/>.
|
||||||
|
/// </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};
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void ResetColor() => Console.ResetColor();
|
public void ResetColor() => Console.ResetColor();
|
||||||
|
|
||||||
|
|||||||
@@ -9,24 +9,24 @@ namespace CliFx
|
|||||||
/// Does not leak to system console in any way.
|
/// Does not leak to system console in any way.
|
||||||
/// Use this class as a substitute for system console when running tests.
|
/// Use this class as a substitute for system console when running tests.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class VirtualConsole : IConsole
|
public partial class VirtualConsole : IConsole
|
||||||
{
|
{
|
||||||
private readonly CancellationToken _cancellationToken;
|
private readonly CancellationToken _cancellationToken;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public TextReader Input { get; }
|
public StreamReader Input { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsInputRedirected { get; }
|
public bool IsInputRedirected { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public TextWriter Output { get; }
|
public StreamWriter Output { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsOutputRedirected { get; }
|
public bool IsOutputRedirected { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public TextWriter Error { get; }
|
public StreamWriter Error { get; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool IsErrorRedirected { get; }
|
public bool IsErrorRedirected { get; }
|
||||||
@@ -37,50 +37,6 @@ namespace CliFx
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black;
|
public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes an instance of <see cref="VirtualConsole"/>.
|
|
||||||
/// </summary>
|
|
||||||
public VirtualConsole(TextReader input, bool isInputRedirected,
|
|
||||||
TextWriter output, bool isOutputRedirected,
|
|
||||||
TextWriter error, bool isErrorRedirected,
|
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
Input = input;
|
|
||||||
IsInputRedirected = isInputRedirected;
|
|
||||||
Output = output;
|
|
||||||
IsOutputRedirected = isOutputRedirected;
|
|
||||||
Error = error;
|
|
||||||
IsErrorRedirected = isErrorRedirected;
|
|
||||||
_cancellationToken = cancellationToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes an instance of <see cref="VirtualConsole"/>.
|
|
||||||
/// </summary>
|
|
||||||
public VirtualConsole(TextReader input, TextWriter output, TextWriter error,
|
|
||||||
CancellationToken cancellationToken = default)
|
|
||||||
: this(input, true, output, true, error, true, cancellationToken)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes an instance of <see cref="VirtualConsole"/> using output stream (stdout) and error stream (stderr).
|
|
||||||
/// Input stream (stdin) is replaced with a no-op stub.
|
|
||||||
/// </summary>
|
|
||||||
public VirtualConsole(TextWriter output, TextWriter error, CancellationToken cancellationToken = default)
|
|
||||||
: this(TextReader.Null, output, error, cancellationToken)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Initializes an instance of <see cref="VirtualConsole"/> using output stream (stdout).
|
|
||||||
/// Input stream (stdin) and error stream (stderr) are replaced with no-op stubs.
|
|
||||||
/// </summary>
|
|
||||||
public VirtualConsole(TextWriter output, CancellationToken cancellationToken = default)
|
|
||||||
: this(output, TextWriter.Null, cancellationToken)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void ResetColor()
|
public void ResetColor()
|
||||||
{
|
{
|
||||||
@@ -90,5 +46,54 @@ namespace CliFx
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public CancellationToken GetCancellationToken() => _cancellationToken;
|
public CancellationToken GetCancellationToken() => _cancellationToken;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="VirtualConsole"/>.
|
||||||
|
/// Use named parameters to specify the streams you want to override.
|
||||||
|
/// </summary>
|
||||||
|
public VirtualConsole(
|
||||||
|
StreamReader? input = null, bool isInputRedirected = true,
|
||||||
|
StreamWriter? output = null, bool isOutputRedirected = true,
|
||||||
|
StreamWriter? error = null, bool isErrorRedirected = true,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
Input = input ?? StreamReader.Null;
|
||||||
|
IsInputRedirected = isInputRedirected;
|
||||||
|
Output = output ?? StreamWriter.Null;
|
||||||
|
IsOutputRedirected = isOutputRedirected;
|
||||||
|
Error = error ?? StreamWriter.Null;
|
||||||
|
IsErrorRedirected = isErrorRedirected;
|
||||||
|
_cancellationToken = cancellationToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes an instance of <see cref="VirtualConsole"/>.
|
||||||
|
/// Use named parameters to specify the streams you want to override.
|
||||||
|
/// </summary>
|
||||||
|
public VirtualConsole(
|
||||||
|
Stream? input = null, bool isInputRedirected = true,
|
||||||
|
Stream? output = null, bool isOutputRedirected = true,
|
||||||
|
Stream? error = null, bool isErrorRedirected = true,
|
||||||
|
CancellationToken cancellationToken = default)
|
||||||
|
: this(
|
||||||
|
WrapInput(input), isInputRedirected,
|
||||||
|
WrapOutput(output), isOutputRedirected,
|
||||||
|
WrapOutput(error), isErrorRedirected,
|
||||||
|
cancellationToken)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public partial class VirtualConsole
|
||||||
|
{
|
||||||
|
private static StreamReader WrapInput(Stream? stream) =>
|
||||||
|
stream != null
|
||||||
|
? new StreamReader(stream, Console.InputEncoding, false)
|
||||||
|
: StreamReader.Null;
|
||||||
|
|
||||||
|
private static StreamWriter WrapOutput(Stream? stream) =>
|
||||||
|
stream != null
|
||||||
|
? new StreamWriter(stream, Console.OutputEncoding) {AutoFlush = true}
|
||||||
|
: StreamWriter.Null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
35
Readme.md
35
Readme.md
@@ -21,6 +21,7 @@ An important property of CliFx, when compared to some other libraries, is that i
|
|||||||
- Configuration via attributes
|
- Configuration via attributes
|
||||||
- Handles conversions to various types, including custom types
|
- Handles conversions to various types, including custom types
|
||||||
- Supports multi-level command hierarchies
|
- Supports multi-level command hierarchies
|
||||||
|
- Exposes raw input, output, error streams to handle binary data
|
||||||
- Allows graceful command cancellation
|
- Allows graceful command cancellation
|
||||||
- Prints errors and routes exit codes on exceptions
|
- Prints errors and routes exit codes on exceptions
|
||||||
- Provides comprehensive and colorful auto-generated help text
|
- Provides comprehensive and colorful auto-generated help text
|
||||||
@@ -424,9 +425,9 @@ public class DivideCommand : ICommand
|
|||||||
Division by zero is not supported.
|
Division by zero is not supported.
|
||||||
|
|
||||||
|
|
||||||
> echo Exit code was %errorlevel%
|
> $LastExitCode
|
||||||
|
|
||||||
Exit code was 1337
|
1337
|
||||||
```
|
```
|
||||||
|
|
||||||
### Graceful cancellation
|
### Graceful cancellation
|
||||||
@@ -521,8 +522,8 @@ By substituting `IConsole` you can write your test cases like this:
|
|||||||
public async Task ConcatCommand_Test()
|
public async Task ConcatCommand_Test()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
using var stdout = new StringWriter();
|
await using var stdOut = new MemoryStream();
|
||||||
var console = new VirtualConsole(stdout);
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
var command = new ConcatCommand
|
var command = new ConcatCommand
|
||||||
{
|
{
|
||||||
@@ -532,9 +533,10 @@ public async Task ConcatCommand_Test()
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
await command.ExecuteAsync(console);
|
await command.ExecuteAsync(console);
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(stdout.ToString(), Is.EqualTo("foo bar"));
|
Assert.That(stdOutData, Is.EqualTo("foo bar"));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -545,8 +547,8 @@ And if you want, you can also test the whole application in a similar fashion:
|
|||||||
public async Task ConcatCommand_Test()
|
public async Task ConcatCommand_Test()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
using var stdout = new StringWriter();
|
await using var stdOut = new MemoryStream();
|
||||||
var console = new VirtualConsole(stdout);
|
var console = new VirtualConsole(output: stdOut);
|
||||||
|
|
||||||
var app = new CliApplicationBuilder()
|
var app = new CliApplicationBuilder()
|
||||||
.AddCommand(typeof(ConcatCommand))
|
.AddCommand(typeof(ConcatCommand))
|
||||||
@@ -558,13 +560,14 @@ public async Task ConcatCommand_Test()
|
|||||||
|
|
||||||
// Act
|
// Act
|
||||||
await app.RunAsync(args, envVars);
|
await app.RunAsync(args, envVars);
|
||||||
|
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.That(stdout.ToString(), Is.EqualTo("foo bar"));
|
Assert.That(stdOutData, Is.EqualTo("foo bar"));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Generally, the first approach is more preferable as it's less verbose and lets you test a specific command in complete isolation.
|
Note that CliFx applications have access to underlying binary streams that allows them to write binary data directly. By using the approach outlined above, we're making the assumption that the application is only expected to produce text output.
|
||||||
|
|
||||||
### Debug and preview mode
|
### Debug and preview mode
|
||||||
|
|
||||||
@@ -657,13 +660,13 @@ Frequency=3124994 Hz, Resolution=320.0006 ns, Timer=TSC
|
|||||||
|
|
||||||
| Method | Mean | Error | StdDev | Ratio | RatioSD | Rank |
|
| Method | Mean | Error | StdDev | Ratio | RatioSD | Rank |
|
||||||
|------------------------------------- |------------:|----------:|-----------:|------:|--------:|-----:|
|
|------------------------------------- |------------:|----------:|-----------:|------:|--------:|-----:|
|
||||||
| CommandLineParser | 24.85 us | 0.413 us | 0.386 us | 0.36 | 0.01 | 1 |
|
| CommandLineParser | 24.79 us | 0.166 us | 0.155 us | 0.49 | 0.00 | 1 |
|
||||||
| CliFx | 68.29 us | 1.050 us | 0.982 us | 1.00 | 0.00 | 2 |
|
| CliFx | 50.27 us | 0.248 us | 0.232 us | 1.00 | 0.00 | 2 |
|
||||||
| Clipr | 162.71 us | 1.299 us | 1.152 us | 2.38 | 0.04 | 3 |
|
| Clipr | 160.22 us | 0.817 us | 0.764 us | 3.19 | 0.02 | 3 |
|
||||||
| McMaster.Extensions.CommandLineUtils | 169.83 us | 1.515 us | 1.417 us | 2.49 | 0.04 | 4 |
|
| McMaster.Extensions.CommandLineUtils | 166.45 us | 1.111 us | 1.039 us | 3.31 | 0.03 | 4 |
|
||||||
| System.CommandLine | 171.73 us | 1.636 us | 1.451 us | 2.51 | 0.05 | 4 |
|
| System.CommandLine | 170.27 us | 0.599 us | 0.560 us | 3.39 | 0.02 | 5 |
|
||||||
| PowerArgs | 312.14 us | 4.335 us | 4.055 us | 4.57 | 0.10 | 5 |
|
| PowerArgs | 306.12 us | 1.495 us | 1.398 us | 6.09 | 0.03 | 6 |
|
||||||
| Cocona | 2,089.95 us | 58.763 us | 170.481 us | 31.61 | 2.34 | 6 |
|
| Cocona | 1,856.07 us | 48.727 us | 141.367 us | 37.88 | 2.60 | 7 |
|
||||||
|
|
||||||
## Etymology
|
## Etymology
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user