mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c6d2359d6b | ||
|
|
0d32876bad | ||
|
|
c063251d89 | ||
|
|
3831cfc7c0 | ||
|
|
b17341b56c | ||
|
|
5bda964fb5 | ||
|
|
432430489a | ||
|
|
9a20101f30 | ||
|
|
b491818779 | ||
|
|
69c24c8dfc | ||
|
|
004f906148 | ||
|
|
ac83233dc2 | ||
|
|
082910c968 | ||
|
|
11e3e0f85d | ||
|
|
42f4d7d5a7 | ||
|
|
bed22b6500 | ||
|
|
17449e0794 | ||
|
|
4732166f5f | ||
|
|
f5e37b96fc | ||
|
|
4cef596fe8 | ||
|
|
19b87717c1 | ||
|
|
7e4c6b20ff | ||
|
|
fb2071ed2b |
Binary file not shown.
|
Before Width: | Height: | Size: 20 KiB After Width: | Height: | Size: 24 KiB |
23
Changelog.md
23
Changelog.md
@@ -1,3 +1,26 @@
|
||||
### v1.4 (20-Aug-2020)
|
||||
|
||||
- Added `VirtualConsole.CreateBuffered()` method to simplify test setup when using in-memory backing stores for output and error streams. Please refer to the readme for updated recommendations on how to test applications built with CliFx.
|
||||
- Added generic `CliApplicationBuilder.AddCommand<TCommand>()`. This overload simplifies adding commands one-by-one as it also checks that the type implements `ICommand`.
|
||||
|
||||
### v1.3.2 (31-Jul-2020)
|
||||
|
||||
- Fixed an issue where a command was incorrectly allowed to execute when the user did not specify any value for a non-scalar parameter. Since they are always required, a parameter needs to be bound to (at least) one value. (Thanks [@Daniel Hix](https://github.com/ADustyOldMuffin))
|
||||
- Fixed an issue where `CliApplication.RunAsync(...)` threw `ArgumentException` if there were two environment variables, whose names differed only in case. Environment variable names are now treated case-sensitively. (Thanks [@Ron Myers](https://github.com/ron-myers))
|
||||
|
||||
### v1.3.1 (19-Jul-2020)
|
||||
|
||||
- Running the application with the debug directive (`myapp [debug]`) will now also try to launch a debugger instance. In most cases it will save time as you won't need to attach the debugger manually. (Thanks [@Volodymyr Shkolka](https://github.com/BlackGad))
|
||||
- Fixed an issue where unhandled generic exceptions (i.e. not `CommandException`) sometimes caused the application to incorrectly return successful exit code due to an overflow issue on Unix systems. Starting from this version, all unhandled generic exceptions will produce `1` as the exit code when thrown. Instances of `CommandException` can still be configured to return any specified exit code, but it's recommended to constrain the values between `1` and `255` to avoid overflow issues. (Thanks [@Ihor Nechyporuk](https://github.com/inech))
|
||||
|
||||
### v1.3 (23-May-2020)
|
||||
|
||||
- Changed analyzers to report errors instead of warnings. If you find that some analyzer works incorrectly, please report it on GitHub. You can also configure inspection severity overrides in your project if you need to.
|
||||
- Improved help text by showing default values for non-required options. This only works on types that have a custom override for `ToString()` method. Additionally, if the type implements `IFormattable`, the overload with a format provider will be used instead. (Thanks [@Domn Werner](https://github.com/domn1995))
|
||||
- Changed default version text to only show 3 version components instead of 4, if the last component (revision) is not specified or is zero. This makes the default version text compliant with semantic versioning.
|
||||
- Fixed an issue where it was possible to define a command with an option that has the same name or short name as built-in help or version options. Previously it would lead to the user-defined option being ignored in favor of the built-in option. Now this will throw an exception instead.
|
||||
- Changed the underlying representation of `StreamReader`/`StreamWriter` objects used in `SystemConsole` and `VirtualConsole` to be thread-safe.
|
||||
|
||||
### v1.2 (11-May-2020)
|
||||
|
||||
- Added built-in Roslyn analyzers that help catch incorrect usage of the library. Currently, all analyzers report issues as warnings so as to not prevent the project from building. In the future that may change.
|
||||
|
||||
@@ -206,7 +206,7 @@ namespace CliFx.Analyzers
|
||||
// Duplicate environment variable name
|
||||
var duplicateEnvironmentVariableNameOptions = options
|
||||
.Where(p => !string.IsNullOrWhiteSpace(p.EnvironmentVariableName))
|
||||
.GroupBy(p => p.EnvironmentVariableName, StringComparer.OrdinalIgnoreCase)
|
||||
.GroupBy(p => p.EnvironmentVariableName, StringComparer.Ordinal)
|
||||
.Where(g => g.Count() > 1)
|
||||
.SelectMany(g => g.AsEnumerable())
|
||||
.ToArray();
|
||||
|
||||
@@ -8,67 +8,67 @@ namespace CliFx.Analyzers
|
||||
new DiagnosticDescriptor(nameof(CliFx0001),
|
||||
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
|
||||
"Type must implement the 'CliFx.ICommand' interface in order to be a valid command",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0002 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0002),
|
||||
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
|
||||
"Type must be annotated with the 'CliFx.Attributes.CommandAttribute' in order to be a valid command",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0021 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0021),
|
||||
"Parameter order must be unique within its command",
|
||||
"Parameter order must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0022 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0022),
|
||||
"Parameter order must have unique name within its command",
|
||||
"Parameter order must have unique name within its command",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0023 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0023),
|
||||
"Only one non-scalar parameter per command is allowed",
|
||||
"Only one non-scalar parameter per command is allowed",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0024 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0024),
|
||||
"Non-scalar parameter must be last in order",
|
||||
"Non-scalar parameter must be last in order",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0041 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0041),
|
||||
"Option must have a name or short name specified",
|
||||
"Option must have a name or short name specified",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0042 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0042),
|
||||
"Option name must be at least 2 characters long",
|
||||
"Option name must be at least 2 characters long",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0043 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0043),
|
||||
"Option name must be unique within its command",
|
||||
"Option name must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0044 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0044),
|
||||
"Option short name must be unique within its command",
|
||||
"Option short name must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0045 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0045),
|
||||
"Option environment variable name must be unique within its command",
|
||||
"Option environment variable name must be unique within its command",
|
||||
"Usage", DiagnosticSeverity.Warning, true);
|
||||
"Usage", DiagnosticSeverity.Error, true);
|
||||
|
||||
public static readonly DiagnosticDescriptor CliFx0100 =
|
||||
new DiagnosticDescriptor(nameof(CliFx0100),
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace CliFx.Benchmarks
|
||||
|
||||
[Benchmark(Description = "CliFx", Baseline = true)]
|
||||
public async ValueTask<int> ExecuteWithCliFx() =>
|
||||
await new CliApplicationBuilder().AddCommand(typeof(CliFxCommand)).Build().RunAsync(Arguments, new Dictionary<string, string>());
|
||||
await new CliApplicationBuilder().AddCommand<CliFxCommand>().Build().RunAsync(Arguments, new Dictionary<string, string>());
|
||||
|
||||
[Benchmark(Description = "System.CommandLine")]
|
||||
public async Task<int> ExecuteWithSystemCommandLine() =>
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CliFx.Tests.Dummy.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Demos how to show an error message then help text from an organizational command.
|
||||
/// </summary>
|
||||
[Command("cmd-err", Description = "This is an organizational command. " +
|
||||
"I don't do anything except provide a route to my subcommands. " +
|
||||
"If you use just me, I print an error message then the help text " +
|
||||
"to remind you of my subcommands.")]
|
||||
public class ShowErrorMessageThenHelpTextOnCommandExceptionCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) =>
|
||||
throw new CommandException("It is an error to use me without a subcommand. " +
|
||||
"Please refer to the help text below for guidance.", showHelp: true);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CliFx.Tests.Dummy.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Demos how to show help text from an organizational command.
|
||||
/// </summary>
|
||||
[Command("cmd", Description = "This is an organizational command. " +
|
||||
"I don't do anything except provide a route to my subcommands. " +
|
||||
"If you use just me, I print the help text to remind you of my subcommands.")]
|
||||
public class ShowHelpTextOnCommandExceptionCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) =>
|
||||
throw new CommandException(null, showHelp: false);
|
||||
}
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ApplicationSpecs
|
||||
{
|
||||
[Command]
|
||||
private class NonImplementedCommand
|
||||
{
|
||||
}
|
||||
|
||||
private class NonAnnotatedCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("dup")]
|
||||
private class DuplicateNameCommandA : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("dup")]
|
||||
private class DuplicateNameCommandB : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateParameterOrderCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13)]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(13)]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateParameterNameCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "param")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1, Name = "param")]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class MultipleNonScalarParametersCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public IReadOnlyList<string>? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class NonLastNonScalarParameterCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class EmptyOptionNameCommand : ICommand
|
||||
{
|
||||
[CommandOption("")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class SingleCharacterOptionNameCommand : ICommand
|
||||
{
|
||||
[CommandOption("a")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateOptionNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption("fruits")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
[CommandOption("fruits")]
|
||||
public string? Oranges { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateOptionShortNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption('x')]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption('x')]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class DuplicateOptionEnvironmentVariableNamesCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class ValidCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("hidden", Description = "Description")]
|
||||
private class HiddenPropertiesCommand : ICommand
|
||||
{
|
||||
[CommandParameter(13, Name = "param", Description = "Param description")]
|
||||
public string? Parameter { get; set; }
|
||||
|
||||
[CommandOption("option", 'o', Description = "Option description", EnvironmentVariableName = "ENV")]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public string? HiddenA { get; set; }
|
||||
|
||||
public bool? HiddenB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Exceptions;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Commands;
|
||||
using CliFx.Tests.Commands.Invalid;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ApplicationSpecs
|
||||
public class ApplicationSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
@@ -31,10 +32,10 @@ namespace CliFx.Tests
|
||||
{
|
||||
// Act
|
||||
var app = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ValidCommand))
|
||||
.AddCommandsFrom(typeof(ValidCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(ValidCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(ValidCommand).Assembly})
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommandsFrom(typeof(DefaultCommand).Assembly)
|
||||
.AddCommands(new[] {typeof(DefaultCommand)})
|
||||
.AddCommandsFrom(new[] {typeof(DefaultCommand).Assembly})
|
||||
.AddCommandsFromThisAssembly()
|
||||
.AllowDebugMode()
|
||||
.AllowPreviewMode()
|
||||
@@ -51,185 +52,324 @@ namespace CliFx.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void At_least_one_command_must_be_defined_in_an_application()
|
||||
public async Task At_least_one_command_must_be_defined_in_an_application()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = Array.Empty<Type>();
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Commands_must_implement_the_corresponding_interface()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(NonImplementedCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Commands_must_be_annotated_by_an_attribute()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(NonAnnotatedCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Commands_must_have_unique_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateNameCommandA), typeof(DuplicateNameCommandB)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameters_must_have_unique_order()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateParameterOrderCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameters_must_have_unique_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateParameterNameCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(MultipleNonScalarParametersCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(NonLastNonScalarParameterCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_names_that_are_not_empty()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(EmptyOptionNameCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_names_that_are_longer_than_one_character()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(SingleCharacterOptionNameCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_unique_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateOptionNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_unique_short_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateOptionShortNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_must_have_unique_environment_variable_names()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(DuplicateOptionEnvironmentVariableNamesCommand)};
|
||||
|
||||
// Act & assert
|
||||
var ex = Assert.Throws<CliFxException>(() => ApplicationSchema.Resolve(commandTypes));
|
||||
_output.WriteLine(ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Command_options_and_parameters_must_be_annotated_by_corresponding_attributes()
|
||||
{
|
||||
// Arrange
|
||||
var commandTypes = new[] {typeof(HiddenPropertiesCommand)};
|
||||
var application = new CliApplicationBuilder()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var schema = ApplicationSchema.Resolve(commandTypes);
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// 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")
|
||||
})
|
||||
}));
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
schema.ToString().Should().NotBeNullOrWhiteSpace(); // this is only for coverage, I'm sorry
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Commands_must_implement_the_corresponding_interface()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(NonImplementedCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Commands_must_be_annotated_by_an_attribute()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<NonAnnotatedCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Commands_must_have_unique_names()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<GenericExceptionCommand>()
|
||||
.AddCommand<CommandExceptionCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_can_be_default_but_only_if_it_is_the_only_such_command()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<OtherDefaultCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_parameters_must_have_unique_order()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DuplicateParameterOrderCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_parameters_must_have_unique_names()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DuplicateParameterNameCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_parameter_can_be_non_scalar_only_if_no_other_such_parameter_is_present()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<MultipleNonScalarParametersCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_parameter_can_be_non_scalar_only_if_it_is_the_last_in_order()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<NonLastNonScalarParameterCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_options_must_have_names_that_are_not_empty()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<EmptyOptionNameCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_options_must_have_names_that_are_longer_than_one_character()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<SingleCharacterOptionNameCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_options_must_have_unique_names()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DuplicateOptionNamesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_options_must_have_unique_short_names()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DuplicateOptionShortNamesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_options_must_have_unique_environment_variable_names()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DuplicateOptionEnvironmentVariableNamesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_options_must_not_have_conflicts_with_the_implicit_help_option()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<ConflictWithHelpOptionCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_options_must_not_have_conflicts_with_the_implicit_version_option()
|
||||
{
|
||||
var (console, _, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<ConflictWithVersionOptionCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,191 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ArgumentBindingSpecs
|
||||
{
|
||||
private enum CustomEnum
|
||||
{
|
||||
Value1 = 1,
|
||||
Value2 = 2,
|
||||
Value3 = 3
|
||||
}
|
||||
|
||||
private class StringConstructable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public StringConstructable(string value) => Value = value;
|
||||
}
|
||||
|
||||
private class StringParseable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private StringParseable(string value) => Value = value;
|
||||
|
||||
public static StringParseable Parse(string value) => new StringParseable(value);
|
||||
}
|
||||
|
||||
private class StringParseableWithFormatProvider
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
private StringParseableWithFormatProvider(string value) => Value = value;
|
||||
|
||||
public static StringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||
new StringParseableWithFormatProvider(value + " " + formatProvider);
|
||||
}
|
||||
|
||||
private class DummyType
|
||||
{
|
||||
}
|
||||
|
||||
public class CustomEnumerable<T> : IEnumerable<T>
|
||||
{
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
1352
CliFx.Tests/ArgumentConversionSpecs.cs
Normal file
1352
CliFx.Tests/ArgumentConversionSpecs.cs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,315 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,41 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Commands;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class CancellationSpecs
|
||||
public class CancellationSpecs
|
||||
{
|
||||
[Fact]
|
||||
public async Task Command_can_perform_additional_cleanup_if_cancellation_is_requested()
|
||||
{
|
||||
// Can't test it with a real console because CliWrap can't send Ctrl+C
|
||||
|
||||
// Arrange
|
||||
using var cts = new CancellationTokenSource();
|
||||
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut, cancellationToken: cts.Token);
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered(cts.Token);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CancellableCommand))
|
||||
.AddCommand<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();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdOutData.Should().Be("Cancellation requested");
|
||||
stdOut.GetString().Trim().Should().Be(CancellableCommand.CancellationOutputText);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
<PackageReference Include="FluentAssertions" Version="5.10.2" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
|
||||
<PackageReference Include="coverlet.msbuild" Version="2.8.0" PrivateAssets="all" />
|
||||
|
||||
31
CliFx.Tests/Commands/CancellableCommand.cs
Normal file
31
CliFx.Tests/Commands/CancellableCommand.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class CancellableCommand : ICommand
|
||||
{
|
||||
public const string CompletionOutputText = "Finished";
|
||||
public const string CancellationOutputText = "Canceled";
|
||||
|
||||
public async ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(
|
||||
TimeSpan.FromSeconds(3),
|
||||
console.GetCancellationToken()
|
||||
);
|
||||
|
||||
console.Output.WriteLine(CompletionOutputText);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
console.Output.WriteLine(CancellationOutputText);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
21
CliFx.Tests/Commands/CommandExceptionCommand.cs
Normal file
21
CliFx.Tests/Commands/CommandExceptionCommand.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class CommandExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("code", 'c')]
|
||||
public int ExitCode { get; set; } = 133;
|
||||
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
[CommandOption("show-help")]
|
||||
public bool ShowHelp { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode, ShowHelp);
|
||||
}
|
||||
}
|
||||
17
CliFx.Tests/Commands/DefaultCommand.cs
Normal file
17
CliFx.Tests/Commands/DefaultCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command(Description = "Default command description")]
|
||||
public class DefaultCommand : ICommand
|
||||
{
|
||||
public const string ExpectedOutputText = nameof(DefaultCommand);
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(ExpectedOutputText);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
15
CliFx.Tests/Commands/GenericExceptionCommand.cs
Normal file
15
CliFx.Tests/Commands/GenericExceptionCommand.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class GenericExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class ConflictWithHelpOptionCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("option-h", 'h')]
|
||||
public string? OptionH { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
// Must be default because version option is available only on default commands
|
||||
[Command]
|
||||
public class ConflictWithVersionOptionCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("version")]
|
||||
public string? Version { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class DuplicateOptionEnvironmentVariableNamesCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("option-a", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", EnvironmentVariableName = "ENV_VAR")]
|
||||
public string? OptionB { get; set; }
|
||||
}
|
||||
}
|
||||
14
CliFx.Tests/Commands/Invalid/DuplicateOptionNamesCommand.cs
Normal file
14
CliFx.Tests/Commands/Invalid/DuplicateOptionNamesCommand.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class DuplicateOptionNamesCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("fruits")]
|
||||
public string? Apples { get; set; }
|
||||
|
||||
[CommandOption("fruits")]
|
||||
public string? Oranges { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class DuplicateOptionShortNamesCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption('x')]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption('x')]
|
||||
public string? OptionB { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class DuplicateParameterNameCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandParameter(0, Name = "param")]
|
||||
public string? ParamA { get; set; }
|
||||
|
||||
[CommandParameter(1, Name = "param")]
|
||||
public string? ParamB { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class DuplicateParameterOrderCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandParameter(13)]
|
||||
public string? ParamA { get; set; }
|
||||
|
||||
[CommandParameter(13)]
|
||||
public string? ParamB { get; set; }
|
||||
}
|
||||
}
|
||||
11
CliFx.Tests/Commands/Invalid/EmptyOptionNameCommand.cs
Normal file
11
CliFx.Tests/Commands/Invalid/EmptyOptionNameCommand.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class EmptyOptionNameCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("")]
|
||||
public string? Apples { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class MultipleNonScalarParametersCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParamA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public IReadOnlyList<string>? ParamB { get; set; }
|
||||
}
|
||||
}
|
||||
6
CliFx.Tests/Commands/Invalid/NonAnnotatedCommand.cs
Normal file
6
CliFx.Tests/Commands/Invalid/NonAnnotatedCommand.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
public class NonAnnotatedCommand : SelfSerializeCommandBase
|
||||
{
|
||||
}
|
||||
}
|
||||
9
CliFx.Tests/Commands/Invalid/NonImplementedCommand.cs
Normal file
9
CliFx.Tests/Commands/Invalid/NonImplementedCommand.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command]
|
||||
public class NonImplementedCommand
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class NonLastNonScalarParameterCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public IReadOnlyList<string>? ParamA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public string? ParamB { get; set; }
|
||||
}
|
||||
}
|
||||
9
CliFx.Tests/Commands/Invalid/OtherDefaultCommand.cs
Normal file
9
CliFx.Tests/Commands/Invalid/OtherDefaultCommand.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command]
|
||||
public class OtherDefaultCommand : SelfSerializeCommandBase
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands.Invalid
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class SingleCharacterOptionNameCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("a")]
|
||||
public string? Apples { get; set; }
|
||||
}
|
||||
}
|
||||
17
CliFx.Tests/Commands/NamedCommand.cs
Normal file
17
CliFx.Tests/Commands/NamedCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("named", Description = "Named command description")]
|
||||
public class NamedCommand : ICommand
|
||||
{
|
||||
public const string ExpectedOutputText = nameof(NamedCommand);
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(ExpectedOutputText);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
CliFx.Tests/Commands/NamedSubCommand.cs
Normal file
17
CliFx.Tests/Commands/NamedSubCommand.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("named sub", Description = "Named sub command description")]
|
||||
public class NamedSubCommand : ICommand
|
||||
{
|
||||
public const string ExpectedOutputText = nameof(NamedSubCommand);
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(ExpectedOutputText);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
CliFx.Tests/Commands/SelfSerializeCommandBase.cs
Normal file
14
CliFx.Tests/Commands/SelfSerializeCommandBase.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
public abstract class SelfSerializeCommandBase : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(JsonConvert.SerializeObject(this));
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
155
CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs
Normal file
155
CliFx.Tests/Commands/SupportedArgumentTypesCommand.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public partial class SupportedArgumentTypesCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("obj")]
|
||||
public object? Object { get; set; } = 42;
|
||||
|
||||
[CommandOption("str")]
|
||||
public string? String { get; set; } = "foo bar";
|
||||
|
||||
[CommandOption("bool")]
|
||||
public bool Bool { get; set; }
|
||||
|
||||
[CommandOption("char")]
|
||||
public char Char { get; set; }
|
||||
|
||||
[CommandOption("sbyte")]
|
||||
public sbyte Sbyte { get; set; }
|
||||
|
||||
[CommandOption("byte")]
|
||||
public byte Byte { get; set; }
|
||||
|
||||
[CommandOption("short")]
|
||||
public short Short { get; set; }
|
||||
|
||||
[CommandOption("ushort")]
|
||||
public ushort Ushort { get; set; }
|
||||
|
||||
[CommandOption("int")]
|
||||
public int Int { get; set; }
|
||||
|
||||
[CommandOption("uint")]
|
||||
public uint Uint { get; set; }
|
||||
|
||||
[CommandOption("long")]
|
||||
public long Long { get; set; }
|
||||
|
||||
[CommandOption("ulong")]
|
||||
public ulong Ulong { get; set; }
|
||||
|
||||
[CommandOption("float")]
|
||||
public float Float { get; set; }
|
||||
|
||||
[CommandOption("double")]
|
||||
public double Double { get; set; }
|
||||
|
||||
[CommandOption("decimal")]
|
||||
public decimal Decimal { get; set; }
|
||||
|
||||
[CommandOption("datetime")]
|
||||
public DateTime DateTime { get; set; }
|
||||
|
||||
[CommandOption("datetime-offset")]
|
||||
public DateTimeOffset DateTimeOffset { get; set; }
|
||||
|
||||
[CommandOption("timespan")]
|
||||
public TimeSpan TimeSpan { get; set; }
|
||||
|
||||
[CommandOption("enum")]
|
||||
public CustomEnum Enum { get; set; }
|
||||
|
||||
[CommandOption("int-nullable")]
|
||||
public int? IntNullable { get; set; }
|
||||
|
||||
[CommandOption("enum-nullable")]
|
||||
public CustomEnum? EnumNullable { get; set; }
|
||||
|
||||
[CommandOption("timespan-nullable")]
|
||||
public TimeSpan? TimeSpanNullable { get; set; }
|
||||
|
||||
[CommandOption("str-constructible")]
|
||||
public CustomStringConstructible? StringConstructible { get; set; }
|
||||
|
||||
[CommandOption("str-parseable")]
|
||||
public CustomStringParseable? StringParseable { get; set; }
|
||||
|
||||
[CommandOption("str-parseable-format")]
|
||||
public CustomStringParseableWithFormatProvider? StringParseableWithFormatProvider { get; set; }
|
||||
|
||||
[CommandOption("obj-array")]
|
||||
public object[]? ObjectArray { get; set; }
|
||||
|
||||
[CommandOption("str-array")]
|
||||
public string[]? StringArray { get; set; }
|
||||
|
||||
[CommandOption("int-array")]
|
||||
public int[]? IntArray { get; set; }
|
||||
|
||||
[CommandOption("enum-array")]
|
||||
public CustomEnum[]? EnumArray { get; set; }
|
||||
|
||||
[CommandOption("int-nullable-array")]
|
||||
public int?[]? IntNullableArray { get; set; }
|
||||
|
||||
[CommandOption("str-constructible-array")]
|
||||
public CustomStringConstructible[]? StringConstructibleArray { get; set; }
|
||||
|
||||
[CommandOption("str-enumerable")]
|
||||
public IEnumerable<string>? StringEnumerable { get; set; }
|
||||
|
||||
[CommandOption("str-read-only-list")]
|
||||
public IReadOnlyList<string>? StringReadOnlyList { get; set; }
|
||||
|
||||
[CommandOption("str-list")]
|
||||
public List<string>? StringList { get; set; }
|
||||
|
||||
[CommandOption("str-set")]
|
||||
public HashSet<string>? StringHashSet { get; set; }
|
||||
}
|
||||
|
||||
public partial class SupportedArgumentTypesCommand
|
||||
{
|
||||
public enum CustomEnum
|
||||
{
|
||||
Value1 = 1,
|
||||
Value2 = 2,
|
||||
Value3 = 3
|
||||
}
|
||||
|
||||
public class CustomStringConstructible
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public CustomStringConstructible(string value) => Value = value;
|
||||
}
|
||||
|
||||
public class CustomStringParseable
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
[JsonConstructor]
|
||||
private CustomStringParseable(string value) => Value = value;
|
||||
|
||||
public static CustomStringParseable Parse(string value) => new CustomStringParseable(value);
|
||||
}
|
||||
|
||||
public class CustomStringParseableWithFormatProvider
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
[JsonConstructor]
|
||||
private CustomStringParseableWithFormatProvider(string value) => Value = value;
|
||||
|
||||
public static CustomStringParseableWithFormatProvider Parse(string value, IFormatProvider formatProvider) =>
|
||||
new CustomStringParseableWithFormatProvider(value + " " + formatProvider);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
CliFx.Tests/Commands/UnsupportedArgumentTypesCommand.cs
Normal file
31
CliFx.Tests/Commands/UnsupportedArgumentTypesCommand.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public partial class UnsupportedArgumentTypesCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("str-non-initializable")]
|
||||
public CustomType? StringNonInitializable { get; set; }
|
||||
|
||||
[CommandOption("str-enumerable-non-initializable")]
|
||||
public CustomEnumerable<string>? StringEnumerableNonInitializable { get; set; }
|
||||
}
|
||||
|
||||
public partial class UnsupportedArgumentTypesCommand
|
||||
{
|
||||
public class CustomType
|
||||
{
|
||||
}
|
||||
|
||||
public class CustomEnumerable<T> : IEnumerable<T>
|
||||
{
|
||||
public IEnumerator<T> GetEnumerator() => ((IEnumerable<T>) Array.Empty<T>()).GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
44
CliFx.Tests/Commands/WithDefaultValuesCommand.cs
Normal file
44
CliFx.Tests/Commands/WithDefaultValuesCommand.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithDefaultValuesCommand : SelfSerializeCommandBase
|
||||
{
|
||||
public enum CustomEnum { Value1, Value2, Value3 };
|
||||
|
||||
[CommandOption("obj")]
|
||||
public object? Object { get; set; } = 42;
|
||||
|
||||
[CommandOption("str")]
|
||||
public string? String { get; set; } = "foo";
|
||||
|
||||
[CommandOption("str-empty")]
|
||||
public string StringEmpty { get; set; } = "";
|
||||
|
||||
[CommandOption("str-array")]
|
||||
public string[]? StringArray { get; set; } = { "foo", "bar", "baz" };
|
||||
|
||||
[CommandOption("bool")]
|
||||
public bool Bool { get; set; } = true;
|
||||
|
||||
[CommandOption("char")]
|
||||
public char Char { get; set; } = 't';
|
||||
|
||||
[CommandOption("int")]
|
||||
public int Int { get; set; } = 1337;
|
||||
|
||||
[CommandOption("int-nullable")]
|
||||
public int? IntNullable { get; set; } = 1337;
|
||||
|
||||
[CommandOption("int-array")]
|
||||
public int[]? IntArray { get; set; } = { 1, 2, 3 };
|
||||
|
||||
[CommandOption("timespan")]
|
||||
public TimeSpan TimeSpan { get; set; } = TimeSpan.FromMinutes(123);
|
||||
|
||||
[CommandOption("enum")]
|
||||
public CustomEnum Enum { get; set; } = CustomEnum.Value2;
|
||||
}
|
||||
}
|
||||
28
CliFx.Tests/Commands/WithDependenciesCommand.cs
Normal file
28
CliFx.Tests/Commands/WithDependenciesCommand.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithDependenciesCommand : ICommand
|
||||
{
|
||||
public class DependencyA
|
||||
{
|
||||
}
|
||||
|
||||
public class DependencyB
|
||||
{
|
||||
}
|
||||
|
||||
private readonly DependencyA _dependencyA;
|
||||
private readonly DependencyB _dependencyB;
|
||||
|
||||
public WithDependenciesCommand(DependencyA dependencyA, DependencyB dependencyB)
|
||||
{
|
||||
_dependencyA = dependencyA;
|
||||
_dependencyB = dependencyB;
|
||||
}
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
19
CliFx.Tests/Commands/WithEnumArgumentsCommand.cs
Normal file
19
CliFx.Tests/Commands/WithEnumArgumentsCommand.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithEnumArgumentsCommand : SelfSerializeCommandBase
|
||||
{
|
||||
public enum CustomEnum { Value1, Value2, Value3 };
|
||||
|
||||
[CommandParameter(0, Name = "enum")]
|
||||
public CustomEnum EnumParameter { get; set; }
|
||||
|
||||
[CommandOption("enum")]
|
||||
public CustomEnum? EnumOption { get; set; }
|
||||
|
||||
[CommandOption("required-enum", IsRequired = true)]
|
||||
public CustomEnum RequiredEnumOption { get; set; }
|
||||
}
|
||||
}
|
||||
15
CliFx.Tests/Commands/WithEnvironmentVariablesCommand.cs
Normal file
15
CliFx.Tests/Commands/WithEnvironmentVariablesCommand.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithEnvironmentVariablesCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("opt-a", 'a', EnvironmentVariableName = "ENV_OPT_A")]
|
||||
public string? OptA { get; set; }
|
||||
|
||||
[CommandOption("opt-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
|
||||
public IReadOnlyList<string>? OptB { get; set; }
|
||||
}
|
||||
}
|
||||
18
CliFx.Tests/Commands/WithParametersCommand.cs
Normal file
18
CliFx.Tests/Commands/WithParametersCommand.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithParametersCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public string? ParamA { get; set; }
|
||||
|
||||
[CommandParameter(1)]
|
||||
public int? ParamB { get; set; }
|
||||
|
||||
[CommandParameter(2)]
|
||||
public IReadOnlyList<string>? ParamC { get; set; }
|
||||
}
|
||||
}
|
||||
18
CliFx.Tests/Commands/WithRequiredOptionsCommand.cs
Normal file
18
CliFx.Tests/Commands/WithRequiredOptionsCommand.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithRequiredOptionsCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("opt-a", 'a', IsRequired = true)]
|
||||
public string? OptA { get; set; }
|
||||
|
||||
[CommandOption("opt-b", 'b')]
|
||||
public int? OptB { get; set; }
|
||||
|
||||
[CommandOption("opt-c", 'c', IsRequired = true)]
|
||||
public IReadOnlyList<char>? OptC { get; set; }
|
||||
}
|
||||
}
|
||||
11
CliFx.Tests/Commands/WithSingleParameterCommand.cs
Normal file
11
CliFx.Tests/Commands/WithSingleParameterCommand.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithSingleParameterCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandParameter(0)]
|
||||
public string? ParamA { get; set; }
|
||||
}
|
||||
}
|
||||
14
CliFx.Tests/Commands/WithSingleRequiredOptionCommand.cs
Normal file
14
CliFx.Tests/Commands/WithSingleRequiredOptionCommand.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithSingleRequiredOptionCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("opt-a")]
|
||||
public string? OptA { get; set; }
|
||||
|
||||
[CommandOption("opt-b", IsRequired = true)]
|
||||
public string? OptB { get; set; }
|
||||
}
|
||||
}
|
||||
12
CliFx.Tests/Commands/WithStringArrayOptionCommand.cs
Normal file
12
CliFx.Tests/Commands/WithStringArrayOptionCommand.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests.Commands
|
||||
{
|
||||
[Command("cmd")]
|
||||
public class WithStringArrayOptionCommand : SelfSerializeCommandBase
|
||||
{
|
||||
[CommandOption("opt", 'o')]
|
||||
public IReadOnlyList<string>? Opt { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,8 @@ namespace CliFx.Tests
|
||||
var console = new VirtualConsole(
|
||||
input: stdIn,
|
||||
output: stdOut,
|
||||
error: stdErr);
|
||||
error: stdErr
|
||||
);
|
||||
|
||||
// Act
|
||||
console.Output.Write("output");
|
||||
@@ -51,6 +52,8 @@ namespace CliFx.Tests
|
||||
console.ResetColor();
|
||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||
console.CursorLeft = 42;
|
||||
console.CursorTop = 24;
|
||||
|
||||
// Assert
|
||||
stdInData.Should().Be("input");
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Tests.Commands;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class DependencyInjectionSpecs
|
||||
public class DependencyInjectionSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
@@ -18,10 +19,10 @@ namespace CliFx.Tests
|
||||
var activator = new DefaultTypeActivator();
|
||||
|
||||
// Act
|
||||
var obj = activator.CreateInstance(typeof(WithoutDependenciesCommand));
|
||||
var obj = activator.CreateInstance(typeof(DefaultCommand));
|
||||
|
||||
// Assert
|
||||
obj.Should().BeOfType<WithoutDependenciesCommand>();
|
||||
obj.Should().BeOfType<DefaultCommand>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -40,7 +41,10 @@ namespace CliFx.Tests
|
||||
{
|
||||
// Arrange
|
||||
var activator = new DelegateTypeActivator(_ =>
|
||||
new WithDependenciesCommand(new DependencyA(), new DependencyB()));
|
||||
new WithDependenciesCommand(
|
||||
new WithDependenciesCommand.DependencyA(),
|
||||
new WithDependenciesCommand.DependencyB())
|
||||
);
|
||||
|
||||
// Act
|
||||
var obj = activator.CreateInstance(typeof(WithDependenciesCommand));
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Commands;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class DirectivesSpecs
|
||||
public class DirectivesSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public DirectivesSpecs(ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public async Task Preview_directive_can_be_enabled_to_print_provided_arguments_as_they_were_parsed()
|
||||
public async Task Preview_directive_can_be_specified_to_print_provided_arguments_as_they_were_parsed()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand<NamedCommand>()
|
||||
.UseConsole(console)
|
||||
.AllowPreviewMode()
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"[preview]", "cmd", "param", "-abc", "--option", "foo"},
|
||||
new[] {"[preview]", "named", "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]");
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"named", "<param>", "[-a]", "[-b]", "[-c]", "[--option \"foo\"]"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Tests.Commands;
|
||||
using CliFx.Tests.Internal;
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
using FluentAssertions;
|
||||
@@ -9,11 +10,11 @@ using Xunit;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class EnvironmentVariablesSpecs
|
||||
public 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()
|
||||
public async Task Option_can_use_an_environment_variable_as_fallback()
|
||||
{
|
||||
// Arrange
|
||||
var command = Cli.Wrap("dotnet")
|
||||
@@ -26,12 +27,12 @@ namespace CliFx.Tests
|
||||
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||
|
||||
// Assert
|
||||
stdOut.TrimEnd().Should().Be("Hello Mars!");
|
||||
stdOut.Trim().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()
|
||||
public async Task Option_only_uses_an_environment_variable_as_fallback_if_the_value_is_not_directly_provided()
|
||||
{
|
||||
// Arrange
|
||||
var command = Cli.Wrap("dotnet")
|
||||
@@ -46,50 +47,94 @@ namespace CliFx.Tests
|
||||
var stdOut = await command.ExecuteBufferedAsync().Select(r => r.StandardOutput);
|
||||
|
||||
// Assert
|
||||
stdOut.TrimEnd().Should().Be("Hello Jupiter!");
|
||||
stdOut.Trim().Should().Be("Hello Jupiter!");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Option_of_non_scalar_type_can_take_multiple_separated_values_from_an_environment_variable()
|
||||
public async Task Option_only_uses_an_environment_variable_as_fallback_if_the_name_matches_case_sensitively()
|
||||
{
|
||||
// Arrange
|
||||
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCollectionCommand)});
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var input = CommandLineInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||
};
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var command = schema.InitializeEntryPoint(input, envVars);
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"cmd"},
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_opt_A"] = "incorrect",
|
||||
["ENV_OPT_A"] = "correct"
|
||||
}
|
||||
);
|
||||
|
||||
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
|
||||
|
||||
// Assert
|
||||
command.Should().BeEquivalentTo(new EnvironmentVariableCollectionCommand
|
||||
exitCode.Should().Be(0);
|
||||
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||
{
|
||||
Option = new[] {"foo", "bar"}
|
||||
OptA = "correct"
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Option_of_scalar_type_can_only_take_a_single_value_from_an_environment_variable_even_if_it_contains_separators()
|
||||
public async Task Option_of_non_scalar_type_can_use_an_environment_variable_as_fallback_and_extract_multiple_values()
|
||||
{
|
||||
// Arrange
|
||||
var schema = ApplicationSchema.Resolve(new[] {typeof(EnvironmentVariableCommand)});
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var input = CommandLineInput.Empty;
|
||||
var envVars = new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT"] = $"foo{Path.PathSeparator}bar"
|
||||
};
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var command = schema.InitializeEntryPoint(input, envVars);
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"cmd"},
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT_B"] = $"foo{Path.PathSeparator}bar"
|
||||
}
|
||||
);
|
||||
|
||||
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
|
||||
|
||||
// Assert
|
||||
command.Should().BeEquivalentTo(new EnvironmentVariableCommand
|
||||
exitCode.Should().Be(0);
|
||||
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||
{
|
||||
Option = $"foo{Path.PathSeparator}bar"
|
||||
OptB = new[] {"foo", "bar"}
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Option_of_scalar_type_can_use_an_environment_variable_as_fallback_regardless_of_separators()
|
||||
{
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"cmd"},
|
||||
new Dictionary<string, string>
|
||||
{
|
||||
["ENV_OPT_A"] = $"foo{Path.PathSeparator}bar"
|
||||
}
|
||||
);
|
||||
|
||||
var commandInstance = stdOut.GetString().DeserializeJson<WithEnvironmentVariablesCommand>();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
commandInstance.Should().BeEquivalentTo(new WithEnvironmentVariablesCommand
|
||||
{
|
||||
OptA = $"foo{Path.PathSeparator}bar"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ErrorReportingSpecs
|
||||
{
|
||||
[Command("exc")]
|
||||
private class GenericExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new Exception(Message);
|
||||
}
|
||||
|
||||
[Command("exc")]
|
||||
private class CommandExceptionCommand : ICommand
|
||||
{
|
||||
[CommandOption("code", 'c')]
|
||||
public int ExitCode { get; set; } = 1337;
|
||||
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(Message, ExitCode);
|
||||
}
|
||||
|
||||
[Command("exc")]
|
||||
private class ShowHelpTextOnlyCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(null, showHelp: true);
|
||||
}
|
||||
|
||||
[Command("exc sub")]
|
||||
private class ShowHelpTextOnlySubCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("exc")]
|
||||
private class ShowErrorMessageThenHelpTextCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) =>
|
||||
throw new CommandException("Error message.", showHelp: true);
|
||||
}
|
||||
|
||||
[Command("exc sub")]
|
||||
private class ShowErrorMessageThenHelpTextSubCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("exc")]
|
||||
private class StackTraceOnlyCommand : ICommand
|
||||
{
|
||||
[CommandOption("msg", 'm')]
|
||||
public string? Message { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => throw new CommandException(null);
|
||||
}
|
||||
|
||||
[Command("inv")]
|
||||
private class InvalidUserInputCommand : ICommand
|
||||
{
|
||||
[CommandOption("required", 'r')]
|
||||
public string? RequiredOption { get; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Commands;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class ErrorReportingSpecs
|
||||
public class ErrorReportingSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
@@ -17,222 +16,131 @@ namespace CliFx.Tests
|
||||
public async Task Command_may_throw_a_generic_exception_which_exits_and_prints_error_message_and_stack_trace()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(error: stdErr);
|
||||
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(GenericExceptionCommand))
|
||||
.AddCommand<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();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().ContainAll(
|
||||
stdOut.GetString().Should().BeEmpty();
|
||||
stdErr.GetString().Should().ContainAll(
|
||||
"System.Exception:",
|
||||
"Kaput", "at",
|
||||
"CliFx.Tests");
|
||||
"CliFx.Tests"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[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 (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CommandExceptionCommand))
|
||||
.AddCommand<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();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "-c", "69"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(69);
|
||||
stdErrData.Should().Be("Kaput");
|
||||
stdOut.GetString().Should().BeEmpty();
|
||||
stdErr.GetString().Trim().Should().Be("Kaput");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[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 (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(CommandExceptionCommand))
|
||||
.AddCommand<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();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().NotBeEmpty();
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_a_specialized_exception_which_shows_only_the_help_text()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
await using var stdErr = new MemoryStream();
|
||||
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ShowHelpTextOnlyCommand))
|
||||
.AddCommand(typeof(ShowHelpTextOnlySubCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"exc"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
var stdErrData = console.Output.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdErrData.Should().BeEmpty();
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text.",
|
||||
"Commands",
|
||||
"sub",
|
||||
"You can run", "to show help on a specific command."
|
||||
stdOut.GetString().Should().BeEmpty();
|
||||
stdErr.GetString().Should().ContainAll(
|
||||
"CliFx.Exceptions.CommandException:",
|
||||
"at",
|
||||
"CliFx.Tests"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_specialized_exception_which_shows_the_error_message_then_the_help_text()
|
||||
public async Task Command_may_throw_a_specialized_exception_which_exits_and_prints_help_text()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
||||
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ShowErrorMessageThenHelpTextCommand))
|
||||
.AddCommand(typeof(ShowErrorMessageThenHelpTextSubCommand))
|
||||
.AddCommand<CommandExceptionCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"exc"});
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdErrData.Should().Be("Error message.");
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text.",
|
||||
"Commands",
|
||||
"sub",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
_output.WriteLine(stdErrData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_may_throw_a_specialized_exception_which_shows_only_a_stack_trace_and_no_help_text()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(error: stdErr);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(GenericExceptionCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"exc", "-m", "Kaput"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "-m", "Kaput", "--show-help"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().ContainAll(
|
||||
"System.Exception:",
|
||||
"Kaput", "at",
|
||||
"CliFx.Tests");
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Usage",
|
||||
"Options",
|
||||
"-h|--help"
|
||||
);
|
||||
stdErr.GetString().Trim().Should().Be("Kaput");
|
||||
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Command_shows_help_text_on_exceptions_related_to_invalid_user_input()
|
||||
public async Task Command_shows_help_text_on_invalid_user_input()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
await using var stdErr = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut, error: stdErr);
|
||||
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(InvalidUserInputCommand))
|
||||
.AddCommand<DefaultCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
new[] {"not-a-valid-command", "-r", "foo"},
|
||||
new Dictionary<string, string>());
|
||||
|
||||
var stdErrData = console.Error.Encoding.GetString(stdErr.ToArray()).TrimEnd();
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
var exitCode = await application.RunAsync(new[] {"not-a-valid-command", "-r", "foo"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().NotBe(0);
|
||||
stdErrData.Should().ContainAll(
|
||||
"Can't find a command that matches the following arguments:",
|
||||
"not-a-valid-command"
|
||||
);
|
||||
stdOutData.Should().ContainAll(
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Usage",
|
||||
"[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text.",
|
||||
"Commands",
|
||||
"inv",
|
||||
"You can run", "to show help on a specific command."
|
||||
"-h|--help"
|
||||
);
|
||||
stdErr.GetString().Should().NotBeNullOrWhiteSpace();
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
_output.WriteLine(stdErrData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
_output.WriteLine(stdErr.GetString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class HelpTextSpecs
|
||||
{
|
||||
[Command(Description = "DefaultCommand description.")]
|
||||
private class DefaultCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", 'a', Description = "OptionA description.")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", 'b', Description = "OptionB description.")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd", Description = "NamedCommand description.")]
|
||||
private class NamedCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "param-a", Description = "ParameterA description.")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandOption("option-c", 'c', Description = "OptionC description.")]
|
||||
public string? OptionC { get; set; }
|
||||
|
||||
[CommandOption("option-d", 'd', Description = "OptionD description.")]
|
||||
public string? OptionD { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd sub", Description = "NamedSubCommand description.")]
|
||||
private class NamedSubCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "param-b", Description = "ParameterB description.")]
|
||||
public string? ParameterB { get; set; }
|
||||
|
||||
[CommandParameter(1, Name = "param-c", Description = "ParameterC description.")]
|
||||
public string? ParameterC { get; set; }
|
||||
|
||||
[CommandOption("option-e", 'e', Description = "OptionE description.")]
|
||||
public string? OptionE { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-params")]
|
||||
private class ParametersCommand : ICommand
|
||||
{
|
||||
[CommandParameter(0, Name = "first")]
|
||||
public string? ParameterA { get; set; }
|
||||
|
||||
[CommandParameter(10)]
|
||||
public int? ParameterB { get; set; }
|
||||
|
||||
[CommandParameter(20, Description = "A list of numbers", Name = "third list")]
|
||||
public IEnumerable<int>? ParameterC { get; set; }
|
||||
|
||||
[CommandOption("option", 'o')]
|
||||
public string? Option { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-req-opts")]
|
||||
private class RequiredOptionsCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-f", 'f', IsRequired = true)]
|
||||
public string? OptionF { get; set; }
|
||||
|
||||
[CommandOption("option-g", 'g', IsRequired = true)]
|
||||
public IEnumerable<int>? OptionG { get; set; }
|
||||
|
||||
[CommandOption("option-h", 'h')]
|
||||
public string? OptionH { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-enum-args")]
|
||||
private class EnumArgumentsCommand : ICommand
|
||||
{
|
||||
public enum TestEnum { Value1, Value2, Value3 };
|
||||
|
||||
[CommandParameter(0, Name = "value", Description = "Enum parameter.")]
|
||||
public TestEnum ParamA { get; set; }
|
||||
|
||||
[CommandOption("value", Description = "Enum option.", IsRequired = true)]
|
||||
public TestEnum OptionA { get; set; } = TestEnum.Value1;
|
||||
|
||||
[CommandOption("nullable-value", Description = "Nullable enum option.")]
|
||||
public TestEnum? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-env-vars")]
|
||||
private class EnvironmentVariableCommand : ICommand
|
||||
{
|
||||
[CommandOption("option-a", 'a', IsRequired = true, EnvironmentVariableName = "ENV_OPT_A")]
|
||||
public string? OptionA { get; set; }
|
||||
|
||||
[CommandOption("option-b", 'b', EnvironmentVariableName = "ENV_OPT_B")]
|
||||
public string? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,297 +1,153 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Commands;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class HelpTextSpecs
|
||||
public class HelpTextSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public HelpTextSpecs(ITestOutputHelper output) => _output = output;
|
||||
|
||||
[Fact]
|
||||
public async Task Version_information_can_be_requested_by_providing_the_version_option_without_other_arguments()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseVersionText("v6.9")
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"--version"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Be("v6.9");
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_by_providing_the_help_option()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseTitle("AppTitle")
|
||||
.UseVersionText("AppVer")
|
||||
.UseDescription("AppDesc")
|
||||
.UseExecutableName("AppExe")
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"AppTitle", "AppVer",
|
||||
"AppDesc",
|
||||
"Usage",
|
||||
"AppExe", "[command]", "[options]",
|
||||
"Options",
|
||||
"-a|--option-a", "OptionA description.",
|
||||
"-b|--option-b", "OptionB description.",
|
||||
"-h|--help", "Shows help text.",
|
||||
"--version", "Shows version information.",
|
||||
"Commands",
|
||||
"cmd", "NamedCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_on_a_specific_named_command()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Description",
|
||||
"NamedCommand description.",
|
||||
"Usage",
|
||||
"cmd", "[command]", "<param-a>", "[options]",
|
||||
"Parameters",
|
||||
"* param-a", "ParameterA description.",
|
||||
"Options",
|
||||
"-c|--option-c", "OptionC description.",
|
||||
"-d|--option-d", "OptionD description.",
|
||||
"-h|--help", "Shows help text.",
|
||||
"Commands",
|
||||
"sub", "SubCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_on_a_specific_named_sub_command()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd", "sub", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Description",
|
||||
"SubCommand description.",
|
||||
"Usage",
|
||||
"cmd sub", "<param-b>", "<param-c>", "[options]",
|
||||
"Parameters",
|
||||
"* param-b", "ParameterB description.",
|
||||
"* param-c", "ParameterC description.",
|
||||
"Options",
|
||||
"-e|--option-e", "OptionE description.",
|
||||
"-h|--help", "Shows help text."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_can_be_requested_without_specifying_command_even_if_default_command_is_not_defined()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(NamedCommand))
|
||||
.AddCommand(typeof(NamedSubCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"[command]",
|
||||
"Options",
|
||||
"-h|--help", "Shows help text.",
|
||||
"--version", "Shows version information.",
|
||||
"Commands",
|
||||
"cmd", "NamedCommand description.",
|
||||
"You can run", "to show help on a specific command."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_usage_format_which_lists_all_parameters()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ParametersCommand))
|
||||
.AddCommand<WithParametersCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-params", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-params", "<first>", "<parameterb>", "<third list...>", "[options]"
|
||||
"cmd", "<parama>", "<paramb>", "<paramc...>"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[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 (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(RequiredOptionsCommand))
|
||||
.AddCommand<WithRequiredOptionsCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-req-opts", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-req-opts", "--option-f <value>", "--option-g <values...>", "[options]",
|
||||
"cmd", "--opt-a <value>", "--opt-c <values...>", "[options]",
|
||||
"Options",
|
||||
"* -f|--option-f",
|
||||
"* -g|--option-g",
|
||||
"-h|--option-h"
|
||||
"* -a|--opt-a",
|
||||
"-b|--opt-b",
|
||||
"* -c|--opt-c"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_usage_format_which_lists_all_valid_values_for_enum_arguments()
|
||||
public async Task Help_text_shows_all_valid_values_for_enum_arguments()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(EnumArgumentsCommand))
|
||||
.AddCommand<WithEnumArgumentsCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] { "cmd-with-enum-args", "--help" });
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-enum-args", "[options]",
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Parameters",
|
||||
"value", "Valid values: Value1, Value2, Value3.",
|
||||
"enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||
"Options",
|
||||
"* --value", "Enum option.", "Valid values: Value1, Value2, Value3.",
|
||||
"--nullable-value", "Nullable enum option.", "Valid values: Value1, Value2, Value3."
|
||||
"--enum", "Valid values: \"Value1\", \"Value2\", \"Value3\".",
|
||||
"* --required-enum", "Valid values: \"Value1\", \"Value2\", \"Value3\"."
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_lists_environment_variable_names_for_options_that_have_them_defined()
|
||||
public async Task Help_text_shows_environment_variable_names_for_options_that_have_them_defined()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(EnvironmentVariableCommand))
|
||||
.AddCommand<WithEnvironmentVariablesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] {"cmd-with-env-vars", "--help"});
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Options",
|
||||
"* -a|--option-a", "Environment variable:", "ENV_OPT_A",
|
||||
"-b|--option-b", "Environment variable:", "ENV_OPT_B"
|
||||
"-a|--opt-a", "Environment variable:", "ENV_OPT_A",
|
||||
"-b|--opt-b", "Environment variable:", "ENV_OPT_B"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOutData);
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_default_values_for_non_required_options()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<WithDefaultValuesCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"cmd", "--help"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Options",
|
||||
"--obj", "Default: \"42\"",
|
||||
"--str", "Default: \"foo\"",
|
||||
"--str-empty", "Default: \"\"",
|
||||
"--str-array", "Default: \"foo\" \"bar\" \"baz\"",
|
||||
"--bool", "Default: \"True\"",
|
||||
"--char", "Default: \"t\"",
|
||||
"--int", "Default: \"1337\"",
|
||||
"--int-nullable", "Default: \"1337\"",
|
||||
"--int-array", "Default: \"1\" \"2\" \"3\"",
|
||||
"--timespan", "Default: \"02:03:00\"",
|
||||
"--enum", "Default: \"Value2\""
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
}
|
||||
}
|
||||
10
CliFx.Tests/Internal/JsonExtensions.cs
Normal file
10
CliFx.Tests/Internal/JsonExtensions.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CliFx.Tests.Internal
|
||||
{
|
||||
internal static class JsonExtensions
|
||||
{
|
||||
public static T DeserializeJson<T>(this string json) =>
|
||||
JsonConvert.DeserializeObject<T>(json);
|
||||
}
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class RoutingSpecs
|
||||
{
|
||||
[Command]
|
||||
private class DefaultCommand : ICommand
|
||||
{
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine("Hello world!");
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
[Command("concat", Description = "Concatenate strings.")]
|
||||
private class ConcatCommand : ICommand
|
||||
{
|
||||
[CommandOption('i', IsRequired = true, Description = "Input strings.")]
|
||||
public IReadOnlyList<string> Inputs { get; set; } = Array.Empty<string>();
|
||||
|
||||
[CommandOption('s', Description = "String separator.")]
|
||||
public string Separator { get; set; } = "";
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(string.Join(Separator, Inputs));
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
[Command("div", Description = "Divide one number by another.")]
|
||||
private class DivideCommand : ICommand
|
||||
{
|
||||
[CommandOption("dividend", 'D', IsRequired = true, Description = "The number to divide.")]
|
||||
public double Dividend { get; set; } = 0;
|
||||
|
||||
[CommandOption("divisor", 'd', IsRequired = true, Description = "The number to divide by.")]
|
||||
public double Divisor { get; set; } = 0;
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(Dividend / Divisor);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +1,235 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Tests.Commands;
|
||||
using FluentAssertions;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CliFx.Tests
|
||||
{
|
||||
public partial class RoutingSpecs
|
||||
public class RoutingSpecs
|
||||
{
|
||||
private readonly ITestOutputHelper _output;
|
||||
|
||||
public RoutingSpecs(ITestOutputHelper testOutput) => _output = testOutput;
|
||||
|
||||
[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 (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(ConcatCommand))
|
||||
.AddCommand(typeof(DivideCommand))
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.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();
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Be("Hello world!");
|
||||
}
|
||||
stdOut.GetString().Trim().Should().Be(DefaultCommand.ExpectedOutputText);
|
||||
|
||||
[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");
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[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 (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(DefaultCommand))
|
||||
.AddCommand(typeof(ConcatCommand))
|
||||
.AddCommand(typeof(DivideCommand))
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.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();
|
||||
var exitCode = await application.RunAsync(new[] {"named"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOutData.Should().Be("foo, bar");
|
||||
stdOut.GetString().Trim().Should().Be(NamedCommand.ExpectedOutputText);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Specific_named_sub_command_is_executed_if_provided_arguments_match_its_name()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"named", "sub"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Trim().Should().Be(NamedSubCommand.ExpectedOutputText);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_is_printed_if_no_arguments_were_provided_and_default_command_is_not_defined()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.UseConsole(console)
|
||||
.UseDescription("This will be visible in help")
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(Array.Empty<string>());
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().Contain("This will be visible in help");
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_is_printed_if_provided_arguments_contain_the_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"--help"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Default command description",
|
||||
"Usage"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_is_printed_if_provided_arguments_contain_the_help_option_even_if_default_command_is_not_defined()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.UseDescription("This will be visible in help")
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"--help"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().Contain("This will be visible in help");
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_for_a_specific_named_command_is_printed_if_provided_arguments_match_its_name_and_contain_the_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"named", "--help"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Named command description",
|
||||
"Usage",
|
||||
"named"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_for_a_specific_named_sub_command_is_printed_if_provided_arguments_match_its_name_and_contain_the_help_option()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"named", "sub", "--help"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Should().ContainAll(
|
||||
"Named sub command description",
|
||||
"Usage",
|
||||
"named", "sub"
|
||||
);
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Version_is_printed_if_the_only_provided_argument_is_the_version_option()
|
||||
{
|
||||
// Arrange
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand<DefaultCommand>()
|
||||
.AddCommand<NamedCommand>()
|
||||
.AddCommand<NamedSubCommand>()
|
||||
.UseVersionText("v6.9")
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(new[] {"--version"});
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.GetString().Trim().Should().Be("v6.9");
|
||||
|
||||
_output.WriteLine(stdOut.GetString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>1.2</Version>
|
||||
<Version>1.4</Version>
|
||||
<Company>Tyrrrz</Company>
|
||||
<Copyright>Copyright (C) Alexey Golub</Copyright>
|
||||
<LangVersion>latest</LangVersion>
|
||||
|
||||
@@ -28,7 +28,8 @@ namespace CliFx
|
||||
/// </summary>
|
||||
public ApplicationConfiguration(
|
||||
IReadOnlyList<Type> commandTypes,
|
||||
bool isDebugModeAllowed, bool isPreviewModeAllowed)
|
||||
bool isDebugModeAllowed,
|
||||
bool isPreviewModeAllowed)
|
||||
{
|
||||
CommandTypes = commandTypes;
|
||||
IsDebugModeAllowed = isDebugModeAllowed;
|
||||
|
||||
@@ -4,15 +4,17 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Command line application facade.
|
||||
/// </summary>
|
||||
public class CliApplication
|
||||
public partial class CliApplication
|
||||
{
|
||||
private readonly ApplicationMetadata _metadata;
|
||||
private readonly ApplicationConfiguration _configuration;
|
||||
@@ -36,42 +38,35 @@ namespace CliFx
|
||||
_helpTextWriter = new HelpTextWriter(metadata, console);
|
||||
}
|
||||
|
||||
private async ValueTask<int?> HandleDebugDirectiveAsync(CommandLineInput commandLineInput)
|
||||
private void WriteError(string message) => _console.WithForegroundColor(ConsoleColor.Red, () =>
|
||||
_console.Error.WriteLine(message));
|
||||
|
||||
private async ValueTask LaunchAndWaitForDebuggerAsync()
|
||||
{
|
||||
var isDebugMode = _configuration.IsDebugModeAllowed && commandLineInput.IsDebugDirectiveSpecified;
|
||||
if (!isDebugMode)
|
||||
return null;
|
||||
var processId = ProcessEx.GetCurrentProcessId();
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.Green, () =>
|
||||
_console.Output.WriteLine($"Attach debugger to PID {Process.GetCurrentProcess().Id} to continue."));
|
||||
_console.Output.WriteLine($"Attach debugger to PID {processId} to continue."));
|
||||
|
||||
Debugger.Launch();
|
||||
|
||||
while (!Debugger.IsAttached)
|
||||
await Task.Delay(100);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private int? HandlePreviewDirective(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||
private void WriteCommandLineInput(CommandInput input)
|
||||
{
|
||||
var isPreviewMode = _configuration.IsPreviewModeAllowed && commandLineInput.IsPreviewDirectiveSpecified;
|
||||
if (!isPreviewMode)
|
||||
return null;
|
||||
|
||||
var commandSchema = applicationSchema.TryFindCommand(commandLineInput, out var argumentOffset);
|
||||
|
||||
_console.Output.WriteLine("Parser preview:");
|
||||
|
||||
// Command name
|
||||
if (commandSchema != null && argumentOffset > 0)
|
||||
if (!string.IsNullOrWhiteSpace(input.CommandName))
|
||||
{
|
||||
_console.WithForegroundColor(ConsoleColor.Cyan, () =>
|
||||
_console.Output.Write(commandSchema.Name));
|
||||
_console.Output.Write(input.CommandName));
|
||||
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
// Parameters
|
||||
foreach (var parameter in commandLineInput.UnboundArguments.Skip(argumentOffset))
|
||||
foreach (var parameter in input.Parameters)
|
||||
{
|
||||
_console.Output.Write('<');
|
||||
|
||||
@@ -83,123 +78,132 @@ namespace CliFx
|
||||
}
|
||||
|
||||
// Options
|
||||
foreach (var option in commandLineInput.Options)
|
||||
foreach (var option in input.Options)
|
||||
{
|
||||
_console.Output.Write('[');
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.White, () =>
|
||||
_console.Output.Write(option));
|
||||
{
|
||||
// Alias
|
||||
_console.Output.Write(option.GetRawAlias());
|
||||
|
||||
// Values
|
||||
if (option.Values.Any())
|
||||
{
|
||||
_console.Output.Write(' ');
|
||||
_console.Output.Write(option.GetRawValues());
|
||||
}
|
||||
});
|
||||
|
||||
_console.Output.Write(']');
|
||||
_console.Output.Write(' ');
|
||||
}
|
||||
|
||||
_console.Output.WriteLine();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int? HandleVersionOption(CommandLineInput commandLineInput)
|
||||
{
|
||||
// Version option is available only on the default command (i.e. when arguments are not specified)
|
||||
var shouldRenderVersion = !commandLineInput.UnboundArguments.Any() && commandLineInput.IsVersionOptionSpecified;
|
||||
if (!shouldRenderVersion)
|
||||
return null;
|
||||
|
||||
_console.Output.WriteLine(_metadata.VersionText);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int? HandleHelpOption(ApplicationSchema applicationSchema, CommandLineInput commandLineInput)
|
||||
{
|
||||
// Help is rendered either when it's requested or when the user provides no arguments and there is no default command
|
||||
var shouldRenderHelp =
|
||||
commandLineInput.IsHelpOptionSpecified ||
|
||||
!applicationSchema.Commands.Any(c => c.IsDefault) && !commandLineInput.UnboundArguments.Any() && !commandLineInput.Options.Any();
|
||||
|
||||
if (!shouldRenderHelp)
|
||||
return null;
|
||||
|
||||
// Get the command schema that matches the input or use a dummy default command as a fallback
|
||||
var commandSchema =
|
||||
applicationSchema.TryFindCommand(commandLineInput) ??
|
||||
CommandSchema.StubDefaultCommand;
|
||||
|
||||
_helpTextWriter.Write(applicationSchema, commandSchema);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private async ValueTask<int> HandleCommandExecutionAsync(
|
||||
ApplicationSchema applicationSchema,
|
||||
CommandLineInput commandLineInput,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
await applicationSchema
|
||||
.InitializeEntryPoint(commandLineInput, environmentVariables, _typeActivator)
|
||||
.ExecuteAsync(_console);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handle <see cref="CommandException"/>s differently from the rest because we want to
|
||||
/// display it different based on whether we are showing the help text or not.
|
||||
/// </summary>
|
||||
private int HandleCliFxException(IReadOnlyList<string> commandLineArguments, CliFxException cfe)
|
||||
{
|
||||
var showHelp = cfe.ShowHelp;
|
||||
|
||||
var errorMessage = cfe.HasMessage
|
||||
? cfe.Message
|
||||
: cfe.ToString();
|
||||
|
||||
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(errorMessage));
|
||||
|
||||
if (showHelp)
|
||||
{
|
||||
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
|
||||
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
|
||||
var commandSchema = applicationSchema.TryFindCommand(commandLineInput) ??
|
||||
CommandSchema.StubDefaultCommand;
|
||||
_helpTextWriter.Write(applicationSchema, commandSchema);
|
||||
}
|
||||
|
||||
return cfe.ExitCode;
|
||||
}
|
||||
private ICommand GetCommandInstance(CommandSchema command) =>
|
||||
command != StubDefaultCommand.Schema
|
||||
? (ICommand) _typeActivator.CreateInstance(command.Type)
|
||||
: new StubDefaultCommand();
|
||||
|
||||
/// <summary>
|
||||
/// Runs the application with specified command line arguments and environment variables, and returns the exit code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
try
|
||||
{
|
||||
var applicationSchema = ApplicationSchema.Resolve(_configuration.CommandTypes);
|
||||
var commandLineInput = CommandLineInput.Parse(commandLineArguments);
|
||||
var root = RootSchema.Resolve(_configuration.CommandTypes);
|
||||
var input = CommandInput.Parse(commandLineArguments, root.GetCommandNames());
|
||||
|
||||
return
|
||||
await HandleDebugDirectiveAsync(commandLineInput) ??
|
||||
HandlePreviewDirective(applicationSchema, commandLineInput) ??
|
||||
HandleVersionOption(commandLineInput) ??
|
||||
HandleHelpOption(applicationSchema, commandLineInput) ??
|
||||
await HandleCommandExecutionAsync(applicationSchema, commandLineInput, environmentVariables);
|
||||
// Debug mode
|
||||
if (_configuration.IsDebugModeAllowed && input.IsDebugDirectiveSpecified)
|
||||
{
|
||||
await LaunchAndWaitForDebuggerAsync();
|
||||
}
|
||||
|
||||
// Preview mode
|
||||
if (_configuration.IsPreviewModeAllowed && input.IsPreviewDirectiveSpecified)
|
||||
{
|
||||
WriteCommandLineInput(input);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// Try to get the command matching the input or fallback to default
|
||||
var command =
|
||||
root.TryFindCommand(input.CommandName) ??
|
||||
root.TryFindDefaultCommand() ??
|
||||
StubDefaultCommand.Schema;
|
||||
|
||||
// Version option
|
||||
if (command.IsVersionOptionAvailable && input.IsVersionOptionSpecified)
|
||||
{
|
||||
_console.Output.WriteLine(_metadata.VersionText);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// Get command instance (also used in help text)
|
||||
var instance = GetCommandInstance(command);
|
||||
|
||||
// To avoid instantiating the command twice, we need to get default values
|
||||
// before the arguments are bound to the properties
|
||||
var defaultValues = command.GetArgumentValues(instance);
|
||||
|
||||
// Help option
|
||||
if (command.IsHelpOptionAvailable && input.IsHelpOptionSpecified ||
|
||||
command == StubDefaultCommand.Schema && !input.Parameters.Any() && !input.Options.Any())
|
||||
{
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
|
||||
// Bind arguments
|
||||
try
|
||||
{
|
||||
command.Bind(instance, input, environmentVariables);
|
||||
}
|
||||
// This may throw exceptions which are useful only to the end-user
|
||||
catch (CliFxException ex)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
|
||||
return ExitCode.FromException(ex);
|
||||
}
|
||||
|
||||
// Execute the command
|
||||
try
|
||||
{
|
||||
await instance.ExecuteAsync(_console);
|
||||
return ExitCode.Success;
|
||||
}
|
||||
// Swallow command exceptions and route them to the console
|
||||
catch (CommandException ex)
|
||||
{
|
||||
WriteError(ex.ToString());
|
||||
|
||||
if (ex.ShowHelp)
|
||||
_helpTextWriter.Write(root, command, defaultValues);
|
||||
|
||||
return ex.ExitCode;
|
||||
}
|
||||
}
|
||||
catch (CliFxException cfe)
|
||||
// To prevent the app from showing the annoying Windows troubleshooting dialog,
|
||||
// we handle all exceptions and route them to the console nicely.
|
||||
// However, we don't want to swallow unhandled exceptions when the debugger is attached,
|
||||
// because we still want the IDE to show them to the developer.
|
||||
catch (Exception ex) when (!Debugger.IsAttached)
|
||||
{
|
||||
// We want to catch exceptions in order to print errors and return correct exit codes.
|
||||
// Doing this also gets rid of the annoying Windows troubleshooting dialog that shows up on unhandled exceptions.
|
||||
var exitCode = HandleCliFxException(commandLineArguments, cfe);
|
||||
return exitCode;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// For all other errors, we just write the entire thing to stderr.
|
||||
_console.WithForegroundColor(ConsoleColor.Red, () => _console.Error.WriteLine(ex.ToString()));
|
||||
return ex.HResult;
|
||||
WriteError(ex.ToString());
|
||||
return ExitCode.FromException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,11 +211,17 @@ namespace CliFx
|
||||
/// Runs the application with specified command line arguments and returns the exit code.
|
||||
/// Environment variables are retrieved automatically.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments)
|
||||
{
|
||||
// Environment variable names are case-insensitive on Windows but are case-sensitive on Linux and macOS
|
||||
var environmentVariables = Environment.GetEnvironmentVariables()
|
||||
.Cast<DictionaryEntry>()
|
||||
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.OrdinalIgnoreCase);
|
||||
.ToDictionary(e => (string) e.Key, e => (string) e.Value, StringComparer.Ordinal);
|
||||
|
||||
return await RunAsync(commandLineArguments, environmentVariables);
|
||||
}
|
||||
@@ -220,6 +230,11 @@ namespace CliFx
|
||||
/// Runs the application and returns the exit code.
|
||||
/// Command line arguments and environment variables are retrieved automatically.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// If a <see cref="CommandException"/> is thrown during command execution, it will be handled and routed to the console.
|
||||
/// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
|
||||
/// this method will be handled and routed to the console as well.
|
||||
/// </remarks>
|
||||
public async ValueTask<int> RunAsync()
|
||||
{
|
||||
var commandLineArguments = Environment.GetCommandLineArgs()
|
||||
@@ -229,4 +244,25 @@ namespace CliFx
|
||||
return await RunAsync(commandLineArguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class CliApplication
|
||||
{
|
||||
private static class ExitCode
|
||||
{
|
||||
public const int Success = 0;
|
||||
|
||||
public static int FromException(Exception ex) =>
|
||||
ex is CommandException cmdEx
|
||||
? cmdEx.ExitCode
|
||||
: 1;
|
||||
}
|
||||
|
||||
[Command]
|
||||
private class StubDefaultCommand : ICommand
|
||||
{
|
||||
public static CommandSchema Schema { get; } = CommandSchema.TryResolve(typeof(StubDefaultCommand))!;
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
@@ -33,6 +34,12 @@ namespace CliFx
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a command of specified type to the application.
|
||||
/// </summary>
|
||||
public CliApplicationBuilder AddCommand<TCommand>() where TCommand : ICommand =>
|
||||
AddCommand(typeof(TCommand));
|
||||
|
||||
/// <summary>
|
||||
/// Adds multiple commands to the application.
|
||||
/// </summary>
|
||||
@@ -158,9 +165,9 @@ namespace CliFx
|
||||
/// </summary>
|
||||
public CliApplication Build()
|
||||
{
|
||||
_title ??= GetDefaultTitle() ?? "App";
|
||||
_executableName ??= GetDefaultExecutableName() ?? "app";
|
||||
_versionText ??= GetDefaultVersionText() ?? "v1.0";
|
||||
_title ??= TryGetDefaultTitle() ?? "App";
|
||||
_executableName ??= TryGetDefaultExecutableName() ?? "app";
|
||||
_versionText ??= TryGetDefaultVersionText() ?? "v1.0";
|
||||
_console ??= new SystemConsole();
|
||||
_typeActivator ??= new DefaultTypeActivator();
|
||||
|
||||
@@ -178,9 +185,9 @@ namespace CliFx
|
||||
// Entry assembly is null in tests
|
||||
private static Assembly? EntryAssembly => LazyEntryAssembly.Value;
|
||||
|
||||
private static string? GetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||
private static string? TryGetDefaultTitle() => EntryAssembly?.GetName().Name;
|
||||
|
||||
private static string? GetDefaultExecutableName()
|
||||
private static string? TryGetDefaultExecutableName()
|
||||
{
|
||||
var entryAssemblyLocation = EntryAssembly?.Location;
|
||||
|
||||
@@ -192,9 +199,9 @@ namespace CliFx
|
||||
: Path.GetFileNameWithoutExtension(entryAssemblyLocation);
|
||||
}
|
||||
|
||||
private static string? GetDefaultVersionText() =>
|
||||
private static string? TryGetDefaultVersionText() =>
|
||||
EntryAssembly != null
|
||||
? $"v{EntryAssembly.GetName().Version}"
|
||||
? $"v{EntryAssembly.GetName().Version.ToSemanticString()}"
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@@ -23,15 +23,6 @@
|
||||
<Nullable>annotations</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||
<_Parameter1>$(AssemblyName).Tests</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
|
||||
<_Parameter1>$(AssemblyName).Analyzers</_Parameter1>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx
|
||||
@@ -18,6 +17,6 @@ namespace CliFx
|
||||
|
||||
/// <inheritdoc />
|
||||
public object CreateInstance(Type type) =>
|
||||
_func(type) ?? throw CliFxException.DelegateActivatorReceivedNull(type);
|
||||
_func(type) ?? throw CliFxException.DelegateActivatorReturnedNull(type);
|
||||
}
|
||||
}
|
||||
@@ -1,252 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CliFx.Exceptions;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal partial class ApplicationSchema
|
||||
{
|
||||
public IReadOnlyList<CommandSchema> Commands { get; }
|
||||
|
||||
public ApplicationSchema(IReadOnlyList<CommandSchema> commands)
|
||||
{
|
||||
Commands = commands;
|
||||
}
|
||||
|
||||
public CommandSchema? TryFindParentCommand(string? childCommandName)
|
||||
{
|
||||
// Default command has no parent
|
||||
if (string.IsNullOrWhiteSpace(childCommandName))
|
||||
return null;
|
||||
|
||||
// Try to find the parent command by repeatedly biting off chunks of its name
|
||||
var route = childCommandName.Split(' ');
|
||||
for (var i = route.Length - 1; i >= 1; i--)
|
||||
{
|
||||
var potentialParentCommandName = string.Join(" ", route.Take(i));
|
||||
var matchingParentCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialParentCommandName));
|
||||
|
||||
if (matchingParentCommand != null)
|
||||
return matchingParentCommand;
|
||||
}
|
||||
|
||||
// If there's no parent - fall back to default command
|
||||
return Commands.FirstOrDefault(c => c.IsDefault);
|
||||
}
|
||||
|
||||
public IReadOnlyList<CommandSchema> GetChildCommands(string? parentCommandName) =>
|
||||
!string.IsNullOrWhiteSpace(parentCommandName) || Commands.Any(c => c.IsDefault)
|
||||
? Commands.Where(c => TryFindParentCommand(c.Name)?.MatchesName(parentCommandName) == true).ToArray()
|
||||
: Commands.Where(c => !string.IsNullOrWhiteSpace(c.Name) && TryFindParentCommand(c.Name) == null).ToArray();
|
||||
|
||||
// TODO: this out parameter is not a really nice design
|
||||
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput, out int argumentOffset)
|
||||
{
|
||||
// Try to find the command that contains the most of the input arguments in its name
|
||||
for (var i = commandLineInput.UnboundArguments.Count; i >= 0; i--)
|
||||
{
|
||||
var potentialCommandName = string.Join(" ", commandLineInput.UnboundArguments.Take(i));
|
||||
var matchingCommand = Commands.FirstOrDefault(c => c.MatchesName(potentialCommandName));
|
||||
|
||||
if (matchingCommand != null)
|
||||
{
|
||||
argumentOffset = i;
|
||||
return matchingCommand;
|
||||
}
|
||||
}
|
||||
|
||||
argumentOffset = 0;
|
||||
return Commands.FirstOrDefault(c => c.IsDefault);
|
||||
}
|
||||
|
||||
public CommandSchema? TryFindCommand(CommandLineInput commandLineInput) =>
|
||||
TryFindCommand(commandLineInput, out _);
|
||||
|
||||
public ICommand InitializeEntryPoint(
|
||||
CommandLineInput commandLineInput,
|
||||
IReadOnlyDictionary<string, string> environmentVariables,
|
||||
ITypeActivator activator)
|
||||
{
|
||||
var command = TryFindCommand(commandLineInput, out var argumentOffset) ??
|
||||
throw CliFxException.CannotFindMatchingCommand(commandLineInput);
|
||||
|
||||
var parameterInputs = argumentOffset == 0
|
||||
? commandLineInput.UnboundArguments.ToArray()
|
||||
: commandLineInput.UnboundArguments.Skip(argumentOffset).ToArray();
|
||||
|
||||
return command.CreateInstance(parameterInputs, commandLineInput.Options, environmentVariables, activator);
|
||||
}
|
||||
|
||||
public ICommand InitializeEntryPoint(
|
||||
CommandLineInput commandLineInput,
|
||||
IReadOnlyDictionary<string, string> environmentVariables) =>
|
||||
InitializeEntryPoint(commandLineInput, environmentVariables, new DefaultTypeActivator());
|
||||
|
||||
public ICommand InitializeEntryPoint(CommandLineInput commandLineInput) =>
|
||||
InitializeEntryPoint(commandLineInput, new Dictionary<string, string>());
|
||||
|
||||
public override string ToString() => string.Join(Environment.NewLine, Commands);
|
||||
}
|
||||
|
||||
internal partial class ApplicationSchema
|
||||
{
|
||||
private static void ValidateParameters(CommandSchema command)
|
||||
{
|
||||
var duplicateOrderGroup = command.Parameters
|
||||
.GroupBy(a => a.Order)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateOrderGroup != null)
|
||||
{
|
||||
throw CliFxException.CommandParametersDuplicateOrder(
|
||||
command,
|
||||
duplicateOrderGroup.Key,
|
||||
duplicateOrderGroup.ToArray());
|
||||
}
|
||||
|
||||
var duplicateNameGroup = command.Parameters
|
||||
.Where(a => !string.IsNullOrWhiteSpace(a.Name))
|
||||
.GroupBy(a => a.Name!, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
throw CliFxException.CommandParametersDuplicateName(
|
||||
command,
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray());
|
||||
}
|
||||
|
||||
var nonScalarParameters = command.Parameters
|
||||
.Where(p => !p.IsScalar)
|
||||
.ToArray();
|
||||
|
||||
if (nonScalarParameters.Length > 1)
|
||||
{
|
||||
throw CliFxException.CommandParametersTooManyNonScalar(
|
||||
command,
|
||||
nonScalarParameters);
|
||||
}
|
||||
|
||||
var nonLastNonScalarParameter = command.Parameters
|
||||
.OrderByDescending(a => a.Order)
|
||||
.Skip(1)
|
||||
.LastOrDefault(p => !p.IsScalar);
|
||||
|
||||
if (nonLastNonScalarParameter != null)
|
||||
{
|
||||
throw CliFxException.CommandParametersNonLastNonScalar(
|
||||
command,
|
||||
nonLastNonScalarParameter);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateOptions(CommandSchema command)
|
||||
{
|
||||
var noNameGroup = command.Options
|
||||
.Where(o => o.ShortName == null && string.IsNullOrWhiteSpace(o.Name))
|
||||
.ToArray();
|
||||
|
||||
if (noNameGroup.Any())
|
||||
{
|
||||
throw CliFxException.CommandOptionsNoName(
|
||||
command,
|
||||
noNameGroup.ToArray());
|
||||
}
|
||||
|
||||
var invalidLengthNameGroup = command.Options
|
||||
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
||||
.Where(o => o.Name!.Length <= 1)
|
||||
.ToArray();
|
||||
|
||||
if (invalidLengthNameGroup.Any())
|
||||
{
|
||||
throw CliFxException.CommandOptionsInvalidLengthName(
|
||||
command,
|
||||
invalidLengthNameGroup);
|
||||
}
|
||||
|
||||
var duplicateNameGroup = command.Options
|
||||
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
||||
.GroupBy(o => o.Name!, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
throw CliFxException.CommandOptionsDuplicateName(
|
||||
command,
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray());
|
||||
}
|
||||
|
||||
var duplicateShortNameGroup = command.Options
|
||||
.Where(o => o.ShortName != null)
|
||||
.GroupBy(o => o.ShortName!.Value)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateShortNameGroup != null)
|
||||
{
|
||||
throw CliFxException.CommandOptionsDuplicateShortName(
|
||||
command,
|
||||
duplicateShortNameGroup.Key,
|
||||
duplicateShortNameGroup.ToArray());
|
||||
}
|
||||
|
||||
var duplicateEnvironmentVariableNameGroup = command.Options
|
||||
.Where(o => !string.IsNullOrWhiteSpace(o.EnvironmentVariableName))
|
||||
.GroupBy(o => o.EnvironmentVariableName!, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateEnvironmentVariableNameGroup != null)
|
||||
{
|
||||
throw CliFxException.CommandOptionsDuplicateEnvironmentVariableName(
|
||||
command,
|
||||
duplicateEnvironmentVariableNameGroup.Key,
|
||||
duplicateEnvironmentVariableNameGroup.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
||||
{
|
||||
if (!commands.Any())
|
||||
{
|
||||
throw CliFxException.CommandsNotRegistered();
|
||||
}
|
||||
|
||||
var duplicateNameGroup = commands
|
||||
.GroupBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(duplicateNameGroup.Key))
|
||||
throw CliFxException.CommandsDuplicateName(
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray());
|
||||
|
||||
throw CliFxException.CommandsTooManyDefaults(duplicateNameGroup.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public static ApplicationSchema Resolve(IReadOnlyList<Type> commandTypes)
|
||||
{
|
||||
var commands = new List<CommandSchema>();
|
||||
|
||||
foreach (var commandType in commandTypes)
|
||||
{
|
||||
var command = CommandSchema.TryResolve(commandType) ??
|
||||
throw CliFxException.InvalidCommandType(commandType);
|
||||
|
||||
ValidateParameters(command);
|
||||
ValidateOptions(command);
|
||||
|
||||
commands.Add(command);
|
||||
}
|
||||
|
||||
ValidateCommands(commands);
|
||||
|
||||
return new ApplicationSchema(commands);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,28 +4,27 @@ using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal abstract partial class CommandArgumentSchema
|
||||
{
|
||||
public PropertyInfo Property { get; }
|
||||
// Property can be null on built-in arguments (help and version options)
|
||||
public PropertyInfo? Property { get; }
|
||||
|
||||
public string? Description { get; }
|
||||
|
||||
public abstract string DisplayName { get; }
|
||||
|
||||
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == null;
|
||||
|
||||
protected CommandArgumentSchema(PropertyInfo property, string? description)
|
||||
protected CommandArgumentSchema(PropertyInfo? property, string? description)
|
||||
{
|
||||
Property = property;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
||||
Property.PropertyType != typeof(string)
|
||||
Property != null && Property.PropertyType != typeof(string)
|
||||
? Property.PropertyType.GetEnumerableUnderlyingType()
|
||||
: null;
|
||||
|
||||
@@ -49,18 +48,18 @@ namespace CliFx.Domain
|
||||
? ConvertScalar(value, nullableUnderlyingType)
|
||||
: null;
|
||||
|
||||
// String-constructable
|
||||
var stringConstructor = GetStringConstructor(targetType);
|
||||
// String-constructible
|
||||
var stringConstructor = targetType.GetConstructor(new[] {typeof(string)});
|
||||
if (stringConstructor != null)
|
||||
return stringConstructor.Invoke(new object[] {value!});
|
||||
|
||||
// String-parseable (with format provider)
|
||||
var parseMethodWithFormatProvider = GetStaticParseMethodWithFormatProvider(targetType);
|
||||
var parseMethodWithFormatProvider = targetType.GetStaticParseMethod(true);
|
||||
if (parseMethodWithFormatProvider != null)
|
||||
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, ConversionFormatProvider});
|
||||
return parseMethodWithFormatProvider.Invoke(null, new object[] {value!, FormatProvider});
|
||||
|
||||
// String-parseable (without format provider)
|
||||
var parseMethod = GetStaticParseMethod(targetType);
|
||||
var parseMethod = targetType.GetStaticParseMethod();
|
||||
if (parseMethod != null)
|
||||
return parseMethod.Invoke(null, new object[] {value!});
|
||||
}
|
||||
@@ -84,7 +83,7 @@ namespace CliFx.Domain
|
||||
if (targetEnumerableType.IsAssignableFrom(arrayType))
|
||||
return array;
|
||||
|
||||
// Constructable from an array
|
||||
// Constructible from an array
|
||||
var arrayConstructor = targetEnumerableType.GetConstructor(new[] {arrayType});
|
||||
if (arrayConstructor != null)
|
||||
return arrayConstructor.Invoke(new object[] {array});
|
||||
@@ -94,6 +93,10 @@ namespace CliFx.Domain
|
||||
|
||||
private object? Convert(IReadOnlyList<string> values)
|
||||
{
|
||||
// Short-circuit built-in arguments
|
||||
if (Property == null)
|
||||
return null;
|
||||
|
||||
var targetType = Property.PropertyType;
|
||||
var enumerableUnderlyingType = TryGetEnumerableArgumentUnderlyingType();
|
||||
|
||||
@@ -111,34 +114,32 @@ namespace CliFx.Domain
|
||||
}
|
||||
}
|
||||
|
||||
public void Inject(ICommand command, IReadOnlyList<string> values) =>
|
||||
Property.SetValue(command, Convert(values));
|
||||
public void BindOn(ICommand command, IReadOnlyList<string> values) =>
|
||||
Property?.SetValue(command, Convert(values));
|
||||
|
||||
public void Inject(ICommand command, params string[] values) =>
|
||||
Inject(command, (IReadOnlyList<string>) values);
|
||||
public void BindOn(ICommand command, params string[] values) =>
|
||||
BindOn(command, (IReadOnlyList<string>) values);
|
||||
|
||||
public IReadOnlyList<string> GetValidValues()
|
||||
{
|
||||
var result = new List<string>();
|
||||
|
||||
// Some arguments may have this as null due to a hack that enables built-in options
|
||||
if (Property == null)
|
||||
return result;
|
||||
return Array.Empty<string>();
|
||||
|
||||
var underlyingPropertyType =
|
||||
Property.PropertyType.GetNullableUnderlyingType() ?? Property.PropertyType;
|
||||
var underlyingType =
|
||||
Property.PropertyType.GetNullableUnderlyingType() ??
|
||||
Property.PropertyType;
|
||||
|
||||
// Enum
|
||||
if (underlyingPropertyType.IsEnum)
|
||||
result.AddRange(Enum.GetNames(underlyingPropertyType));
|
||||
if (underlyingType.IsEnum)
|
||||
return Enum.GetNames(underlyingType);
|
||||
|
||||
return result;
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandArgumentSchema
|
||||
{
|
||||
private static readonly IFormatProvider ConversionFormatProvider = CultureInfo.InvariantCulture;
|
||||
private static readonly IFormatProvider FormatProvider = CultureInfo.InvariantCulture;
|
||||
|
||||
private static readonly IReadOnlyDictionary<Type, Func<string?, object?>> PrimitiveConverters =
|
||||
new Dictionary<Type, Func<string?, object?>>
|
||||
@@ -147,33 +148,20 @@ namespace CliFx.Domain
|
||||
[typeof(string)] = v => v,
|
||||
[typeof(bool)] = v => string.IsNullOrWhiteSpace(v) || bool.Parse(v),
|
||||
[typeof(char)] = v => v.Single(),
|
||||
[typeof(sbyte)] = v => sbyte.Parse(v, ConversionFormatProvider),
|
||||
[typeof(byte)] = v => byte.Parse(v, ConversionFormatProvider),
|
||||
[typeof(short)] = v => short.Parse(v, ConversionFormatProvider),
|
||||
[typeof(ushort)] = v => ushort.Parse(v, ConversionFormatProvider),
|
||||
[typeof(int)] = v => int.Parse(v, ConversionFormatProvider),
|
||||
[typeof(uint)] = v => uint.Parse(v, ConversionFormatProvider),
|
||||
[typeof(long)] = v => long.Parse(v, ConversionFormatProvider),
|
||||
[typeof(ulong)] = v => ulong.Parse(v, ConversionFormatProvider),
|
||||
[typeof(float)] = v => float.Parse(v, ConversionFormatProvider),
|
||||
[typeof(double)] = v => double.Parse(v, ConversionFormatProvider),
|
||||
[typeof(decimal)] = v => decimal.Parse(v, ConversionFormatProvider),
|
||||
[typeof(DateTime)] = v => DateTime.Parse(v, ConversionFormatProvider),
|
||||
[typeof(DateTimeOffset)] = v => DateTimeOffset.Parse(v, ConversionFormatProvider),
|
||||
[typeof(TimeSpan)] = v => TimeSpan.Parse(v, ConversionFormatProvider),
|
||||
[typeof(sbyte)] = v => sbyte.Parse(v, FormatProvider),
|
||||
[typeof(byte)] = v => byte.Parse(v, FormatProvider),
|
||||
[typeof(short)] = v => short.Parse(v, FormatProvider),
|
||||
[typeof(ushort)] = v => ushort.Parse(v, FormatProvider),
|
||||
[typeof(int)] = v => int.Parse(v, FormatProvider),
|
||||
[typeof(uint)] = v => uint.Parse(v, FormatProvider),
|
||||
[typeof(long)] = v => long.Parse(v, FormatProvider),
|
||||
[typeof(ulong)] = v => ulong.Parse(v, FormatProvider),
|
||||
[typeof(float)] = v => float.Parse(v, FormatProvider),
|
||||
[typeof(double)] = v => double.Parse(v, FormatProvider),
|
||||
[typeof(decimal)] = v => decimal.Parse(v, FormatProvider),
|
||||
[typeof(DateTime)] = v => DateTime.Parse(v, FormatProvider),
|
||||
[typeof(DateTimeOffset)] = v => DateTimeOffset.Parse(v, FormatProvider),
|
||||
[typeof(TimeSpan)] = v => TimeSpan.Parse(v, FormatProvider),
|
||||
};
|
||||
|
||||
private static ConstructorInfo? GetStringConstructor(Type type) =>
|
||||
type.GetConstructor(new[] {typeof(string)});
|
||||
|
||||
private static MethodInfo? GetStaticParseMethod(Type type) =>
|
||||
type.GetMethod("Parse",
|
||||
BindingFlags.Public | BindingFlags.Static,
|
||||
null, new[] {typeof(string)}, null);
|
||||
|
||||
private static MethodInfo? GetStaticParseMethodWithFormatProvider(Type type) =>
|
||||
type.GetMethod("Parse",
|
||||
BindingFlags.Public | BindingFlags.Static,
|
||||
null, new[] {typeof(string), typeof(IFormatProvider)}, null);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
@@ -10,11 +11,9 @@ namespace CliFx.Domain
|
||||
|
||||
public bool IsPreviewDirective => string.Equals(Name, "preview", StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
public CommandDirectiveInput(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
public CommandDirectiveInput(string name) => Name = name;
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public override string ToString() => $"[{Name}]";
|
||||
}
|
||||
}
|
||||
240
CliFx/Domain/CommandInput.cs
Normal file
240
CliFx/Domain/CommandInput.cs
Normal file
@@ -0,0 +1,240 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal partial class CommandInput
|
||||
{
|
||||
public IReadOnlyList<CommandDirectiveInput> Directives { get; }
|
||||
|
||||
public string? CommandName { get; }
|
||||
|
||||
public IReadOnlyList<CommandParameterInput> Parameters { get; }
|
||||
|
||||
public IReadOnlyList<CommandOptionInput> Options { get; }
|
||||
|
||||
public bool IsDebugDirectiveSpecified => Directives.Any(d => d.IsDebugDirective);
|
||||
|
||||
public bool IsPreviewDirectiveSpecified => Directives.Any(d => d.IsPreviewDirective);
|
||||
|
||||
public bool IsHelpOptionSpecified => Options.Any(o => o.IsHelpOption);
|
||||
|
||||
public bool IsVersionOptionSpecified => Options.Any(o => o.IsVersionOption);
|
||||
|
||||
public CommandInput(
|
||||
IReadOnlyList<CommandDirectiveInput> directives,
|
||||
string? commandName,
|
||||
IReadOnlyList<CommandParameterInput> parameters,
|
||||
IReadOnlyList<CommandOptionInput> options)
|
||||
{
|
||||
Directives = directives;
|
||||
CommandName = commandName;
|
||||
Parameters = parameters;
|
||||
Options = options;
|
||||
}
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public override string ToString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
foreach (var directive in Directives)
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(directive);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(CommandName))
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(CommandName);
|
||||
}
|
||||
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(parameter);
|
||||
}
|
||||
|
||||
foreach (var option in Options)
|
||||
{
|
||||
buffer
|
||||
.AppendIfNotEmpty(' ')
|
||||
.Append(option);
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandInput
|
||||
{
|
||||
private static IReadOnlyList<CommandDirectiveInput> ParseDirectives(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ref int index)
|
||||
{
|
||||
var result = new List<CommandDirectiveInput>();
|
||||
|
||||
for (; index < commandLineArguments.Count; index++)
|
||||
{
|
||||
var argument = commandLineArguments[index];
|
||||
|
||||
if (!argument.StartsWith('[') || !argument.EndsWith(']'))
|
||||
break;
|
||||
|
||||
var name = argument.Substring(1, argument.Length - 2);
|
||||
result.Add(new CommandDirectiveInput(name));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static string? ParseCommandName(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ISet<string> commandNames,
|
||||
ref int index)
|
||||
{
|
||||
var buffer = new List<string>();
|
||||
|
||||
var commandName = default(string?);
|
||||
var lastIndex = index;
|
||||
|
||||
// We need to look ahead to see if we can match as many consecutive arguments to a command name as possible
|
||||
for (var i = index; i < commandLineArguments.Count; i++)
|
||||
{
|
||||
var argument = commandLineArguments[i];
|
||||
buffer.Add(argument);
|
||||
|
||||
var potentialCommandName = buffer.JoinToString(" ");
|
||||
|
||||
if (commandNames.Contains(potentialCommandName))
|
||||
{
|
||||
commandName = potentialCommandName;
|
||||
lastIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the index only if command name was found in the arguments
|
||||
if (!string.IsNullOrWhiteSpace(commandName))
|
||||
index = lastIndex + 1;
|
||||
|
||||
return commandName;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<CommandParameterInput> ParseParameters(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ref int index)
|
||||
{
|
||||
var result = new List<CommandParameterInput>();
|
||||
|
||||
for (; index < commandLineArguments.Count; index++)
|
||||
{
|
||||
var argument = commandLineArguments[index];
|
||||
|
||||
if (argument.StartsWith('-'))
|
||||
break;
|
||||
|
||||
result.Add(new CommandParameterInput(argument));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static IReadOnlyList<CommandOptionInput> ParseOptions(
|
||||
IReadOnlyList<string> commandLineArguments,
|
||||
ref int index)
|
||||
{
|
||||
var result = new List<CommandOptionInput>();
|
||||
|
||||
var currentOptionAlias = default(string?);
|
||||
var currentOptionValues = new List<string>();
|
||||
|
||||
for (; index < commandLineArguments.Count; index++)
|
||||
{
|
||||
var argument = commandLineArguments[index];
|
||||
|
||||
// Name
|
||||
if (argument.StartsWith("--", StringComparison.Ordinal))
|
||||
{
|
||||
// Flush previous
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
|
||||
|
||||
currentOptionAlias = argument.Substring(2);
|
||||
currentOptionValues = new List<string>();
|
||||
}
|
||||
// Short name
|
||||
else if (argument.StartsWith('-'))
|
||||
{
|
||||
foreach (var alias in argument.Substring(1))
|
||||
{
|
||||
// Flush previous
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
|
||||
|
||||
currentOptionAlias = alias.AsString();
|
||||
currentOptionValues = new List<string>();
|
||||
}
|
||||
}
|
||||
// Value
|
||||
else if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
{
|
||||
currentOptionValues.Add(argument);
|
||||
}
|
||||
}
|
||||
|
||||
// Flush last option
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
result.Add(new CommandOptionInput(currentOptionAlias, currentOptionValues));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static CommandInput Parse(IReadOnlyList<string> commandLineArguments, IReadOnlyList<string> availableCommandNames)
|
||||
{
|
||||
var availableCommandNamesSet = availableCommandNames.ToHashSet(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
var index = 0;
|
||||
|
||||
var directives = ParseDirectives(
|
||||
commandLineArguments,
|
||||
ref index
|
||||
);
|
||||
|
||||
var commandName = ParseCommandName(
|
||||
commandLineArguments,
|
||||
availableCommandNamesSet,
|
||||
ref index
|
||||
);
|
||||
|
||||
var parameters = ParseParameters(
|
||||
commandLineArguments,
|
||||
ref index
|
||||
);
|
||||
|
||||
var options = ParseOptions(
|
||||
commandLineArguments,
|
||||
ref index
|
||||
);
|
||||
|
||||
return new CommandInput(directives, commandName, parameters, options);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandInput
|
||||
{
|
||||
public static CommandInput Empty { get; } = new CommandInput(
|
||||
Array.Empty<CommandDirectiveInput>(),
|
||||
null,
|
||||
Array.Empty<CommandParameterInput>(),
|
||||
Array.Empty<CommandOptionInput>()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,163 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using CliFx.Internal;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal partial class CommandLineInput
|
||||
{
|
||||
public IReadOnlyList<CommandDirectiveInput> Directives { get; }
|
||||
|
||||
public IReadOnlyList<CommandUnboundArgumentInput> UnboundArguments { get; }
|
||||
|
||||
public IReadOnlyList<CommandOptionInput> Options { get; }
|
||||
|
||||
public bool IsDebugDirectiveSpecified => Directives.Any(d => d.IsDebugDirective);
|
||||
|
||||
public bool IsPreviewDirectiveSpecified => Directives.Any(d => d.IsPreviewDirective);
|
||||
|
||||
public bool IsHelpOptionSpecified => Options.Any(o => o.IsHelpOption);
|
||||
|
||||
public bool IsVersionOptionSpecified => Options.Any(o => o.IsVersionOption);
|
||||
|
||||
public CommandLineInput(
|
||||
IReadOnlyList<CommandDirectiveInput> directives,
|
||||
IReadOnlyList<CommandUnboundArgumentInput> unboundArguments,
|
||||
IReadOnlyList<CommandOptionInput> options)
|
||||
{
|
||||
Directives = directives;
|
||||
UnboundArguments = unboundArguments;
|
||||
Options = options;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
foreach (var directive in Directives)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
buffer.Append(directive);
|
||||
}
|
||||
|
||||
foreach (var argument in UnboundArguments)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
buffer.Append(argument);
|
||||
}
|
||||
|
||||
foreach (var option in Options)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
buffer.Append(option);
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandLineInput
|
||||
{
|
||||
public static CommandLineInput Parse(IReadOnlyList<string> commandLineArguments)
|
||||
{
|
||||
var builder = new CommandLineInputBuilder();
|
||||
|
||||
var currentOptionAlias = "";
|
||||
var currentOptionValues = new List<string>();
|
||||
|
||||
bool TryParseDirective(string argument)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
return false;
|
||||
|
||||
if (!argument.StartsWith("[", StringComparison.OrdinalIgnoreCase) ||
|
||||
!argument.EndsWith("]", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
var directive = argument.Substring(1, argument.Length - 2);
|
||||
builder.AddDirective(directive);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryParseArgument(string argument)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
return false;
|
||||
|
||||
builder.AddUnboundArgument(argument);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryParseOptionName(string argument)
|
||||
{
|
||||
if (!argument.StartsWith("--", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||
|
||||
currentOptionAlias = argument.Substring(2);
|
||||
currentOptionValues = new List<string>();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryParseOptionShortName(string argument)
|
||||
{
|
||||
if (!argument.StartsWith("-", StringComparison.OrdinalIgnoreCase))
|
||||
return false;
|
||||
|
||||
foreach (var c in argument.Substring(1))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||
|
||||
currentOptionAlias = c.AsString();
|
||||
currentOptionValues = new List<string>();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TryParseOptionValue(string argument)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
return false;
|
||||
|
||||
currentOptionValues.Add(argument);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var argument in commandLineArguments)
|
||||
{
|
||||
var _ =
|
||||
TryParseOptionName(argument) ||
|
||||
TryParseOptionShortName(argument) ||
|
||||
TryParseDirective(argument) ||
|
||||
TryParseArgument(argument) ||
|
||||
TryParseOptionValue(argument);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(currentOptionAlias))
|
||||
builder.AddOption(currentOptionAlias, currentOptionValues);
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandLineInput
|
||||
{
|
||||
private static IReadOnlyList<CommandDirectiveInput> EmptyDirectives { get; } = new CommandDirectiveInput[0];
|
||||
|
||||
private static IReadOnlyList<CommandUnboundArgumentInput> EmptyUnboundArguments { get; } = new CommandUnboundArgumentInput[0];
|
||||
|
||||
private static IReadOnlyList<CommandOptionInput> EmptyOptions { get; } = new CommandOptionInput[0];
|
||||
|
||||
public static CommandLineInput Empty { get; } = new CommandLineInput(EmptyDirectives, EmptyUnboundArguments, EmptyOptions);
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal class CommandLineInputBuilder
|
||||
{
|
||||
private readonly List<CommandDirectiveInput> _directives = new List<CommandDirectiveInput>();
|
||||
private readonly List<CommandUnboundArgumentInput> _unboundArguments = new List<CommandUnboundArgumentInput>();
|
||||
private readonly List<CommandOptionInput> _options = new List<CommandOptionInput>();
|
||||
|
||||
public CommandLineInputBuilder AddDirective(CommandDirectiveInput directive)
|
||||
{
|
||||
_directives.Add(directive);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandLineInputBuilder AddDirective(string directive) =>
|
||||
AddDirective(new CommandDirectiveInput(directive));
|
||||
|
||||
public CommandLineInputBuilder AddUnboundArgument(CommandUnboundArgumentInput unboundArgument)
|
||||
{
|
||||
_unboundArguments.Add(unboundArgument);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandLineInputBuilder AddUnboundArgument(string unboundArgument) =>
|
||||
AddUnboundArgument(new CommandUnboundArgumentInput(unboundArgument));
|
||||
|
||||
public CommandLineInputBuilder AddOption(CommandOptionInput option)
|
||||
{
|
||||
_options.Add(option);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandLineInputBuilder AddOption(string optionAlias, IReadOnlyList<string> values) =>
|
||||
AddOption(new CommandOptionInput(optionAlias, values));
|
||||
|
||||
public CommandLineInputBuilder AddOption(string optionAlias, params string[] values) =>
|
||||
AddOption(optionAlias, (IReadOnlyList<string>) values);
|
||||
|
||||
public CommandLineInput Build() => new CommandLineInput(_directives, _unboundArguments, _options);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using CliFx.Internal;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
@@ -8,11 +9,6 @@ namespace CliFx.Domain
|
||||
{
|
||||
public string Alias { get; }
|
||||
|
||||
public string DisplayAlias =>
|
||||
Alias.Length > 1
|
||||
? $"--{Alias}"
|
||||
: $"-{Alias}";
|
||||
|
||||
public IReadOnlyList<string> Values { get; }
|
||||
|
||||
public bool IsHelpOption => CommandOptionSchema.HelpOption.MatchesNameOrShortName(Alias);
|
||||
@@ -25,28 +21,16 @@ namespace CliFx.Domain
|
||||
Values = values;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GetRawAlias() => Alias switch
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
{Length: 0} => Alias,
|
||||
{Length: 1} => $"-{Alias}",
|
||||
_ => $"--{Alias}"
|
||||
};
|
||||
|
||||
buffer.Append(DisplayAlias);
|
||||
public string GetRawValues() => Values.Select(v => v.Quote()).JoinToString(" ");
|
||||
|
||||
foreach (var value in Values)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
|
||||
var isEscaped = value.Contains(" ");
|
||||
|
||||
if (isEscaped)
|
||||
buffer.Append('"');
|
||||
|
||||
buffer.Append(value);
|
||||
|
||||
if (isEscaped)
|
||||
buffer.Append('"');
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
[ExcludeFromCodeCoverage]
|
||||
public override string ToString() => $"{GetRawAlias()} {GetRawValues()}";
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
@@ -12,16 +13,12 @@ namespace CliFx.Domain
|
||||
|
||||
public char? ShortName { get; }
|
||||
|
||||
public override string DisplayName => !string.IsNullOrWhiteSpace(Name)
|
||||
? $"--{Name}"
|
||||
: $"-{ShortName}";
|
||||
|
||||
public string? EnvironmentVariableName { get; }
|
||||
|
||||
public bool IsRequired { get; }
|
||||
|
||||
public CommandOptionSchema(
|
||||
PropertyInfo property,
|
||||
PropertyInfo? property,
|
||||
string? name,
|
||||
char? shortName,
|
||||
string? environmentVariableName,
|
||||
@@ -49,29 +46,38 @@ namespace CliFx.Domain
|
||||
|
||||
public bool MatchesEnvironmentVariableName(string environmentVariableName) =>
|
||||
!string.IsNullOrWhiteSpace(EnvironmentVariableName) &&
|
||||
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.OrdinalIgnoreCase);
|
||||
string.Equals(EnvironmentVariableName, environmentVariableName, StringComparison.Ordinal);
|
||||
|
||||
public override string ToString()
|
||||
public string GetUserFacingDisplayString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name))
|
||||
{
|
||||
buffer.Append("--");
|
||||
buffer.Append(Name);
|
||||
buffer
|
||||
.Append("--")
|
||||
.Append(Name);
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name) && ShortName != null)
|
||||
{
|
||||
buffer.Append('|');
|
||||
}
|
||||
|
||||
if (ShortName != null)
|
||||
{
|
||||
buffer.Append('-');
|
||||
buffer.Append(ShortName);
|
||||
buffer
|
||||
.Append('-')
|
||||
.Append(ShortName);
|
||||
}
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ('{GetUserFacingDisplayString()}')";
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public override string ToString() => GetInternalDisplayString();
|
||||
}
|
||||
|
||||
internal partial class CommandOptionSchema
|
||||
@@ -82,9 +88,12 @@ namespace CliFx.Domain
|
||||
if (attribute == null)
|
||||
return null;
|
||||
|
||||
// The user may mistakenly specify dashes, thinking it's required, so trim them
|
||||
var name = attribute.Name?.TrimStart('-');
|
||||
|
||||
return new CommandOptionSchema(
|
||||
property,
|
||||
attribute.Name,
|
||||
name,
|
||||
attribute.ShortName,
|
||||
attribute.EnvironmentVariableName,
|
||||
attribute.IsRequired,
|
||||
@@ -96,9 +105,9 @@ namespace CliFx.Domain
|
||||
internal partial class CommandOptionSchema
|
||||
{
|
||||
public static CommandOptionSchema HelpOption { get; } =
|
||||
new CommandOptionSchema(null!, "help", 'h', null, false, "Shows help text.");
|
||||
new CommandOptionSchema(null, "help", 'h', null, false, "Shows help text.");
|
||||
|
||||
public static CommandOptionSchema VersionOption { get; } =
|
||||
new CommandOptionSchema(null!, "version", null, null, false, "Shows version information.");
|
||||
new CommandOptionSchema(null, "version", null, null, false, "Shows version information.");
|
||||
}
|
||||
}
|
||||
14
CliFx/Domain/CommandParameterInput.cs
Normal file
14
CliFx/Domain/CommandParameterInput.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal class CommandParameterInput
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public CommandParameterInput(string value) => Value = value;
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public override string ToString() => Value;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Reflection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using CliFx.Attributes;
|
||||
|
||||
@@ -8,31 +9,31 @@ namespace CliFx.Domain
|
||||
{
|
||||
public int Order { get; }
|
||||
|
||||
public string? Name { get; }
|
||||
public string Name { get; }
|
||||
|
||||
public override string DisplayName =>
|
||||
!string.IsNullOrWhiteSpace(Name)
|
||||
? Name
|
||||
: Property.Name.ToLowerInvariant();
|
||||
|
||||
public CommandParameterSchema(PropertyInfo property, int order, string? name, string? description)
|
||||
public CommandParameterSchema(PropertyInfo? property, int order, string name, string? description)
|
||||
: base(property, description)
|
||||
{
|
||||
Order = order;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GetUserFacingDisplayString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
buffer
|
||||
.Append('<')
|
||||
.Append(DisplayName)
|
||||
.Append(Name)
|
||||
.Append('>');
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
public string GetInternalDisplayString() => $"{Property?.Name ?? "<implicit>"} ([{Order}] {GetUserFacingDisplayString()})";
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public override string ToString() => GetInternalDisplayString();
|
||||
}
|
||||
|
||||
internal partial class CommandParameterSchema
|
||||
@@ -43,10 +44,12 @@ namespace CliFx.Domain
|
||||
if (attribute == null)
|
||||
return null;
|
||||
|
||||
var name = attribute.Name ?? property.Name.ToLowerInvariant();
|
||||
|
||||
return new CommandParameterSchema(
|
||||
property,
|
||||
attribute.Order,
|
||||
attribute.Name,
|
||||
name,
|
||||
attribute.Description
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
@@ -24,6 +25,10 @@ namespace CliFx.Domain
|
||||
|
||||
public IReadOnlyList<CommandOptionSchema> Options { get; }
|
||||
|
||||
public bool IsHelpOptionAvailable => Options.Contains(CommandOptionSchema.HelpOption);
|
||||
|
||||
public bool IsVersionOptionAvailable => Options.Contains(CommandOptionSchema.VersionOption);
|
||||
|
||||
public CommandSchema(
|
||||
Type type,
|
||||
string? name,
|
||||
@@ -34,28 +39,42 @@ namespace CliFx.Domain
|
||||
Type = type;
|
||||
Name = name;
|
||||
Description = description;
|
||||
Options = options;
|
||||
Parameters = parameters;
|
||||
Options = options;
|
||||
}
|
||||
|
||||
public bool MatchesName(string? name) => string.Equals(name, Name, StringComparison.OrdinalIgnoreCase);
|
||||
public bool MatchesName(string? name) =>
|
||||
!string.IsNullOrWhiteSpace(Name)
|
||||
? string.Equals(name, Name, StringComparison.OrdinalIgnoreCase)
|
||||
: string.IsNullOrWhiteSpace(name);
|
||||
|
||||
public IReadOnlyList<CommandOptionSchema> GetBuiltInOptions()
|
||||
public IEnumerable<CommandArgumentSchema> GetArguments()
|
||||
{
|
||||
var result = new List<CommandOptionSchema>(2);
|
||||
foreach (var parameter in Parameters)
|
||||
yield return parameter;
|
||||
|
||||
var helpOption = CommandOptionSchema.HelpOption;
|
||||
var versionOption = CommandOptionSchema.VersionOption;
|
||||
foreach (var option in Options)
|
||||
yield return option;
|
||||
}
|
||||
|
||||
result.Add(helpOption);
|
||||
public IReadOnlyDictionary<CommandArgumentSchema, object?> GetArgumentValues(ICommand instance)
|
||||
{
|
||||
var result = new Dictionary<CommandArgumentSchema, object?>();
|
||||
|
||||
if (IsDefault)
|
||||
result.Add(versionOption);
|
||||
foreach (var argument in GetArguments())
|
||||
{
|
||||
// Skip built-in arguments
|
||||
if (argument.Property == null)
|
||||
continue;
|
||||
|
||||
var value = argument.Property.GetValue(instance);
|
||||
result[argument] = value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void InjectParameters(ICommand command, IReadOnlyList<CommandUnboundArgumentInput> parameterInputs)
|
||||
private void BindParameters(ICommand instance, IReadOnlyList<CommandParameterInput> parameterInputs)
|
||||
{
|
||||
// All inputs must be bound
|
||||
var remainingParameterInputs = parameterInputs.ToList();
|
||||
@@ -68,14 +87,14 @@ namespace CliFx.Domain
|
||||
|
||||
for (var i = 0; i < scalarParameters.Length; i++)
|
||||
{
|
||||
var scalarParameter = scalarParameters[i];
|
||||
var parameter = scalarParameters[i];
|
||||
|
||||
var scalarParameterInput = i < parameterInputs.Count
|
||||
var scalarInput = i < parameterInputs.Count
|
||||
? parameterInputs[i]
|
||||
: throw CliFxException.ParameterNotSet(scalarParameter);
|
||||
: throw CliFxException.ParameterNotSet(parameter);
|
||||
|
||||
scalarParameter.Inject(command, scalarParameterInput.Value);
|
||||
remainingParameterInputs.Remove(scalarParameterInput);
|
||||
parameter.BindOn(instance, scalarInput.Value);
|
||||
remainingParameterInputs.Remove(scalarInput);
|
||||
}
|
||||
|
||||
// Non-scalar parameter (only one is allowed)
|
||||
@@ -85,21 +104,27 @@ namespace CliFx.Domain
|
||||
|
||||
if (nonScalarParameter != null)
|
||||
{
|
||||
var nonScalarParameterValues = parameterInputs.Skip(scalarParameters.Length).Select(i => i.Value).ToArray();
|
||||
var nonScalarValues = parameterInputs
|
||||
.Skip(scalarParameters.Length)
|
||||
.Select(p => p.Value)
|
||||
.ToArray();
|
||||
|
||||
nonScalarParameter.Inject(command, nonScalarParameterValues);
|
||||
// Parameters are required by default and so a non-scalar parameter must
|
||||
// be bound to at least one value
|
||||
if(!nonScalarValues.Any())
|
||||
throw CliFxException.ParameterNotSet(nonScalarParameter);
|
||||
|
||||
nonScalarParameter.BindOn(instance, nonScalarValues);
|
||||
remainingParameterInputs.Clear();
|
||||
}
|
||||
|
||||
// Ensure all inputs were bound
|
||||
if (remainingParameterInputs.Any())
|
||||
{
|
||||
throw CliFxException.UnrecognizedParametersProvided(remainingParameterInputs);
|
||||
}
|
||||
}
|
||||
|
||||
private void InjectOptions(
|
||||
ICommand command,
|
||||
private void BindOptions(
|
||||
ICommand instance,
|
||||
IReadOnlyList<CommandOptionInput> optionInputs,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
@@ -113,19 +138,17 @@ namespace CliFx.Domain
|
||||
foreach (var (name, value) in environmentVariables)
|
||||
{
|
||||
var option = Options.FirstOrDefault(o => o.MatchesEnvironmentVariableName(name));
|
||||
if (option == null)
|
||||
continue;
|
||||
|
||||
if (option != null)
|
||||
{
|
||||
var values = option.IsScalar
|
||||
? new[] {value}
|
||||
: value.Split(Path.PathSeparator);
|
||||
var values = option.IsScalar
|
||||
? new[] {value}
|
||||
: value.Split(Path.PathSeparator);
|
||||
|
||||
option.Inject(command, values);
|
||||
unsetRequiredOptions.Remove(option);
|
||||
}
|
||||
option.BindOn(instance, values);
|
||||
unsetRequiredOptions.Remove(option);
|
||||
}
|
||||
|
||||
// TODO: refactor this part? I wrote this while sick
|
||||
// Direct input
|
||||
foreach (var option in Options)
|
||||
{
|
||||
@@ -133,67 +156,60 @@ namespace CliFx.Domain
|
||||
.Where(i => option.MatchesNameOrShortName(i.Alias))
|
||||
.ToArray();
|
||||
|
||||
if (inputs.Any())
|
||||
{
|
||||
var inputValues = inputs.SelectMany(i => i.Values).ToArray();
|
||||
option.Inject(command, inputValues);
|
||||
// Skip if the inputs weren't provided for this option
|
||||
if (!inputs.Any())
|
||||
continue;
|
||||
|
||||
foreach (var input in inputs)
|
||||
remainingOptionInputs.Remove(input);
|
||||
var inputValues = inputs.SelectMany(i => i.Values).ToArray();
|
||||
option.BindOn(instance, inputValues);
|
||||
|
||||
if (inputValues.Any())
|
||||
unsetRequiredOptions.Remove(option);
|
||||
}
|
||||
}
|
||||
remainingOptionInputs.RemoveRange(inputs);
|
||||
|
||||
// Ensure all required options were set
|
||||
if (unsetRequiredOptions.Any())
|
||||
{
|
||||
throw CliFxException.RequiredOptionsNotSet(unsetRequiredOptions);
|
||||
// Required option implies that the value has to be set and also be non-empty
|
||||
if (inputValues.Any())
|
||||
unsetRequiredOptions.Remove(option);
|
||||
}
|
||||
|
||||
// Ensure all inputs were bound
|
||||
if (remainingOptionInputs.Any())
|
||||
{
|
||||
throw CliFxException.UnrecognizedOptionsProvided(remainingOptionInputs);
|
||||
}
|
||||
|
||||
// Ensure all required options were set
|
||||
if (unsetRequiredOptions.Any())
|
||||
throw CliFxException.RequiredOptionsNotSet(unsetRequiredOptions);
|
||||
}
|
||||
|
||||
public ICommand CreateInstance(
|
||||
IReadOnlyList<CommandUnboundArgumentInput> parameterInputs,
|
||||
IReadOnlyList<CommandOptionInput> optionInputs,
|
||||
IReadOnlyDictionary<string, string> environmentVariables,
|
||||
ITypeActivator activator)
|
||||
public void Bind(
|
||||
ICommand instance,
|
||||
CommandInput input,
|
||||
IReadOnlyDictionary<string, string> environmentVariables)
|
||||
{
|
||||
var command = (ICommand) activator.CreateInstance(Type);
|
||||
|
||||
InjectParameters(command, parameterInputs);
|
||||
InjectOptions(command, optionInputs, environmentVariables);
|
||||
|
||||
return command;
|
||||
BindParameters(instance, input.Parameters);
|
||||
BindOptions(instance, input.Options, environmentVariables);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
public string GetInternalDisplayString()
|
||||
{
|
||||
var buffer = new StringBuilder();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(Name))
|
||||
buffer.Append(Name);
|
||||
// Type
|
||||
buffer.Append(Type.FullName);
|
||||
|
||||
foreach (var parameter in Parameters)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
buffer.Append(parameter);
|
||||
}
|
||||
|
||||
foreach (var option in Options)
|
||||
{
|
||||
buffer.AppendIfNotEmpty(' ');
|
||||
buffer.Append(option);
|
||||
}
|
||||
// Name
|
||||
buffer
|
||||
.Append(' ')
|
||||
.Append('(')
|
||||
.Append(IsDefault
|
||||
? "<default command>"
|
||||
: $"'{Name}'"
|
||||
)
|
||||
.Append(')');
|
||||
|
||||
return buffer.ToString();
|
||||
}
|
||||
|
||||
[ExcludeFromCodeCoverage]
|
||||
public override string ToString() => GetInternalDisplayString();
|
||||
}
|
||||
|
||||
internal partial class CommandSchema
|
||||
@@ -211,6 +227,12 @@ namespace CliFx.Domain
|
||||
|
||||
var attribute = type.GetCustomAttribute<CommandAttribute>();
|
||||
|
||||
var name = attribute?.Name;
|
||||
|
||||
var builtInOptions = string.IsNullOrWhiteSpace(name)
|
||||
? new[] {CommandOptionSchema.HelpOption, CommandOptionSchema.VersionOption}
|
||||
: new[] {CommandOptionSchema.HelpOption};
|
||||
|
||||
var parameters = type.GetProperties()
|
||||
.Select(CommandParameterSchema.TryResolve)
|
||||
.Where(p => p != null)
|
||||
@@ -219,21 +241,16 @@ namespace CliFx.Domain
|
||||
var options = type.GetProperties()
|
||||
.Select(CommandOptionSchema.TryResolve)
|
||||
.Where(o => o != null)
|
||||
.Concat(builtInOptions)
|
||||
.ToArray();
|
||||
|
||||
return new CommandSchema(
|
||||
type,
|
||||
attribute?.Name,
|
||||
name,
|
||||
attribute?.Description,
|
||||
parameters!,
|
||||
options!
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandSchema
|
||||
{
|
||||
public static CommandSchema StubDefaultCommand { get; } =
|
||||
new CommandSchema(null!, null, null, new CommandParameterSchema[0], new CommandOptionSchema[0]);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal class CommandUnboundArgumentInput
|
||||
{
|
||||
public string Value { get; }
|
||||
|
||||
public CommandUnboundArgumentInput(string value)
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public override string ToString() => Value;
|
||||
}
|
||||
}
|
||||
@@ -1,346 +1,388 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using CliFx.Internal;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal class HelpTextWriter
|
||||
internal partial class HelpTextWriter
|
||||
{
|
||||
private readonly ApplicationMetadata _metadata;
|
||||
private readonly IConsole _console;
|
||||
|
||||
private int _column;
|
||||
private int _row;
|
||||
|
||||
private bool IsEmpty => _column == 0 && _row == 0;
|
||||
|
||||
public HelpTextWriter(ApplicationMetadata metadata, IConsole console)
|
||||
{
|
||||
_metadata = metadata;
|
||||
_console = console;
|
||||
}
|
||||
|
||||
public void Write(ApplicationSchema applicationSchema, CommandSchema command)
|
||||
private void Write(char value)
|
||||
{
|
||||
var column = 0;
|
||||
var row = 0;
|
||||
_console.Output.Write(value);
|
||||
_column++;
|
||||
}
|
||||
|
||||
var childCommands = applicationSchema.GetChildCommands(command.Name);
|
||||
private void Write(string value)
|
||||
{
|
||||
_console.Output.Write(value);
|
||||
_column += value.Length;
|
||||
}
|
||||
|
||||
bool IsEmpty() => column == 0 && row == 0;
|
||||
private void Write(ConsoleColor foregroundColor, string value)
|
||||
{
|
||||
_console.WithForegroundColor(foregroundColor, () => Write(value));
|
||||
}
|
||||
|
||||
void Render(string text)
|
||||
private void WriteLine()
|
||||
{
|
||||
_console.Output.WriteLine();
|
||||
_column = 0;
|
||||
_row++;
|
||||
}
|
||||
|
||||
private void WriteVerticalMargin(int size = 1)
|
||||
{
|
||||
for (var i = 0; i < size; i++)
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
private void WriteHorizontalMargin(int size = 2)
|
||||
{
|
||||
for (var i = 0; i < size; i++)
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
private void WriteColumnMargin(int columnSize = 20, int offsetSize = 2)
|
||||
{
|
||||
if (_column + offsetSize < columnSize)
|
||||
WriteHorizontalMargin(columnSize - _column);
|
||||
else
|
||||
WriteHorizontalMargin(offsetSize);
|
||||
}
|
||||
|
||||
private void WriteHeader(string text)
|
||||
{
|
||||
Write(ConsoleColor.Magenta, text);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
private void WriteApplicationInfo()
|
||||
{
|
||||
// Title and version
|
||||
Write(ConsoleColor.Yellow, _metadata.Title);
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Yellow, _metadata.VersionText);
|
||||
WriteLine();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(_metadata.Description))
|
||||
{
|
||||
_console.Output.Write(text);
|
||||
WriteHorizontalMargin();
|
||||
Write(_metadata.Description);
|
||||
WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
column += text.Length;
|
||||
private void WriteCommandDescription(CommandSchema command)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Description))
|
||||
return;
|
||||
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
WriteHeader("Description");
|
||||
|
||||
WriteHorizontalMargin();
|
||||
Write(command.Description);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
private void WriteCommandUsage(CommandSchema command, IReadOnlyList<CommandSchema> childCommands)
|
||||
{
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
WriteHeader("Usage");
|
||||
|
||||
// Exe name
|
||||
WriteHorizontalMargin();
|
||||
Write(_metadata.ExecutableName);
|
||||
|
||||
// Command name
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, command.Name);
|
||||
}
|
||||
|
||||
void RenderNewLine()
|
||||
// Child command placeholder
|
||||
if (childCommands.Any())
|
||||
{
|
||||
_console.Output.WriteLine();
|
||||
|
||||
column = 0;
|
||||
row++;
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, "[command]");
|
||||
}
|
||||
|
||||
void RenderMargin(int lines = 1)
|
||||
// Parameters
|
||||
foreach (var parameter in command.Parameters)
|
||||
{
|
||||
if (!IsEmpty())
|
||||
Write(' ');
|
||||
Write(parameter.IsScalar
|
||||
? $"<{parameter.Name}>"
|
||||
: $"<{parameter.Name}...>"
|
||||
);
|
||||
}
|
||||
|
||||
// Required options
|
||||
foreach (var option in command.Options.Where(o => o.IsRequired))
|
||||
{
|
||||
Write(' ');
|
||||
Write(ConsoleColor.White, !string.IsNullOrWhiteSpace(option.Name)
|
||||
? $"--{option.Name}"
|
||||
: $"-{option.ShortName}"
|
||||
);
|
||||
|
||||
Write(' ');
|
||||
Write(option.IsScalar
|
||||
? "<value>"
|
||||
: "<values...>"
|
||||
);
|
||||
}
|
||||
|
||||
// Options placeholder
|
||||
Write(' ');
|
||||
Write(ConsoleColor.White, "[options]");
|
||||
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
private void WriteCommandParameters(CommandSchema command)
|
||||
{
|
||||
if (!command.Parameters.Any())
|
||||
return;
|
||||
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
WriteHeader("Parameters");
|
||||
|
||||
foreach (var parameter in command.Parameters.OrderBy(p => p.Order))
|
||||
{
|
||||
Write(ConsoleColor.Red, "* ");
|
||||
Write(ConsoleColor.White, $"{parameter.Name}");
|
||||
|
||||
WriteColumnMargin();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(parameter.Description))
|
||||
{
|
||||
for (var i = 0; i < lines; i++)
|
||||
RenderNewLine();
|
||||
Write(parameter.Description);
|
||||
Write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void RenderIndent(int spaces = 2)
|
||||
{
|
||||
Render(' '.Repeat(spaces));
|
||||
}
|
||||
|
||||
void RenderColumnIndent(int spaces = 20, int margin = 2)
|
||||
{
|
||||
if (column + margin < spaces)
|
||||
// Valid values
|
||||
var validValues = parameter.GetValidValues();
|
||||
if (validValues.Any())
|
||||
{
|
||||
RenderIndent(spaces - column);
|
||||
Write($"Valid values: {FormatValidValues(validValues)}.");
|
||||
}
|
||||
|
||||
WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteCommandOptions(
|
||||
CommandSchema command,
|
||||
IReadOnlyDictionary<CommandArgumentSchema, object?> argumentDefaultValues)
|
||||
{
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
WriteHeader("Options");
|
||||
|
||||
foreach (var option in command.Options.OrderByDescending(o => o.IsRequired))
|
||||
{
|
||||
if (option.IsRequired)
|
||||
{
|
||||
Write(ConsoleColor.Red, "* ");
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderIndent(margin);
|
||||
WriteHorizontalMargin();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderWithColor(string text, ConsoleColor foregroundColor)
|
||||
{
|
||||
_console.WithForegroundColor(foregroundColor, () => Render(text));
|
||||
}
|
||||
// Short name
|
||||
if (option.ShortName != null)
|
||||
{
|
||||
Write(ConsoleColor.White, $"-{option.ShortName}");
|
||||
}
|
||||
|
||||
void RenderHeader(string text)
|
||||
{
|
||||
RenderWithColor(text, ConsoleColor.Magenta);
|
||||
RenderNewLine();
|
||||
}
|
||||
// Separator
|
||||
if (!string.IsNullOrWhiteSpace(option.Name) && option.ShortName != null)
|
||||
{
|
||||
Write('|');
|
||||
}
|
||||
|
||||
void RenderApplicationInfo()
|
||||
{
|
||||
if (!command.IsDefault)
|
||||
return;
|
||||
// Name
|
||||
if (!string.IsNullOrWhiteSpace(option.Name))
|
||||
{
|
||||
Write(ConsoleColor.White, $"--{option.Name}");
|
||||
}
|
||||
|
||||
// Title and version
|
||||
RenderWithColor(_metadata.Title, ConsoleColor.Yellow);
|
||||
Render(" ");
|
||||
RenderWithColor(_metadata.VersionText, ConsoleColor.Yellow);
|
||||
RenderNewLine();
|
||||
WriteColumnMargin();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(_metadata.Description))
|
||||
if (!string.IsNullOrWhiteSpace(option.Description))
|
||||
{
|
||||
Render(_metadata.Description);
|
||||
RenderNewLine();
|
||||
Write(option.Description);
|
||||
Write(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void RenderDescription()
|
||||
// Valid values
|
||||
var validValues = option.GetValidValues();
|
||||
if (validValues.Any())
|
||||
{
|
||||
Write($"Valid values: {FormatValidValues(validValues)}.");
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
// Environment variable
|
||||
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
|
||||
{
|
||||
Write($"Environment variable: \"{option.EnvironmentVariableName}\".");
|
||||
Write(' ');
|
||||
}
|
||||
|
||||
// Default value
|
||||
if (!option.IsRequired)
|
||||
{
|
||||
var defaultValue = argumentDefaultValues.GetValueOrDefault(option);
|
||||
var defaultValueFormatted = FormatDefaultValue(defaultValue);
|
||||
if (defaultValueFormatted != null)
|
||||
{
|
||||
Write($"Default: {defaultValueFormatted}.");
|
||||
}
|
||||
}
|
||||
|
||||
WriteLine();
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteCommandChildren(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandSchema> childCommands)
|
||||
{
|
||||
if (!childCommands.Any())
|
||||
return;
|
||||
|
||||
if (!IsEmpty)
|
||||
WriteVerticalMargin();
|
||||
|
||||
WriteHeader("Commands");
|
||||
|
||||
foreach (var childCommand in childCommands)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(command.Description))
|
||||
return;
|
||||
var relativeCommandName = !string.IsNullOrWhiteSpace(command.Name)
|
||||
? childCommand.Name!.Substring(command.Name.Length).Trim()
|
||||
: childCommand.Name!;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Description");
|
||||
// Name
|
||||
WriteHorizontalMargin();
|
||||
Write(ConsoleColor.Cyan, relativeCommandName);
|
||||
|
||||
RenderIndent();
|
||||
Render(command.Description);
|
||||
RenderNewLine();
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(childCommand.Description))
|
||||
{
|
||||
WriteColumnMargin();
|
||||
Write(childCommand.Description);
|
||||
}
|
||||
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
void RenderUsage()
|
||||
// Child command help tip
|
||||
WriteVerticalMargin();
|
||||
Write("You can run `");
|
||||
Write(_metadata.ExecutableName);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
RenderMargin();
|
||||
RenderHeader("Usage");
|
||||
|
||||
// Exe name
|
||||
RenderIndent();
|
||||
Render(_metadata.ExecutableName);
|
||||
|
||||
// Command name
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor(command.Name, ConsoleColor.Cyan);
|
||||
}
|
||||
|
||||
// Child command placeholder
|
||||
if (childCommands.Any())
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor("[command]", ConsoleColor.Cyan);
|
||||
}
|
||||
|
||||
// Parameters
|
||||
foreach (var parameter in command.Parameters)
|
||||
{
|
||||
Render(" ");
|
||||
Render(parameter.IsScalar
|
||||
? $"<{parameter.DisplayName}>"
|
||||
: $"<{parameter.DisplayName}...>");
|
||||
}
|
||||
|
||||
// Required options
|
||||
var requiredOptionSchemas = command.Options
|
||||
.Where(o => o.IsRequired)
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in requiredOptionSchemas)
|
||||
{
|
||||
Render(" ");
|
||||
if (!string.IsNullOrWhiteSpace(option.Name))
|
||||
{
|
||||
RenderWithColor($"--{option.Name}", ConsoleColor.White);
|
||||
Render(" ");
|
||||
Render(option.IsScalar
|
||||
? "<value>"
|
||||
: "<values...>");
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderWithColor($"-{option.ShortName}", ConsoleColor.White);
|
||||
Render(" ");
|
||||
Render(option.IsScalar
|
||||
? "<value>"
|
||||
: "<values...>");
|
||||
}
|
||||
}
|
||||
|
||||
// Options placeholder
|
||||
if (command.Options.Count != requiredOptionSchemas.Length)
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor("[options]", ConsoleColor.White);
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, command.Name);
|
||||
}
|
||||
|
||||
void RenderParameters()
|
||||
{
|
||||
if (!command.Parameters.Any())
|
||||
return;
|
||||
Write(' ');
|
||||
Write(ConsoleColor.Cyan, "[command]");
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Parameters");
|
||||
Write(' ');
|
||||
Write(ConsoleColor.White, "--help");
|
||||
|
||||
var parameters = command.Parameters
|
||||
.OrderBy(p => p.Order)
|
||||
.ToArray();
|
||||
Write("` to show help on a specific command.");
|
||||
|
||||
foreach (var parameter in parameters)
|
||||
{
|
||||
RenderWithColor("* ", ConsoleColor.Red);
|
||||
RenderWithColor($"{parameter.DisplayName}", ConsoleColor.White);
|
||||
WriteLine();
|
||||
}
|
||||
|
||||
RenderColumnIndent();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(parameter.Description))
|
||||
{
|
||||
Render(parameter.Description);
|
||||
Render(" ");
|
||||
}
|
||||
|
||||
// Valid values
|
||||
var validValues = parameter.GetValidValues();
|
||||
if (validValues.Any())
|
||||
{
|
||||
Render($"Valid values: {string.Join(", ", validValues)}.");
|
||||
Render(" ");
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderOptions()
|
||||
{
|
||||
RenderMargin();
|
||||
RenderHeader("Options");
|
||||
|
||||
var options = command.Options
|
||||
.OrderByDescending(o => o.IsRequired)
|
||||
.Concat(command.GetBuiltInOptions())
|
||||
.ToArray();
|
||||
|
||||
foreach (var option in options)
|
||||
{
|
||||
if (option.IsRequired)
|
||||
{
|
||||
RenderWithColor("* ", ConsoleColor.Red);
|
||||
}
|
||||
else
|
||||
{
|
||||
RenderIndent();
|
||||
}
|
||||
|
||||
// Short name
|
||||
if (option.ShortName != null)
|
||||
{
|
||||
RenderWithColor($"-{option.ShortName}", ConsoleColor.White);
|
||||
}
|
||||
|
||||
// Delimiter
|
||||
if (!string.IsNullOrWhiteSpace(option.Name) && option.ShortName != null)
|
||||
{
|
||||
Render("|");
|
||||
}
|
||||
|
||||
// Name
|
||||
if (!string.IsNullOrWhiteSpace(option.Name))
|
||||
{
|
||||
RenderWithColor($"--{option.Name}", ConsoleColor.White);
|
||||
}
|
||||
|
||||
RenderColumnIndent();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(option.Description))
|
||||
{
|
||||
Render(option.Description);
|
||||
Render(" ");
|
||||
}
|
||||
|
||||
// Valid values
|
||||
var validValues = option.GetValidValues();
|
||||
if (validValues.Any())
|
||||
{
|
||||
Render($"Valid values: {string.Join(", ", validValues)}.");
|
||||
Render(" ");
|
||||
}
|
||||
|
||||
// TODO: Render default value here.
|
||||
|
||||
// Environment variable
|
||||
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
|
||||
{
|
||||
Render($"Environment variable: {option.EnvironmentVariableName}");
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderChildCommands()
|
||||
{
|
||||
if (!childCommands.Any())
|
||||
return;
|
||||
|
||||
RenderMargin();
|
||||
RenderHeader("Commands");
|
||||
|
||||
foreach (var childCommand in childCommands)
|
||||
{
|
||||
var relativeCommandName =
|
||||
!string.IsNullOrWhiteSpace(command.Name)
|
||||
? childCommand.Name!.Substring(command.Name.Length + 1)
|
||||
: childCommand.Name!;
|
||||
|
||||
// Name
|
||||
RenderIndent();
|
||||
RenderWithColor(relativeCommandName, ConsoleColor.Cyan);
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(childCommand.Description))
|
||||
{
|
||||
RenderColumnIndent();
|
||||
Render(childCommand.Description);
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
|
||||
RenderMargin();
|
||||
|
||||
// Child command help tip
|
||||
Render("You can run `");
|
||||
Render(_metadata.ExecutableName);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(command.Name))
|
||||
{
|
||||
Render(" ");
|
||||
RenderWithColor(command.Name, ConsoleColor.Cyan);
|
||||
}
|
||||
|
||||
Render(" ");
|
||||
RenderWithColor("[command]", ConsoleColor.Cyan);
|
||||
|
||||
Render(" ");
|
||||
RenderWithColor("--help", ConsoleColor.White);
|
||||
|
||||
Render("` to show help on a specific command.");
|
||||
|
||||
RenderNewLine();
|
||||
}
|
||||
public void Write(
|
||||
RootSchema root,
|
||||
CommandSchema command,
|
||||
IReadOnlyDictionary<CommandArgumentSchema, object?> defaultValues)
|
||||
{
|
||||
var childCommands = root.GetChildCommands(command.Name);
|
||||
|
||||
_console.ResetColor();
|
||||
RenderApplicationInfo();
|
||||
RenderDescription();
|
||||
RenderUsage();
|
||||
RenderParameters();
|
||||
RenderOptions();
|
||||
RenderChildCommands();
|
||||
|
||||
if (command.IsDefault)
|
||||
WriteApplicationInfo();
|
||||
|
||||
WriteCommandDescription(command);
|
||||
WriteCommandUsage(command, childCommands);
|
||||
WriteCommandParameters(command);
|
||||
WriteCommandOptions(command, defaultValues);
|
||||
WriteCommandChildren(command, childCommands);
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class HelpTextWriter
|
||||
{
|
||||
private static string FormatValidValues(IReadOnlyList<string> values) =>
|
||||
values.Select(v => v.Quote()).JoinToString(", ");
|
||||
|
||||
private static string? FormatDefaultValue(object? defaultValue)
|
||||
{
|
||||
if (defaultValue == null)
|
||||
return null;
|
||||
|
||||
// Enumerable
|
||||
if (!(defaultValue is string) && defaultValue is IEnumerable defaultValues)
|
||||
{
|
||||
var elementType = defaultValues.GetType().GetEnumerableUnderlyingType() ?? typeof(object);
|
||||
|
||||
// If the ToString() method is not overriden, the default value can't be formatted nicely
|
||||
if (!elementType.IsToStringOverriden())
|
||||
return null;
|
||||
|
||||
return defaultValues
|
||||
.Cast<object?>()
|
||||
.Where(o => o != null)
|
||||
.Select(o => o!.ToFormattableString(CultureInfo.InvariantCulture).Quote())
|
||||
.JoinToString(" ");
|
||||
}
|
||||
// Non-enumerable
|
||||
else
|
||||
{
|
||||
// If the ToString() method is not overriden, the default value can't be formatted nicely
|
||||
if (!defaultValue.GetType().IsToStringOverriden())
|
||||
return null;
|
||||
|
||||
return defaultValue.ToFormattableString(CultureInfo.InvariantCulture).Quote();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
232
CliFx/Domain/RootSchema.cs
Normal file
232
CliFx/Domain/RootSchema.cs
Normal file
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx.Domain
|
||||
{
|
||||
internal partial class RootSchema
|
||||
{
|
||||
public IReadOnlyList<CommandSchema> Commands { get; }
|
||||
|
||||
public RootSchema(IReadOnlyList<CommandSchema> commands)
|
||||
{
|
||||
Commands = commands;
|
||||
}
|
||||
|
||||
public IReadOnlyList<string> GetCommandNames() => Commands
|
||||
.Select(c => c.Name)
|
||||
.Where(n => !string.IsNullOrWhiteSpace(n))
|
||||
.ToArray()!;
|
||||
|
||||
public CommandSchema? TryFindDefaultCommand() =>
|
||||
Commands.FirstOrDefault(c => c.IsDefault);
|
||||
|
||||
public CommandSchema? TryFindCommand(string? commandName) =>
|
||||
Commands.FirstOrDefault(c => c.MatchesName(commandName));
|
||||
|
||||
private IReadOnlyList<CommandSchema> GetDescendantCommands(
|
||||
IReadOnlyList<CommandSchema> potentialParentCommands,
|
||||
string? parentCommandName) =>
|
||||
potentialParentCommands
|
||||
// Default commands can't be children of anything
|
||||
.Where(c => !string.IsNullOrWhiteSpace(c.Name))
|
||||
// Command can't be its own child
|
||||
.Where(c => !c.MatchesName(parentCommandName))
|
||||
.Where(c =>
|
||||
string.IsNullOrWhiteSpace(parentCommandName) ||
|
||||
c.Name!.StartsWith(parentCommandName + ' ', StringComparison.OrdinalIgnoreCase))
|
||||
.ToArray();
|
||||
|
||||
public IReadOnlyList<CommandSchema> GetDescendantCommands(string? parentCommandName) =>
|
||||
GetDescendantCommands(Commands, parentCommandName);
|
||||
|
||||
public IReadOnlyList<CommandSchema> GetChildCommands(string? parentCommandName)
|
||||
{
|
||||
var descendants = GetDescendantCommands(parentCommandName);
|
||||
|
||||
// Filter out descendants of descendants, leave only children
|
||||
var result = new List<CommandSchema>(descendants);
|
||||
|
||||
foreach (var descendant in descendants)
|
||||
{
|
||||
var descendantDescendants = GetDescendantCommands(descendants, descendant.Name);
|
||||
result.RemoveRange(descendantDescendants);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class RootSchema
|
||||
{
|
||||
private static void ValidateParameters(CommandSchema command)
|
||||
{
|
||||
var duplicateOrderGroup = command.Parameters
|
||||
.GroupBy(a => a.Order)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateOrderGroup != null)
|
||||
{
|
||||
throw CliFxException.ParametersWithSameOrder(
|
||||
command,
|
||||
duplicateOrderGroup.Key,
|
||||
duplicateOrderGroup.ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
var duplicateNameGroup = command.Parameters
|
||||
.Where(a => !string.IsNullOrWhiteSpace(a.Name))
|
||||
.GroupBy(a => a.Name!, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
throw CliFxException.ParametersWithSameName(
|
||||
command,
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
var nonScalarParameters = command.Parameters
|
||||
.Where(p => !p.IsScalar)
|
||||
.ToArray();
|
||||
|
||||
if (nonScalarParameters.Length > 1)
|
||||
{
|
||||
throw CliFxException.TooManyNonScalarParameters(
|
||||
command,
|
||||
nonScalarParameters
|
||||
);
|
||||
}
|
||||
|
||||
var nonLastNonScalarParameter = command.Parameters
|
||||
.OrderByDescending(a => a.Order)
|
||||
.Skip(1)
|
||||
.LastOrDefault(p => !p.IsScalar);
|
||||
|
||||
if (nonLastNonScalarParameter != null)
|
||||
{
|
||||
throw CliFxException.NonLastNonScalarParameter(
|
||||
command,
|
||||
nonLastNonScalarParameter
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateOptions(CommandSchema command)
|
||||
{
|
||||
var noNameGroup = command.Options
|
||||
.Where(o => o.ShortName == null && string.IsNullOrWhiteSpace(o.Name))
|
||||
.ToArray();
|
||||
|
||||
if (noNameGroup.Any())
|
||||
{
|
||||
throw CliFxException.OptionsWithNoName(
|
||||
command,
|
||||
noNameGroup.ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
var invalidLengthNameGroup = command.Options
|
||||
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
||||
.Where(o => o.Name!.Length <= 1)
|
||||
.ToArray();
|
||||
|
||||
if (invalidLengthNameGroup.Any())
|
||||
{
|
||||
throw CliFxException.OptionsWithInvalidLengthName(
|
||||
command,
|
||||
invalidLengthNameGroup
|
||||
);
|
||||
}
|
||||
|
||||
var duplicateNameGroup = command.Options
|
||||
.Where(o => !string.IsNullOrWhiteSpace(o.Name))
|
||||
.GroupBy(o => o.Name!, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
throw CliFxException.OptionsWithSameName(
|
||||
command,
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
var duplicateShortNameGroup = command.Options
|
||||
.Where(o => o.ShortName != null)
|
||||
.GroupBy(o => o.ShortName!.Value)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateShortNameGroup != null)
|
||||
{
|
||||
throw CliFxException.OptionsWithSameShortName(
|
||||
command,
|
||||
duplicateShortNameGroup.Key,
|
||||
duplicateShortNameGroup.ToArray()
|
||||
);
|
||||
}
|
||||
|
||||
var duplicateEnvironmentVariableNameGroup = command.Options
|
||||
.Where(o => !string.IsNullOrWhiteSpace(o.EnvironmentVariableName))
|
||||
.GroupBy(o => o.EnvironmentVariableName!, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateEnvironmentVariableNameGroup != null)
|
||||
{
|
||||
throw CliFxException.OptionsWithSameEnvironmentVariableName(
|
||||
command,
|
||||
duplicateEnvironmentVariableNameGroup.Key,
|
||||
duplicateEnvironmentVariableNameGroup.ToArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ValidateCommands(IReadOnlyList<CommandSchema> commands)
|
||||
{
|
||||
if (!commands.Any())
|
||||
{
|
||||
throw CliFxException.NoCommandsDefined();
|
||||
}
|
||||
|
||||
var duplicateNameGroup = commands
|
||||
.GroupBy(c => c.Name, StringComparer.OrdinalIgnoreCase)
|
||||
.FirstOrDefault(g => g.Count() > 1);
|
||||
|
||||
if (duplicateNameGroup != null)
|
||||
{
|
||||
throw !string.IsNullOrWhiteSpace(duplicateNameGroup.Key)
|
||||
? CliFxException.CommandsWithSameName(
|
||||
duplicateNameGroup.Key,
|
||||
duplicateNameGroup.ToArray()
|
||||
)
|
||||
: CliFxException.TooManyDefaultCommands(duplicateNameGroup.ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public static RootSchema Resolve(IReadOnlyList<Type> commandTypes)
|
||||
{
|
||||
var commands = new List<CommandSchema>();
|
||||
|
||||
foreach (var commandType in commandTypes)
|
||||
{
|
||||
var command =
|
||||
CommandSchema.TryResolve(commandType) ??
|
||||
throw CliFxException.InvalidCommandType(commandType);
|
||||
|
||||
ValidateParameters(command);
|
||||
ValidateOptions(command);
|
||||
|
||||
commands.Add(command);
|
||||
}
|
||||
|
||||
ValidateCommands(commands);
|
||||
|
||||
return new RootSchema(commands);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Domain;
|
||||
using CliFx.Internal.Extensions;
|
||||
|
||||
namespace CliFx.Exceptions
|
||||
{
|
||||
@@ -11,53 +12,25 @@ namespace CliFx.Exceptions
|
||||
/// </summary>
|
||||
public partial class CliFxException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents the default exit code assigned to exceptions in CliFx.
|
||||
/// </summary>
|
||||
protected const int DefaultExitCode = -100;
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show the help text after handling this exception.
|
||||
/// </summary>
|
||||
public bool ShowHelp { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether this exception was constructed with a message.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// We cannot check against the 'Message' property because it will always return
|
||||
/// a default message if it was constructed with a null value or is currently null.
|
||||
/// </remarks>
|
||||
public bool HasMessage { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns an exit code associated with this exception.
|
||||
/// </summary>
|
||||
public int ExitCode { get; }
|
||||
private readonly bool _isMessageSet;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CliFxException"/>.
|
||||
/// </summary>
|
||||
public CliFxException(string? message, bool showHelp = false)
|
||||
: this(message, null, showHelp: showHelp)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CliFxException"/>.
|
||||
/// </summary>
|
||||
public CliFxException(string? message, Exception? innerException, int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
public CliFxException(string? message, Exception? innerException = null)
|
||||
: base(message, innerException)
|
||||
{
|
||||
ExitCode = exitCode != 0
|
||||
? exitCode
|
||||
: throw new ArgumentException("Exit code must not be zero in order to signify failure.");
|
||||
HasMessage = !string.IsNullOrWhiteSpace(message);
|
||||
ShowHelp = showHelp;
|
||||
// Message property has a fallback so it's never empty, hence why we need this check
|
||||
_isMessageSet = !string.IsNullOrWhiteSpace(message);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => _isMessageSet
|
||||
? Message
|
||||
: base.ToString();
|
||||
}
|
||||
|
||||
// Mid-user-facing exceptions
|
||||
// Internal exceptions
|
||||
// Provide more diagnostic information here
|
||||
public partial class CliFxException
|
||||
{
|
||||
@@ -75,13 +48,13 @@ Refer to the readme to learn how to integrate a dependency container of your cho
|
||||
return new CliFxException(message.Trim(), innerException);
|
||||
}
|
||||
|
||||
internal static CliFxException DelegateActivatorReceivedNull(Type type)
|
||||
internal static CliFxException DelegateActivatorReturnedNull(Type type)
|
||||
{
|
||||
var message = $@"
|
||||
Failed to create an instance of type '{type.FullName}', received <null> instead.
|
||||
|
||||
To fix this, ensure that the provided type activator was configured correctly, as it's not expected to return <null>.
|
||||
If you are using a dependency container, ensure this type is registered, because it may return <null> otherwise.";
|
||||
If you are using a dependency container, this error may signify that the type wasn't registered.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
@@ -101,7 +74,7 @@ If you're experiencing problems, please refer to the readme for a quickstart exa
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandsNotRegistered()
|
||||
internal static CliFxException NoCommandsDefined()
|
||||
{
|
||||
var message = $@"
|
||||
There are no commands configured in the application.
|
||||
@@ -112,12 +85,11 @@ If you're experiencing problems, please refer to the readme for a quickstart exa
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandsTooManyDefaults(
|
||||
IReadOnlyList<CommandSchema> invalidCommands)
|
||||
internal static CliFxException TooManyDefaultCommands(IReadOnlyList<CommandSchema> invalidCommands)
|
||||
{
|
||||
var message = $@"
|
||||
Application configuration is invalid because there are {invalidCommands.Count} default commands:
|
||||
{string.Join(Environment.NewLine, invalidCommands.Select(p => p.Type.FullName))}
|
||||
{invalidCommands.JoinToString(Environment.NewLine)}
|
||||
|
||||
There can only be one default command (i.e. command with no name) in an application.
|
||||
Other commands must have unique non-empty names that identify them.";
|
||||
@@ -125,13 +97,13 @@ Other commands must have unique non-empty names that identify them.";
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandsDuplicateName(
|
||||
internal static CliFxException CommandsWithSameName(
|
||||
string name,
|
||||
IReadOnlyList<CommandSchema> invalidCommands)
|
||||
{
|
||||
var message = $@"
|
||||
Application configuration is invalid because there are {invalidCommands.Count} commands with the same name ('{name}'):
|
||||
{string.Join(Environment.NewLine, invalidCommands.Select(p => p.Type.FullName))}
|
||||
{invalidCommands.JoinToString(Environment.NewLine)}
|
||||
|
||||
Commands must have unique names.
|
||||
Names are not case-sensitive.";
|
||||
@@ -139,28 +111,28 @@ Names are not case-sensitive.";
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandParametersDuplicateOrder(
|
||||
internal static CliFxException ParametersWithSameOrder(
|
||||
CommandSchema command,
|
||||
int order,
|
||||
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same order ({order}):
|
||||
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Parameters must have unique order.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandParametersDuplicateName(
|
||||
internal static CliFxException ParametersWithSameName(
|
||||
CommandSchema command,
|
||||
string name,
|
||||
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} parameters with the same name ('{name}'):
|
||||
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Parameters must have unique names to avoid potential confusion in the help text.
|
||||
Names are not case-sensitive.";
|
||||
@@ -168,15 +140,15 @@ Names are not case-sensitive.";
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandParametersTooManyNonScalar(
|
||||
internal static CliFxException TooManyNonScalarParameters(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandParameterSchema> invalidParameters)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidParameters.Count} non-scalar parameters:
|
||||
{string.Join(Environment.NewLine, invalidParameters.Select(p => p.Property.Name))}
|
||||
{invalidParameters.JoinToString(Environment.NewLine)}
|
||||
|
||||
Non-scalar parameter is such that is bound from more than one value (e.g. array or a complex object).
|
||||
Non-scalar parameter is such that is bound from more than one value (e.g. array).
|
||||
Only one parameter in a command may be non-scalar and it must be the last one in order.
|
||||
|
||||
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
|
||||
@@ -184,15 +156,15 @@ If it's not feasible to fit into these constraints, consider using options inste
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandParametersNonLastNonScalar(
|
||||
internal static CliFxException NonLastNonScalarParameter(
|
||||
CommandSchema command,
|
||||
CommandParameterSchema invalidParameter)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains a non-scalar parameter which is not the last in order:
|
||||
{invalidParameter.Property.Name}
|
||||
{invalidParameter}
|
||||
|
||||
Non-scalar parameter is such that is bound from more than one value (e.g. array or a complex object).
|
||||
Non-scalar parameter is such that is bound from more than one value (e.g. array).
|
||||
Only one parameter in a command may be non-scalar and it must be the last one in order.
|
||||
|
||||
If it's not feasible to fit into these constraints, consider using options instead as they don't have these limitations.";
|
||||
@@ -200,26 +172,26 @@ If it's not feasible to fit into these constraints, consider using options inste
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandOptionsNoName(
|
||||
internal static CliFxException OptionsWithNoName(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains one or more options without a name:
|
||||
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options must have either a name or a short name or both.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandOptionsInvalidLengthName(
|
||||
internal static CliFxException OptionsWithInvalidLengthName(
|
||||
CommandSchema command,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains one or more options whose names are too short:
|
||||
{string.Join(Environment.NewLine, invalidOptions.Select(o => $"{o.Property.Name} ('{o.DisplayName}')"))}
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Option names must be at least 2 characters long to avoid confusion with short names.
|
||||
If you intended to set the short name instead, use the attribute overload that accepts a char.";
|
||||
@@ -227,31 +199,29 @@ If you intended to set the short name instead, use the attribute overload that a
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandOptionsDuplicateName(
|
||||
internal static CliFxException OptionsWithSameName(
|
||||
CommandSchema command,
|
||||
string name,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same name ('{name}'):
|
||||
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options must have unique names, because that's what identifies them.
|
||||
Names are not case-sensitive.
|
||||
|
||||
To fix this, ensure that all options have different names.";
|
||||
Options must have unique names.
|
||||
Names are not case-sensitive.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandOptionsDuplicateShortName(
|
||||
internal static CliFxException OptionsWithSameShortName(
|
||||
CommandSchema command,
|
||||
char shortName,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same short name ('{shortName}'):
|
||||
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options must have unique short names.
|
||||
Short names are case-sensitive (i.e. 'a' and 'A' are different short names).";
|
||||
@@ -259,14 +229,14 @@ Short names are case-sensitive (i.e. 'a' and 'A' are different short names).";
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CommandOptionsDuplicateEnvironmentVariableName(
|
||||
internal static CliFxException OptionsWithSameEnvironmentVariableName(
|
||||
CommandSchema command,
|
||||
string environmentVariableName,
|
||||
IReadOnlyList<CommandOptionSchema> invalidOptions)
|
||||
{
|
||||
var message = $@"
|
||||
Command '{command.Type.FullName}' is invalid because it contains {invalidOptions.Count} options with the same fallback environment variable name ('{environmentVariableName}'):
|
||||
{string.Join(Environment.NewLine, invalidOptions.Select(o => o.Property.Name))}
|
||||
{invalidOptions.JoinToString(Environment.NewLine)}
|
||||
|
||||
Options cannot share the same environment variable as a fallback.
|
||||
Environment variable names are not case-sensitive.";
|
||||
@@ -279,98 +249,145 @@ Environment variable names are not case-sensitive.";
|
||||
// Avoid internal details and fix recommendations here
|
||||
public partial class CliFxException
|
||||
{
|
||||
internal static CliFxException CannotFindMatchingCommand(CommandLineInput input)
|
||||
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
|
||||
CommandParameterSchema parameter,
|
||||
IReadOnlyList<string> values)
|
||||
{
|
||||
var message = $@"
|
||||
Can't find a command that matches the following arguments:
|
||||
{string.Join(" ", input.UnboundArguments.Select(a => a.Value))}";
|
||||
Parameter {parameter.GetUserFacingDisplayString()} expects a single value, but provided with multiple:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}";
|
||||
|
||||
return new CliFxException(message.Trim(), showHelp: true);
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
|
||||
CommandOptionSchema option,
|
||||
IReadOnlyList<string> values)
|
||||
{
|
||||
var message = $@"
|
||||
Option {option.GetUserFacingDisplayString()} expects a single value, but provided with multiple:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertMultipleValuesToNonScalar(
|
||||
CommandArgumentSchema argument,
|
||||
IReadOnlyList<string> values)
|
||||
IReadOnlyList<string> values) => argument switch
|
||||
{
|
||||
var argumentDisplayText = argument is CommandParameterSchema
|
||||
? $"Parameter <{argument.DisplayName}>"
|
||||
: $"Option '{argument.DisplayName}'";
|
||||
CommandParameterSchema parameter => CannotConvertMultipleValuesToNonScalar(parameter, values),
|
||||
CommandOptionSchema option => CannotConvertMultipleValuesToNonScalar(option, values),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(argument))
|
||||
};
|
||||
|
||||
internal static CliFxException CannotConvertToType(
|
||||
CommandParameterSchema parameter,
|
||||
string? value,
|
||||
Type type,
|
||||
Exception? innerException = null)
|
||||
{
|
||||
var message = $@"
|
||||
{argumentDisplayText} expects a single value, but provided with multiple:
|
||||
{string.Join(", ", values.Select(v => $"'{v}'"))}";
|
||||
Can't convert value ""{value ?? "<null>"}"" to type '{type.Name}' for parameter {parameter.GetUserFacingDisplayString()}.
|
||||
{innerException?.Message ?? "This type is not supported."}";
|
||||
|
||||
return new CliFxException(message.Trim(), showHelp: true);
|
||||
return new CliFxException(message.Trim(), innerException);
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertToType(
|
||||
CommandOptionSchema option,
|
||||
string? value,
|
||||
Type type,
|
||||
Exception? innerException = null)
|
||||
{
|
||||
var message = $@"
|
||||
Can't convert value ""{value ?? "<null>"}"" to type '{type.Name}' for option {option.GetUserFacingDisplayString()}.
|
||||
{innerException?.Message ?? "This type is not supported."}";
|
||||
|
||||
return new CliFxException(message.Trim(), innerException);
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertToType(
|
||||
CommandArgumentSchema argument,
|
||||
string? value,
|
||||
Type type,
|
||||
Exception? innerException = null)
|
||||
Exception? innerException = null) => argument switch
|
||||
{
|
||||
var argumentDisplayText = argument is CommandParameterSchema
|
||||
? $"parameter <{argument.DisplayName}>"
|
||||
: $"option '{argument.DisplayName}'";
|
||||
CommandParameterSchema parameter => CannotConvertToType(parameter, value, type, innerException),
|
||||
CommandOptionSchema option => CannotConvertToType(option, value, type, innerException),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(argument))
|
||||
};
|
||||
|
||||
internal static CliFxException CannotConvertNonScalar(
|
||||
CommandParameterSchema parameter,
|
||||
IReadOnlyList<string> values,
|
||||
Type type)
|
||||
{
|
||||
var message = $@"
|
||||
Can't convert value '{value ?? "<null>"}' to type '{type.FullName}' for {argumentDisplayText}.
|
||||
{innerException?.Message ?? "This type is not supported."}";
|
||||
Can't convert provided values to type '{type.Name}' for parameter {parameter.GetUserFacingDisplayString()}:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}
|
||||
|
||||
return new CliFxException(message.Trim(), innerException, showHelp: true);
|
||||
Target type is not assignable from array and doesn't have a public constructor that takes an array.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertNonScalar(
|
||||
CommandOptionSchema option,
|
||||
IReadOnlyList<string> values,
|
||||
Type type)
|
||||
{
|
||||
var message = $@"
|
||||
Can't convert provided values to type '{type.Name}' for option {option.GetUserFacingDisplayString()}:
|
||||
{values.Select(v => v.Quote()).JoinToString(" ")}
|
||||
|
||||
Target type is not assignable from array and doesn't have a public constructor that takes an array.";
|
||||
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException CannotConvertNonScalar(
|
||||
CommandArgumentSchema argument,
|
||||
IReadOnlyList<string> values,
|
||||
Type type)
|
||||
Type type) => argument switch
|
||||
{
|
||||
var argumentDisplayText = argument is CommandParameterSchema
|
||||
? $"parameter <{argument.DisplayName}>"
|
||||
: $"option '{argument.DisplayName}'";
|
||||
|
||||
var message = $@"
|
||||
Can't convert provided values to type '{type.FullName}' for {argumentDisplayText}:
|
||||
{string.Join(", ", values.Select(v => $"'{v}'"))}
|
||||
|
||||
Target type is not assignable from array and doesn't have a public constructor that takes an array.";
|
||||
|
||||
return new CliFxException(message.Trim(), showHelp: true);
|
||||
}
|
||||
CommandParameterSchema parameter => CannotConvertNonScalar(parameter, values, type),
|
||||
CommandOptionSchema option => CannotConvertNonScalar(option, values, type),
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(argument))
|
||||
};
|
||||
|
||||
internal static CliFxException ParameterNotSet(CommandParameterSchema parameter)
|
||||
{
|
||||
var message = $@"
|
||||
Missing value for parameter <{parameter.DisplayName}>.";
|
||||
Missing value for parameter {parameter.GetUserFacingDisplayString()}.";
|
||||
|
||||
return new CliFxException(message.Trim(), showHelp: true);
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException RequiredOptionsNotSet(IReadOnlyList<CommandOptionSchema> options)
|
||||
{
|
||||
var message = $@"
|
||||
Missing values for one or more required options:
|
||||
{string.Join(Environment.NewLine, options.Select(o => o.DisplayName))}";
|
||||
{options.Select(o => o.GetUserFacingDisplayString()).JoinToString(Environment.NewLine)}";
|
||||
|
||||
return new CliFxException(message.Trim(), showHelp: true);
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException UnrecognizedParametersProvided(IReadOnlyList<CommandUnboundArgumentInput> inputs)
|
||||
internal static CliFxException UnrecognizedParametersProvided(IReadOnlyList<CommandParameterInput> parameterInputs)
|
||||
{
|
||||
var message = $@"
|
||||
Unrecognized parameters provided:
|
||||
{string.Join(Environment.NewLine, inputs.Select(i => $"<{i.Value}>"))}";
|
||||
{parameterInputs.Select(p => p.Value).JoinToString(Environment.NewLine)}";
|
||||
|
||||
return new CliFxException(message.Trim(), showHelp: true);
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
|
||||
internal static CliFxException UnrecognizedOptionsProvided(IReadOnlyList<CommandOptionInput> inputs)
|
||||
internal static CliFxException UnrecognizedOptionsProvided(IReadOnlyList<CommandOptionInput> optionInputs)
|
||||
{
|
||||
var message = $@"
|
||||
Unrecognized options provided:
|
||||
{string.Join(Environment.NewLine, inputs.Select(i => i.DisplayAlias))}";
|
||||
{optionInputs.Select(o => o.GetRawAlias()).JoinToString(Environment.NewLine)}";
|
||||
|
||||
return new CliFxException(message.Trim(), showHelp: true);
|
||||
return new CliFxException(message.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,35 +4,66 @@ namespace CliFx.Exceptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Thrown when a command cannot proceed with normal execution due to an error.
|
||||
/// Use this exception if you want to report an error that occured during execution of a command.
|
||||
/// Use this exception if you want to report an error that occured during the execution of a command.
|
||||
/// This exception also allows specifying exit code which will be returned to the calling process.
|
||||
/// </summary>
|
||||
public class CommandException : CliFxException
|
||||
public class CommandException : Exception
|
||||
{
|
||||
private const int DefaultExitCode = 1;
|
||||
|
||||
private readonly bool _isMessageSet;
|
||||
|
||||
/// <summary>
|
||||
/// Exit code returned by the application when this exception is handled.
|
||||
/// </summary>
|
||||
public int ExitCode { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether to show the help text after handling this exception.
|
||||
/// </summary>
|
||||
public bool ShowHelp { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CommandException"/>.
|
||||
/// </summary>
|
||||
public CommandException(string? message, Exception? innerException,
|
||||
int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
: base(message, innerException, exitCode, showHelp)
|
||||
/// <remarks>
|
||||
/// On Unix systems an exit code is 8-bit unsigned integer so it's strongly recommended to use values between 1 and 255 to avoid overflow.
|
||||
/// </remarks>
|
||||
public CommandException(string? message, Exception? innerException, int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
: base(message, innerException)
|
||||
{
|
||||
|
||||
ExitCode = exitCode;
|
||||
ShowHelp = showHelp;
|
||||
|
||||
// Message property has a fallback so it's never empty, hence why we need this check
|
||||
_isMessageSet = !string.IsNullOrWhiteSpace(message);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CommandException"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// On Unix systems an exit code is 8-bit unsigned integer so it's strongly recommended to use values between 1 and 255 to avoid overflow.
|
||||
/// </remarks>
|
||||
public CommandException(string? message, int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
: this(message, null, exitCode, showHelp)
|
||||
: this(message, null, exitCode, showHelp)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="CommandException"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// On Unix systems an exit code is 8-bit unsigned integer so it's strongly recommended to use values between 1 and 255 to avoid overflow.
|
||||
/// </remarks>
|
||||
public CommandException(int exitCode = DefaultExitCode, bool showHelp = false)
|
||||
: this(null, exitCode, showHelp)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => _isMessageSet
|
||||
? Message
|
||||
: base.ToString();
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Executes the command using the specified implementation of <see cref="IConsole"/>.
|
||||
/// This is the method that's called when the command is invoked by a user through command line interface.
|
||||
/// This is the method that's called when the command is invoked by a user through command line.
|
||||
/// </summary>
|
||||
/// <remarks>If the execution of the command is not asynchronous, simply end the method with <code>return default;</code></remarks>
|
||||
ValueTask ExecuteAsync(IConsole console);
|
||||
|
||||
@@ -65,10 +65,15 @@ namespace CliFx
|
||||
int CursorTop { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Provides a token that signals when application cancellation is requested.
|
||||
/// Subsequent calls return the same token.
|
||||
/// When working with system console, the user can request cancellation by issuing an interrupt signal (Ctrl+C).
|
||||
/// Defers the application termination in case of a cancellation request and returns the token that represents it.
|
||||
/// Subsequent calls to this method return the same token.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// When working with <see cref="SystemConsole"/>:<br/>
|
||||
/// - Cancellation can be requested by the user by pressing Ctrl+C.<br/>
|
||||
/// - Cancellation can only be deferred once, subsequent requests to cancel by the user will result in instant termination.<br/>
|
||||
/// - Any code executing prior to calling this method is not cancellation-aware and as such will terminate instantly when cancellation is requested.
|
||||
/// </remarks>
|
||||
CancellationToken GetCancellationToken();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Abstraction for a service can initialize objects at runtime.
|
||||
/// Abstraction for a service that can initialize objects at runtime.
|
||||
/// </summary>
|
||||
public interface ITypeActivator
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates an instance of specified type.
|
||||
/// Creates an instance of the specified type.
|
||||
/// </summary>
|
||||
object CreateInstance(Type type);
|
||||
}
|
||||
|
||||
13
CliFx/Internal/Extensions/CollectionExtensions.cs
Normal file
13
CliFx/Internal/Extensions/CollectionExtensions.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CliFx.Internal.Extensions
|
||||
{
|
||||
internal static class CollectionExtensions
|
||||
{
|
||||
public static void RemoveRange<T>(this ICollection<T> source, IEnumerable<T> items)
|
||||
{
|
||||
foreach (var item in items)
|
||||
source.Remove(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
26
CliFx/Internal/Extensions/StringExtensions.cs
Normal file
26
CliFx/Internal/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace CliFx.Internal.Extensions
|
||||
{
|
||||
internal static class StringExtensions
|
||||
{
|
||||
public static string Repeat(this char c, int count) => new string(c, count);
|
||||
|
||||
public static string AsString(this char c) => c.Repeat(1);
|
||||
|
||||
public static string Quote(this string str) => $"\"{str}\"";
|
||||
|
||||
public static string JoinToString<T>(this IEnumerable<T> source, string separator) => string.Join(separator, source);
|
||||
|
||||
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
|
||||
builder.Length > 0 ? builder.Append(value) : builder;
|
||||
|
||||
public static string ToFormattableString(this object obj,
|
||||
IFormatProvider? formatProvider = null, string? format = null) =>
|
||||
obj is IFormattable formattable
|
||||
? formattable.ToString(format, formatProvider)
|
||||
: obj.ToString();
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,9 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace CliFx.Internal
|
||||
namespace CliFx.Internal.Extensions
|
||||
{
|
||||
internal static class TypeExtensions
|
||||
{
|
||||
@@ -22,13 +23,30 @@ namespace CliFx.Internal
|
||||
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
|
||||
return type.GetGenericArguments().FirstOrDefault();
|
||||
|
||||
return type.GetInterfaces()
|
||||
return type
|
||||
.GetInterfaces()
|
||||
.Select(GetEnumerableUnderlyingType)
|
||||
.Where(t => t != null)
|
||||
.OrderByDescending(t => t != typeof(object)) // prioritize more specific types
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
public static MethodInfo GetToStringMethod(this Type type) => type.GetMethod(nameof(ToString), Type.EmptyTypes);
|
||||
|
||||
public static bool IsToStringOverriden(this Type type) => type.GetToStringMethod() != typeof(object).GetToStringMethod();
|
||||
|
||||
public static MethodInfo GetStaticParseMethod(this Type type, bool withFormatProvider = false)
|
||||
{
|
||||
var argumentTypes = withFormatProvider
|
||||
? new[] {typeof(string), typeof(IFormatProvider)}
|
||||
: new[] {typeof(string)};
|
||||
|
||||
return type.GetMethod("Parse",
|
||||
BindingFlags.Public | BindingFlags.Static,
|
||||
null, argumentTypes, null
|
||||
);
|
||||
}
|
||||
|
||||
public static Array ToNonGenericArray<T>(this IEnumerable<T> source, Type elementType)
|
||||
{
|
||||
var sourceAsCollection = source as ICollection ?? source.ToArray();
|
||||
10
CliFx/Internal/Extensions/VersionExtensions.cs
Normal file
10
CliFx/Internal/Extensions/VersionExtensions.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System;
|
||||
|
||||
namespace CliFx.Internal.Extensions
|
||||
{
|
||||
internal static class VersionExtensions
|
||||
{
|
||||
public static string ToSemanticString(this Version version) =>
|
||||
version.Revision <= 0 ? version.ToString(3) : version.ToString();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,23 @@
|
||||
// Polyfills to bridge the missing APIs in older versions of the framework/standard.
|
||||
|
||||
#if NETSTANDARD2_0
|
||||
namespace System
|
||||
{
|
||||
using Linq;
|
||||
|
||||
internal static class Extensions
|
||||
{
|
||||
public static bool Contains(this string str, char c) =>
|
||||
str.Any(i => i == c);
|
||||
|
||||
public static bool StartsWith(this string str, char c) =>
|
||||
str.Length > 0 && str[0] == c;
|
||||
|
||||
public static bool EndsWith(this string str, char c) =>
|
||||
str.Length > 0 && str[str.Length - 1] == c;
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Collections.Generic
|
||||
{
|
||||
internal static class Extensions
|
||||
@@ -17,4 +34,15 @@ namespace System.Collections.Generic
|
||||
dic.TryGetValue(key, out var result) ? result! : default!;
|
||||
}
|
||||
}
|
||||
|
||||
namespace System.Linq
|
||||
{
|
||||
using Collections.Generic;
|
||||
|
||||
internal static class Extensions
|
||||
{
|
||||
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source, IEqualityComparer<T> comparer) =>
|
||||
new HashSet<T>(source, comparer);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
13
CliFx/Internal/ProcessEx.cs
Normal file
13
CliFx/Internal/ProcessEx.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace CliFx.Internal
|
||||
{
|
||||
internal static class ProcessEx
|
||||
{
|
||||
public static int GetCurrentProcessId()
|
||||
{
|
||||
using var process = Process.GetCurrentProcess();
|
||||
return process.Id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System.Text;
|
||||
|
||||
namespace CliFx.Internal
|
||||
{
|
||||
internal static class StringExtensions
|
||||
{
|
||||
public static string Repeat(this char c, int count) => new string(c, count);
|
||||
|
||||
public static string AsString(this char c) => c.Repeat(1);
|
||||
|
||||
public static StringBuilder AppendIfNotEmpty(this StringBuilder builder, char value) =>
|
||||
builder.Length > 0 ? builder.Append(value) : builder;
|
||||
}
|
||||
}
|
||||
@@ -96,12 +96,12 @@ namespace CliFx
|
||||
{
|
||||
private static StreamReader WrapInput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamReader(stream, Console.InputEncoding, false)
|
||||
? new StreamReader(Stream.Synchronized(stream), Console.InputEncoding, false)
|
||||
: StreamReader.Null;
|
||||
|
||||
private static StreamWriter WrapOutput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamWriter(stream, Console.OutputEncoding) {AutoFlush = true}
|
||||
? new StreamWriter(Stream.Synchronized(stream), Console.OutputEncoding) {AutoFlush = true}
|
||||
: StreamWriter.Null;
|
||||
}
|
||||
}
|
||||
43
CliFx/Utilities/MemoryStreamWriter.cs
Normal file
43
CliFx/Utilities/MemoryStreamWriter.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace CliFx.Utilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="StreamWriter"/> with a <see cref="MemoryStream"/> as a backing store.
|
||||
/// </summary>
|
||||
public class MemoryStreamWriter : StreamWriter
|
||||
{
|
||||
private new MemoryStream BaseStream => (MemoryStream) base.BaseStream;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="MemoryStreamWriter"/>.
|
||||
/// </summary>
|
||||
public MemoryStreamWriter(Encoding encoding)
|
||||
: base(new MemoryStream(), encoding)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes an instance of <see cref="MemoryStreamWriter"/>.
|
||||
/// </summary>
|
||||
public MemoryStreamWriter()
|
||||
: base(new MemoryStream())
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bytes written to the underlying stream.
|
||||
/// </summary>
|
||||
public byte[] GetBytes()
|
||||
{
|
||||
Flush();
|
||||
return BaseStream.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string written to the underlying stream.
|
||||
/// </summary>
|
||||
public string GetString() => Encoding.GetString(GetBytes());
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,12 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using CliFx.Utilities;
|
||||
|
||||
namespace CliFx
|
||||
{
|
||||
/// <summary>
|
||||
/// Implementation of <see cref="IConsole"/> that routes data to specified streams.
|
||||
/// Implementation of <see cref="IConsole"/> that routes all data to preconfigured streams.
|
||||
/// Does not leak to system console in any way.
|
||||
/// Use this class as a substitute for system console when running tests.
|
||||
/// </summary>
|
||||
@@ -94,12 +95,32 @@ namespace CliFx
|
||||
{
|
||||
private static StreamReader WrapInput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamReader(stream, Console.InputEncoding, false)
|
||||
? new StreamReader(Stream.Synchronized(stream), Console.InputEncoding, false)
|
||||
: StreamReader.Null;
|
||||
|
||||
private static StreamWriter WrapOutput(Stream? stream) =>
|
||||
stream != null
|
||||
? new StreamWriter(stream, Console.OutputEncoding) {AutoFlush = true}
|
||||
? new StreamWriter(Stream.Synchronized(stream), Console.OutputEncoding) {AutoFlush = true}
|
||||
: StreamWriter.Null;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="VirtualConsole"/> that uses in-memory output and error streams.
|
||||
/// Use the exposed streams to easily get the current output.
|
||||
/// </summary>
|
||||
public static (VirtualConsole console, MemoryStreamWriter output, MemoryStreamWriter error) CreateBuffered(
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
// Memory streams don't need to be disposed
|
||||
var output = new MemoryStreamWriter(Console.OutputEncoding);
|
||||
var error = new MemoryStreamWriter(Console.OutputEncoding);
|
||||
|
||||
var console = new VirtualConsole(
|
||||
output: output,
|
||||
error: error,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
|
||||
return (console, output, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
114
Readme.md
114
Readme.md
@@ -27,7 +27,7 @@ An important property of CliFx, when compared to some other libraries, is that i
|
||||
- Provides comprehensive and colorful auto-generated help text
|
||||
- Highly testable and easy to debug
|
||||
- Comes with built-in analyzers to help catch common mistakes
|
||||
- Targets .NET Framework 4.5+ and .NET Standard 2.0+
|
||||
- Targets .NET Standard 2.0+
|
||||
- No external dependencies
|
||||
|
||||
## Screenshots
|
||||
@@ -51,6 +51,8 @@ An important property of CliFx, when compared to some other libraries, is that i
|
||||
|
||||
### Quick start
|
||||
|
||||

|
||||
|
||||
To turn your application into a command line interface you need to change your program's `Main` method so that it delegates execution to `CliApplication`.
|
||||
|
||||
The following code will create and run a default `CliApplication` that will resolve commands defined in the calling assembly. Using fluent interface provided by `CliApplicationBuilder` you can easily configure different aspects of your application.
|
||||
@@ -166,7 +168,7 @@ Parameters
|
||||
* value Value whose logarithm is to be found.
|
||||
|
||||
Options
|
||||
-b|--base Logarithm base.
|
||||
-b|--base Logarithm base. Default: "10".
|
||||
-h|--help Shows help text.
|
||||
--version Shows version information.
|
||||
```
|
||||
@@ -213,9 +215,9 @@ As a general guideline, prefer to use parameters for required inputs that the co
|
||||
|
||||
### Argument syntax
|
||||
|
||||
This library supports an argument syntax which is based on the POSIX standard. To be fair, nobody really knows what the standard is about and very few tools actually follow it as they're supposed to, so for the purpose of having dashes and spaces, CliFx is using the "standard command line syntax".
|
||||
This library supports an argument syntax which is based on the POSIX standard. To be fair, nobody really knows what the standard is about and very few tools actually follow it to the letter, so for the purpose of having dashes and spaces, CliFx is using the "standard command line syntax".
|
||||
|
||||
In more detail, the following examples are all valid:
|
||||
More specifically, the following examples are all valid:
|
||||
|
||||
- `myapp --foo bar` sets option `"foo"` to value `"bar"`
|
||||
- `myapp -f bar` sets option `'f'` to value `"bar"`
|
||||
@@ -225,32 +227,36 @@ In more detail, the following examples are all valid:
|
||||
- `myapp -xqf bar` sets options `'x'` and `'q'` without value, and option `'f'` to value `"bar"`
|
||||
- `myapp -i file1.txt file2.txt` sets option `'i'` to a sequence of values `"file1.txt"` and `"file2.txt"`
|
||||
- `myapp -i file1.txt -i file2.txt` sets option `'i'` to a sequence of values `"file1.txt"` and `"file2.txt"`
|
||||
- `myapp jar new -o cookie` sets option `'o'` to value `"cookie"` and retains two unbound arguments `"jar"` and `"new"`
|
||||
- `myapp cmd abc -o` routes to command `cmd` (assuming it exists) with parameter `abc` and sets option `'o'` without value
|
||||
|
||||
Note that CliFx purposely employs a context-free parser when consuming command line arguments. That means that every input is parsed the same way.
|
||||
Argument parsing in CliFx aims to be as deterministic as possible, ideally yielding the same result no matter the context. The only context-sensitive part in the parser is the command name resolution which needs to know what commands are available in order to discern between arguments that correspond to the command name and arguments which are parameters.
|
||||
|
||||
This also means that `myapp -i file1.txt file2.txt` will _always_ be parsed as an option with multiple values, even if the underlying bound property is not enumerable. For the same reason, unseparated arguments such as `myapp -ofile` will be treated as five distinct options `'o'`, `'f'`, `'i'`, `'l'`, `'e'`, instead of `'o'` being set to `"file"`.
|
||||
Options are always parsed the same way, disregarding the arity of the actual property it binds to. This means that `myapp -i file1.txt file2.txt` will _always_ be parsed as an option with multiple values, even if the underlying bound property is not enumerable. For the same reason, unseparated arguments such as `myapp -ofile` will be treated as five distinct options `'o'`, `'f'`, `'i'`, `'l'`, `'e'`, instead of `'o'` being set to `"file"`.
|
||||
|
||||
When it comes to command name and parameters, they must appear in a strict order, before any options. The parser can't distinguish between arguments that make up a part of the command name and arguments that belong to command parameters, which is why the non-option arguments are bound at a later stage. It is done by trying to find a command that matches the longest sequence of arguments starting from the first, binding any remaining arguments to positional parameters.
|
||||
Because of these rules, order of arguments is semantically important and it always goes like this:
|
||||
|
||||
The above design may seem like a deficiency, but it actually provides value in the fact that it's deterministic -- given a set of command line arguments, the semantics behind them always remain the same. This leads to a more consistent experience for both you as a developer, as well as for the users of your application.
|
||||
```ini
|
||||
{directives} {command name} {parameters} {options}
|
||||
```
|
||||
|
||||
The above design makes the usage of your applications a lot more intuitive and predictable, providing a better end-user experience.
|
||||
|
||||
### Value conversion
|
||||
|
||||
Parameters and options can have different underlying types:
|
||||
|
||||
- Standard types
|
||||
- Primitive types (`int`, `bool`, `double`, `ulong`, `char`, etc)
|
||||
- Primitive types (`int`, `bool`, `double`, `ulong`, `char`, etc.)
|
||||
- Date and time types (`DateTime`, `DateTimeOffset`, `TimeSpan`)
|
||||
- Enum types
|
||||
- Enum types (converted from either name or value)
|
||||
- String-initializable types
|
||||
- Types with a constructor that accepts a single `string` parameter (`FileInfo`, `DirectoryInfo`, etc)
|
||||
- Types with a constructor that accepts a single `string` parameter (`FileInfo`, `DirectoryInfo`, etc.)
|
||||
- Types with a static method `Parse` that accepts a single `string` parameter (and optionally `IFormatProvider`)
|
||||
- Nullable versions of all above types (`decimal?`, `TimeSpan?`, etc)
|
||||
- Nullable versions of all above types (`decimal?`, `TimeSpan?`, etc.)
|
||||
- Collections of all above types
|
||||
- Array types (`T[]`)
|
||||
- Types that are assignable from arrays (`IReadOnlyList<T>`, `ICollection<T>`, etc)
|
||||
- Types with a constructor that accepts a single `T[]` parameter (`HashSet<T>`, `List<T>`, etc)
|
||||
- Types that are assignable from arrays (`IReadOnlyList<T>`, `ICollection<T>`, etc.)
|
||||
- Types with a constructor that accepts a single `T[]` parameter (`HashSet<T>`, `List<T>`, etc.)
|
||||
|
||||
When defining a parameter of an enumerable type, keep in mind that it has to be the only such parameter and it must be the last in order. Options, on the other hand, don't have this limitation.
|
||||
|
||||
@@ -390,10 +396,12 @@ You can run `myapp.exe cmd1 [command] --help` to show help on a specific command
|
||||
|
||||
You may have noticed that commands in CliFx don't return exit codes. This is by design as exit codes are considered a higher-level concern and thus handled by `CliApplication`, not by individual commands.
|
||||
|
||||
Commands can report execution failure simply by throwing exceptions just like any other C# code. When an exception is thrown, `CliApplication` will catch it, print the error, and return an appropriate exit code to the calling process.
|
||||
Commands can report execution failure simply by throwing exceptions just like any other C# code. When an exception is thrown, `CliApplication` will catch it, print the error, and return `1` as the exit code to the calling process.
|
||||
|
||||
If you want to communicate a specific error through exit code, you can instead throw an instance of `CommandException` which takes an exit code as a parameter. When a command throws an exception of type `CommandException`, it is assumed that this was a result of a handled error and, as such, only the exception message will be printed to the error stream. If a command throws an exception of any other type, the full stack trace will be printed as well.
|
||||
|
||||
> Note: Unix systems rely on 8-bit unsigned integers for exit codes, so it's strongly recommended to use values between `1` and `255` to avoid potential overflow issues.
|
||||
|
||||
```c#
|
||||
[Command]
|
||||
public class DivideCommand : ICommand
|
||||
@@ -408,8 +416,8 @@ public class DivideCommand : ICommand
|
||||
{
|
||||
if (Math.Abs(Divisor) < double.Epsilon)
|
||||
{
|
||||
// This will print the error and set exit code to 1337
|
||||
throw new CommandException("Division by zero is not supported.", 1337);
|
||||
// This will print the error and set exit code to 133
|
||||
throw new CommandException("Division by zero is not supported.", 133);
|
||||
}
|
||||
|
||||
var result = Dividend / Divisor;
|
||||
@@ -428,10 +436,10 @@ Division by zero is not supported.
|
||||
|
||||
> $LastExitCode
|
||||
|
||||
1337
|
||||
133
|
||||
```
|
||||
|
||||
You can also specify the `showHelp` parameter to instruct whether to show the help text after printing the error:
|
||||
You can also specify the `showHelp` parameter to instruct whether to show the help text for the current command after printing the error:
|
||||
|
||||
```c#
|
||||
[Command]
|
||||
@@ -446,7 +454,7 @@ public class ExampleCommand : ICommand
|
||||
|
||||
### Graceful cancellation
|
||||
|
||||
It is possible to gracefully cancel execution of a command and preform any necessary cleanup. By default an app gets forcefully killed when it receives an interrupt signal (Ctrl+C or Ctrl+Break), but you can easily override this behavior.
|
||||
It is possible to gracefully cancel execution of a command and preform any necessary cleanup. By default an app gets forcefully killed when it receives an interrupt signal (Ctrl+C or Ctrl+Break), but you can override this behavior.
|
||||
|
||||
In order to make a command cancellation-aware, you need to call `console.GetCancellationToken()`. This method returns a `CancellationToken` that will trigger when the user issues an interrupt signal. Note that any code that comes before the first call to `GetCancellationToken()` will not be cancellation-aware and as such will terminate instantly. Any subsequent calls to this method will return the same token.
|
||||
|
||||
@@ -502,11 +510,29 @@ public static class Program
|
||||
|
||||
### Testing
|
||||
|
||||
CliFx provides an easy way to write functional tests for your commands thanks to the `IConsole` interface.
|
||||
CliFx provides a convenient way to write functional tests for your applications, thanks to the `IConsole` interface.
|
||||
|
||||
You can use `VirtualConsole` to replace the application's stdin, stdout and stderr with your own streams. It has multiple constructor overloads allowing you to specify the exact set of streams that you want. Streams that are not provided are replaced with stubs, i.e. `VirtualConsole` doesn't leak to `System.Console` in any way.
|
||||
Instead of interacting with the real console, you can use an instance of `VirtualConsole` to replace the application's stdin, stdout and stderr with your own streams. Using optional parameters you can also choose to substitute only some of the streams, in which case the remaining streams are replaced with no-op stubs:
|
||||
|
||||
Let's assume you want to test a simple command such as this one.
|
||||
```c#
|
||||
var console = new VirtualConsole(
|
||||
input: stdIn,
|
||||
output: stdOut,
|
||||
error: stdErr
|
||||
);
|
||||
```
|
||||
|
||||
Although `VirtualConsole` can be constructed with all kinds of streams, most of the time you will want to test against in-memory stores. To simplify setup in such scenarios, CliFx also provides a `CreateBuffered` factory method that returns an instance of `IConsole` along with in-memory streams that you can later read from:
|
||||
|
||||
```c#
|
||||
var (console, stdOut, stdErr) = VirtualConsole.CreateBuffered();
|
||||
|
||||
// ...
|
||||
|
||||
var stdOutData = stdOut.GetString();
|
||||
```
|
||||
|
||||
To illustrate how to use all this, let's look at an example. Assume you want to test a simple command such as this one:
|
||||
|
||||
```c#
|
||||
[Command]
|
||||
@@ -529,15 +555,15 @@ public class ConcatCommand : ICommand
|
||||
}
|
||||
```
|
||||
|
||||
By substituting `IConsole` you can write your test cases like this:
|
||||
By substituting `IConsole` you can write your test cases like so:
|
||||
|
||||
```c#
|
||||
// Integration test at command level
|
||||
[Test]
|
||||
public async Task ConcatCommand_Test()
|
||||
public async Task ConcatCommand_executes_successfully()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var command = new ConcatCommand
|
||||
{
|
||||
@@ -547,25 +573,24 @@ public async Task ConcatCommand_Test()
|
||||
|
||||
// Act
|
||||
await command.ExecuteAsync(console);
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||
|
||||
// Assert
|
||||
Assert.That(stdOutData, Is.EqualTo("foo bar"));
|
||||
Assert.That(stdOut.GetString(), Is.EqualTo("foo bar"));
|
||||
}
|
||||
```
|
||||
|
||||
And if you want, you can also test the whole application in a similar fashion:
|
||||
Similarly, you can also test the command end-to-end like so:
|
||||
|
||||
```c#
|
||||
// End-to-end test at application level
|
||||
[Test]
|
||||
public async Task ConcatCommand_Test()
|
||||
public async Task ConcatCommand_executes_successfully()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
var (console, stdOut, _) = VirtualConsole.CreateBuffered();
|
||||
|
||||
var app = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(ConcatCommand))
|
||||
.AddCommand<ConcatCommand>()
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
@@ -574,14 +599,25 @@ public async Task ConcatCommand_Test()
|
||||
|
||||
// Act
|
||||
await app.RunAsync(args, envVars);
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray());
|
||||
|
||||
// Assert
|
||||
Assert.That(stdOutData, Is.EqualTo("foo bar"));
|
||||
Assert.That(stdOut.GetString(), Is.EqualTo("foo bar"));
|
||||
}
|
||||
```
|
||||
|
||||
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.
|
||||
As a general recommendation, it's nearly always more preferable to test at the application level. While you can validate your command's execution adequately simply by testing its `ExecuteAsync` method, testing end-to-end helps you to also catch bugs related to configuration, such as incorrect option names, parameter order, environment variable names, etc.
|
||||
|
||||
Additionally, it's important to remember that commands in CliFx are not constrained to text and can also produce binary data. In such cases, you can still use the above setup but use `GetBytes` instead of `GetString`:
|
||||
|
||||
```c#
|
||||
// Act
|
||||
await app.RunAsync(args, envVars);
|
||||
|
||||
// Assert
|
||||
Assert.That(stdOut.GetBytes(), Is.EqualTo(new byte[] {1, 2, 3, 4, 5}));
|
||||
```
|
||||
|
||||
In some scenarios the binary data may be too large to load in-memory. In situations like this, it's recommended to use `VirtualConsole` directly with custom streams.
|
||||
|
||||
### Debug and preview mode
|
||||
|
||||
@@ -631,7 +667,7 @@ for (var i = 0.0; i <= 1; i += 0.01)
|
||||
|
||||
### Environment variables
|
||||
|
||||
An option can be configured to use the value of a specific environment variable as a fallback.
|
||||
An option can be configured to use the value of an environment variable as a fallback. If an option was not specified by the user, the value will be extracted from that environment variable instead. This also works on options which are marked as required.
|
||||
|
||||
```c#
|
||||
[Command]
|
||||
|
||||
Reference in New Issue
Block a user