Skip to content

Commit

Permalink
Tyrrrz#13 - Add CliApplicationBuilder.AllowSuggestMode() support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricel committed Apr 12, 2021
1 parent 5958052 commit 4bb6e5c
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 2 deletions.
77 changes: 77 additions & 0 deletions CliFx.Tests/SuggestDirectiveSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using CliFx.Tests.Utils;
using CliFx.Tests.Utils.Extensions;
using FluentAssertions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace CliFx.Tests
{
public class SuggestDirectivesSpecs : SpecsBase
{
public SuggestDirectivesSpecs(ITestOutputHelper testOutput)
: base(testOutput)
{
}

private string _cmdCommandCs = @"
[Command(""cmd"")]
public class Command : ICommand
{
public ValueTask ExecuteAsync(IConsole console) => default;
}
";

public CliApplicationBuilder TestApplicationFactory(params string[] commandClasses)
{
var builder = new CliApplicationBuilder();

commandClasses.ToList().ForEach(c =>
{
var commandType = DynamicCommandBuilder.Compile(c);
builder = builder.AddCommand(commandType);
});

return builder.UseConsole(FakeConsole);
}

[Theory]
[InlineData(true, 0 )]
[InlineData(false, 1)]
public async Task Suggest_directive_can_be_configured(bool enabled, int expectedExitCode)
{
// Arrange
var application = TestApplicationFactory(_cmdCommandCs)
.AllowSuggestMode(enabled)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "[suggest]", "clifx.exe", "c" }
);

// Assert
exitCode.Should().Be(expectedExitCode);
}

[Fact]
public async Task Suggest_directive_is_enabled_by_default()
{
// Arrange
var application = TestApplicationFactory(_cmdCommandCs)
.Build();

// Act
var exitCode = await application.RunAsync(
new[] { "[suggest]", "clifx.exe", "c" }
);

// Assert
exitCode.Should().Be(0);
}
}
}
9 changes: 8 additions & 1 deletion CliFx/ApplicationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@ public class ApplicationConfiguration
/// </summary>
public bool IsPreviewModeAllowed { get; }

/// <summary>
/// Whether suggest mode is allowed in this application.
/// </summary>
public bool IsSuggestModeAllowed { get; }

/// <summary>
/// Initializes an instance of <see cref="ApplicationConfiguration"/>.
/// </summary>
public ApplicationConfiguration(
IReadOnlyList<Type> commandTypes,
bool isDebugModeAllowed,
bool isPreviewModeAllowed)
bool isPreviewModeAllowed,
bool isSuggestModeAllowed)
{
CommandTypes = commandTypes;
IsDebugModeAllowed = isDebugModeAllowed;
IsPreviewModeAllowed = isPreviewModeAllowed;
IsSuggestModeAllowed = isSuggestModeAllowed;
}
}
}
10 changes: 10 additions & 0 deletions CliFx/CliApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class CliApplication
private bool IsPreviewModeEnabled(CommandInput commandInput) =>
Configuration.IsPreviewModeAllowed && commandInput.IsPreviewDirectiveSpecified;

private bool IsSuggestModeEnabled(CommandInput commandInput) =>
Configuration.IsSuggestModeAllowed && commandInput.IsSuggestDirectiveSpecified;

private bool ShouldShowHelpText(CommandSchema commandSchema, CommandInput commandInput) =>
commandSchema.IsHelpOptionAvailable && commandInput.IsHelpOptionSpecified ||
// Show help text also in case the fallback default command is
Expand Down Expand Up @@ -103,6 +106,13 @@ private async ValueTask<int> RunAsync(ApplicationSchema applicationSchema, Comma
return 0;
}

// Handle suggest directive
if (IsSuggestModeEnabled(commandInput))
{
_console.Output.WriteLine("cmd");
return 0;
}

// Try to get the command schema that matches the input
var commandSchema =
applicationSchema.TryFindCommand(commandInput.CommandName) ??
Expand Down
14 changes: 13 additions & 1 deletion CliFx/CliApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public partial class CliApplicationBuilder

private bool _isDebugModeAllowed = true;
private bool _isPreviewModeAllowed = true;
private bool _isSuggestModeAllowed = true;
private string? _title;
private string? _executableName;
private string? _versionText;
Expand All @@ -36,6 +37,7 @@ public CliApplicationBuilder AddCommand(Type commandType)
return this;
}


/// <summary>
/// Adds a command to the application.
/// </summary>
Expand Down Expand Up @@ -110,6 +112,15 @@ public CliApplicationBuilder AllowPreviewMode(bool isAllowed = true)
return this;
}

/// <summary>
/// Specifies whether suggest mode (enabled with the [suggest] directive) is allowed in the application.
/// </summary>
public CliApplicationBuilder AllowSuggestMode(bool isAllowed = true)
{
_isSuggestModeAllowed = isAllowed;
return this;
}

/// <summary>
/// Sets application title, which is shown in the help text.
/// </summary>
Expand Down Expand Up @@ -196,7 +207,8 @@ public CliApplication Build()
var configuration = new ApplicationConfiguration(
_commandTypes.ToArray(),
_isDebugModeAllowed,
_isPreviewModeAllowed
_isPreviewModeAllowed,
_isSuggestModeAllowed
);

return new CliApplication(
Expand Down
2 changes: 2 additions & 0 deletions CliFx/Input/CommandInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ internal partial class CommandInput

public bool IsPreviewDirectiveSpecified => Directives.Any(d => d.IsPreviewDirective);

public bool IsSuggestDirectiveSpecified => Directives.Any(d => d.IsSuggestDirective);

public bool IsHelpOptionSpecified => Options.Any(o => o.IsHelpOption);

public bool IsVersionOptionSpecified => Options.Any(o => o.IsVersionOption);
Expand Down
3 changes: 3 additions & 0 deletions CliFx/Input/DirectiveInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ internal class DirectiveInput
public bool IsPreviewDirective =>
string.Equals(Name, "preview", StringComparison.OrdinalIgnoreCase);

public bool IsSuggestDirective =>
string.Equals(Name, "suggest", StringComparison.OrdinalIgnoreCase);

public DirectiveInput(string name) => Name = name;
}
}

0 comments on commit 4bb6e5c

Please sign in to comment.