mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5e97ebe7f0 | ||
|
|
64cbdaaeab | ||
|
|
ae1f03914c | ||
|
|
ff25dccf8a | ||
|
|
6e0d881682 | ||
|
|
89fd42888a | ||
|
|
eeac82a6e7 | ||
|
|
c641c6fbe2 |
10
Changelog.md
10
Changelog.md
@@ -1,3 +1,13 @@
|
||||
### v2.3 (12-Jul-2022)
|
||||
|
||||
- Added console dimension properties `WindowWidth` and `WindowHeight` to `IConsole` interface and implementing classes.
|
||||
- Improved inline documentation for members of `IConsole` interface.
|
||||
|
||||
### v2.2.6 (14-Jun-2022)
|
||||
|
||||
- Added an overload of `CliApplicationBuilder.UseTypeActivator(...)` that accepts an instance of `IServiceProvider`. This slightly simplifies integration with many DI containers.
|
||||
- Fixed minor grammar mistakes in user-facing error messages.
|
||||
|
||||
### v2.2.5 (10-May-2022)
|
||||
|
||||
- Updated default value resolution for the application executable name. It will now resolve to `myapp.exe` instead of `dotnet myapp.dll` when the application is launched through the EXE apphost on Windows. On other platforms, or when running the application through the .NET CLI, the behavior will be the same as before.
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Basic.Reference.Assemblies" Version="1.2.4" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.4.1" PrivateAssets="all" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.6.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="2.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.7.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2" PrivateAssets="all" />
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<PackageReference Include="BenchmarkDotNet" Version="0.13.1" />
|
||||
<PackageReference Include="clipr" Version="1.6.1" />
|
||||
<PackageReference Include="Cocona" Version="2.0.3" />
|
||||
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||
<PackageReference Include="CommandLineParser" Version="2.9.1" />
|
||||
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.1" />
|
||||
<PackageReference Include="PowerArgs" Version="3.6.0" />
|
||||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20574.7" />
|
||||
|
||||
@@ -11,10 +11,11 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Basic.Reference.Assemblies" Version="1.2.4" />
|
||||
<PackageReference Include="CliWrap" Version="3.4.4" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.6.0" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="1.4.1" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.7.0" />
|
||||
<PackageReference Include="GitHubActionsTestLogger" Version="2.0.0" PrivateAssets="all" />
|
||||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5" PrivateAssets="all" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.2" PrivateAssets="all" />
|
||||
|
||||
@@ -57,6 +57,8 @@ public class Command : ICommand
|
||||
console.ResetColor();
|
||||
console.ForegroundColor = ConsoleColor.DarkMagenta;
|
||||
console.BackgroundColor = ConsoleColor.DarkMagenta;
|
||||
console.WindowWidth = 100;
|
||||
console.WindowHeight = 25;
|
||||
console.CursorLeft = 42;
|
||||
console.CursorTop = 24;
|
||||
|
||||
@@ -90,6 +92,8 @@ public class Command : ICommand
|
||||
Console.BackgroundColor.Should().NotBe(ConsoleColor.DarkMagenta);
|
||||
|
||||
// This fails because tests don't spawn a console window
|
||||
//Console.WindowWidth.Should().Be(100);
|
||||
//Console.WindowHeight.Should().Be(25);
|
||||
//Console.CursorLeft.Should().NotBe(42);
|
||||
//Console.CursorTop.Should().NotBe(24);
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Threading.Tasks;
|
||||
using CliFx.Infrastructure;
|
||||
using CliFx.Tests.Utils;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
@@ -87,7 +88,7 @@ public class Command : ICommand
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Delegate_type_activator_can_initialize_a_type_using_a_custom_function()
|
||||
public async Task Custom_type_activator_can_initialize_a_type_using_a_given_function()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
@@ -110,7 +111,7 @@ public class Command : ICommand
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(commandType)
|
||||
.UseConsole(FakeConsole)
|
||||
.UseTypeActivator(type => Activator.CreateInstance(type, "hello world")!)
|
||||
.UseTypeActivator(type => Activator.CreateInstance(type, "Hello world")!)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
@@ -123,11 +124,55 @@ public class Command : ICommand
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.Trim().Should().Be("hello world");
|
||||
stdOut.Trim().Should().Be("Hello world");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Delegate_type_activator_fails_if_the_underlying_function_returns_null()
|
||||
public async Task Custom_type_activator_can_initialize_a_type_using_a_service_provider()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
// language=cs
|
||||
@"
|
||||
[Command]
|
||||
public class Command : ICommand
|
||||
{
|
||||
private readonly string _foo;
|
||||
|
||||
public Command(string foo) => _foo = foo;
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console)
|
||||
{
|
||||
console.Output.WriteLine(_foo);
|
||||
return default;
|
||||
}
|
||||
}");
|
||||
|
||||
var serviceProvider = new ServiceCollection()
|
||||
.AddSingleton(commandType, Activator.CreateInstance(commandType, "Hello world")!)
|
||||
.BuildServiceProvider();
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(commandType)
|
||||
.UseConsole(FakeConsole)
|
||||
.UseTypeActivator(serviceProvider)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
var exitCode = await application.RunAsync(
|
||||
Array.Empty<string>(),
|
||||
new Dictionary<string, string>()
|
||||
);
|
||||
|
||||
var stdOut = FakeConsole.ReadOutputString();
|
||||
|
||||
// Assert
|
||||
exitCode.Should().Be(0);
|
||||
stdOut.Trim().Should().Be("Hello world");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Custom_type_activator_fails_if_the_underlying_function_returns_null()
|
||||
{
|
||||
// Arrange
|
||||
var commandType = DynamicCommandBuilder.Compile(
|
||||
|
||||
@@ -182,6 +182,12 @@ public partial class CliApplicationBuilder
|
||||
public CliApplicationBuilder UseTypeActivator(Func<Type, object> typeActivator) =>
|
||||
UseTypeActivator(new DelegateTypeActivator(typeActivator));
|
||||
|
||||
/// <summary>
|
||||
/// Configures the application to use the specified service provider for activating types.
|
||||
/// </summary>
|
||||
public CliApplicationBuilder UseTypeActivator(IServiceProvider serviceProvider) =>
|
||||
UseTypeActivator(serviceProvider.GetService);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a configured instance of <see cref="CliApplication"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -169,7 +169,7 @@ internal class CommandBinder
|
||||
: ex.Message;
|
||||
|
||||
throw CliFxException.UserError(
|
||||
$"{memberSchema.GetKind()} {memberSchema.GetFormattedIdentifier()} cannot be set from provided argument(s):" +
|
||||
$"{memberSchema.GetKind()} {memberSchema.GetFormattedIdentifier()} cannot be set from the provided argument(s):" +
|
||||
Environment.NewLine +
|
||||
rawValues.Select(v => '<' + v + '>').JoinToString(" ") +
|
||||
Environment.NewLine +
|
||||
|
||||
@@ -42,11 +42,10 @@ public class FakeConsole : IConsole, IDisposable
|
||||
public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.Black;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ResetColor()
|
||||
{
|
||||
ForegroundColor = ConsoleColor.Gray;
|
||||
BackgroundColor = ConsoleColor.Black;
|
||||
}
|
||||
public int WindowWidth { get; set; } = 232; // Windows defaults
|
||||
|
||||
/// <inheritdoc />
|
||||
public int WindowHeight { get; set; } = 14; // Windows defaults
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CursorLeft { get; set; }
|
||||
@@ -78,6 +77,13 @@ public class FakeConsole : IConsole, IDisposable
|
||||
/// </summary>
|
||||
public void EnqueueKey(ConsoleKeyInfo key) => _keys.Enqueue(key);
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ResetColor()
|
||||
{
|
||||
ForegroundColor = ConsoleColor.Gray;
|
||||
BackgroundColor = ConsoleColor.Black;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Clear()
|
||||
{
|
||||
|
||||
@@ -15,7 +15,7 @@ public interface IConsole
|
||||
ConsoleReader Input { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the input stream is redirected.
|
||||
/// Gets a value that indicates whether input has been redirected from the standard input stream.
|
||||
/// </summary>
|
||||
bool IsInputRedirected { get; }
|
||||
|
||||
@@ -25,7 +25,7 @@ public interface IConsole
|
||||
ConsoleWriter Output { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the output stream is redirected.
|
||||
/// Gets a value that indicates whether output has been redirected from the standard output stream.
|
||||
/// </summary>
|
||||
bool IsOutputRedirected { get; }
|
||||
|
||||
@@ -35,32 +35,37 @@ public interface IConsole
|
||||
ConsoleWriter Error { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether the error stream is redirected.
|
||||
/// Gets a value that indicates whether error output has been redirected from the standard error stream.
|
||||
/// </summary>
|
||||
bool IsErrorRedirected { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Current foreground color.
|
||||
/// Gets or sets the foreground color of the console
|
||||
/// </summary>
|
||||
ConsoleColor ForegroundColor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Current background color.
|
||||
/// Gets or sets the background color of the console.
|
||||
/// </summary>
|
||||
ConsoleColor BackgroundColor { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Resets foreground and background colors to their default values.
|
||||
/// Gets or sets the width of the console window.
|
||||
/// </summary>
|
||||
void ResetColor();
|
||||
int WindowWidth { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cursor left offset.
|
||||
/// Gets or sets the height of the console window.
|
||||
/// </summary>
|
||||
int WindowHeight { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the column position of the cursor within the buffer area.
|
||||
/// </summary>
|
||||
int CursorLeft { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Cursor top offset.
|
||||
/// Gets or sets the row position of the cursor within the buffer area.
|
||||
/// </summary>
|
||||
int CursorTop { get; set; }
|
||||
|
||||
@@ -69,6 +74,11 @@ public interface IConsole
|
||||
/// </summary>
|
||||
ConsoleKeyInfo ReadKey(bool intercept = false);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the foreground and background console colors to their defaults.
|
||||
/// </summary>
|
||||
void ResetColor();
|
||||
|
||||
/// <summary>
|
||||
/// Clears the console buffer and corresponding console window of display information.
|
||||
/// </summary>
|
||||
|
||||
@@ -42,6 +42,20 @@ public class SystemConsole : IConsole, IDisposable
|
||||
set => Console.BackgroundColor = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int WindowWidth
|
||||
{
|
||||
get => Console.WindowWidth;
|
||||
set => Console.WindowWidth = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int WindowHeight
|
||||
{
|
||||
get => Console.WindowHeight;
|
||||
set => Console.WindowHeight = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public int CursorLeft
|
||||
{
|
||||
@@ -67,10 +81,10 @@ public class SystemConsole : IConsole, IDisposable
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void ResetColor() => Console.ResetColor();
|
||||
public ConsoleKeyInfo ReadKey(bool intercept = false) => Console.ReadKey(intercept);
|
||||
|
||||
/// <inheritdoc />
|
||||
public ConsoleKeyInfo ReadKey(bool intercept = false) => Console.ReadKey(intercept);
|
||||
public void ResetColor() => Console.ResetColor();
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Clear() => Console.Clear();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<Project>
|
||||
|
||||
<PropertyGroup>
|
||||
<Version>2.2.5</Version>
|
||||
<Version>2.3</Version>
|
||||
<Company>Tyrrrz</Company>
|
||||
<Copyright>Copyright (C) Oleksii Holub</Copyright>
|
||||
<LangVersion>latest</LangVersion>
|
||||
|
||||
22
Readme.md
22
Readme.md
@@ -14,7 +14,7 @@
|
||||
**CliFx** is a simple to use, yet powerful framework for building command line applications.
|
||||
Its primary goal is to completely take over the user input layer, allowing you to forget about infrastructural concerns and instead focus on writing your application.
|
||||
|
||||
## Terms of use
|
||||
## Terms of use<sup>[[?]](https://github.com/Tyrrrz/.github/blob/master/docs/why-so-political.md)</sup>
|
||||
|
||||
By using this project or its source code, for any purpose and in any shape or form, you grant your **implicit agreement** to all the following statements:
|
||||
|
||||
@@ -65,10 +65,12 @@ public static class Program
|
||||
}
|
||||
```
|
||||
|
||||
> ⚠️ Ensure that your `Main()` method returns the integer exit code provided by `CliApplication.RunAsync()`, as shown in the above example.
|
||||
> **Warning**:
|
||||
> Ensure that your `Main()` method returns the integer exit code provided by `CliApplication.RunAsync()`, as shown in the above example.
|
||||
> Exit code is used to communicate execution result to the parent process, so it's important that your program propagates it.
|
||||
|
||||
> 💡 When calling `CliApplication.RunAsync()`, **CliFx** resolves command line arguments and environment variables from `Environment.GetCommandLineArgs()` and `Environment.GetEnvironmentVariables()` respectively.
|
||||
> **Note**:
|
||||
> When calling `CliApplication.RunAsync()`, **CliFx** resolves command line arguments and environment variables from `Environment.GetCommandLineArgs()` and `Environment.GetEnvironmentVariables()` respectively.
|
||||
> You can also provide them explicitly if you choose.
|
||||
|
||||
The code above uses `AddCommandsFromThisAssembly()` to detect command types defined within the current assembly.
|
||||
@@ -159,7 +161,8 @@ public class LogCommand : ICommand
|
||||
}
|
||||
```
|
||||
|
||||
> 💡 **CliFx** has built-in analyzers that detect common errors in command definitions.
|
||||
> **Note**:
|
||||
> **CliFx** has built-in analyzers that detect common errors in command definitions.
|
||||
> Your code will not compile if the command contains duplicate options, overlapping parameters, or otherwise invalid configuration.
|
||||
|
||||
In order to execute this command, at a minimum, the user needs to provide the input value:
|
||||
@@ -483,7 +486,8 @@ COMMANDS
|
||||
You can run `dotnet myapp.dll cmd1 [command] --help` to show help on a specific command.
|
||||
```
|
||||
|
||||
> 💡 Defining a default (unnamed) command is not required.
|
||||
> **Note**:
|
||||
> Defining a default (unnamed) command is not required.
|
||||
> If it's absent, running the application without specifying a command will just show the root level help text.
|
||||
|
||||
### Reporting errors
|
||||
@@ -528,7 +532,8 @@ Division by zero is not supported.
|
||||
133
|
||||
```
|
||||
|
||||
> ⚠️ Even though exit codes are represented by 32-bit integers in .NET, using values outside 8-bit unsigned range will cause an overflow on Unix systems.
|
||||
> **Warning**:
|
||||
> Even though exit codes are represented by 32-bit integers in .NET, using values outside 8-bit unsigned range will cause an overflow on Unix systems.
|
||||
> To avoid unexpected results, use numbers between 1 and 255 for exit codes that indicate failure.
|
||||
|
||||
### Graceful cancellation
|
||||
@@ -561,7 +566,8 @@ public class CancellableCommand : ICommand
|
||||
}
|
||||
```
|
||||
|
||||
> ⚠️ Forceful termination of a command can only be delayed once.
|
||||
> **Warning**:
|
||||
> Forceful termination of a command can only be delayed once.
|
||||
> If the user issues a second interrupt signal, the process will be killed immediately without waiting for graceful cancellation.
|
||||
|
||||
### Type activation
|
||||
@@ -591,7 +597,7 @@ public static class Program
|
||||
|
||||
return await new CliApplicationBuilder()
|
||||
.AddCommandsFromThisAssembly()
|
||||
.UseTypeActivator(serviceProvider.GetService)
|
||||
.UseTypeActivator(serviceProvider)
|
||||
.Build()
|
||||
.RunAsync();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user