mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-19 10:12:50 +08:00
Create Test, allow for conditional trim
This commit is contained in:
parent
a91a3c12ad
commit
f6a7c96413
@ -73,6 +73,23 @@ public static class ConfiguratorExtensions
|
||||
return configurator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tells the help writer whether or not to trim trailing period.
|
||||
/// </summary>
|
||||
/// <param name="configurator">The configurator.</param>
|
||||
/// <param name="trimTrailingPeriods">True to trim trailing period (default), false to not.</param>
|
||||
/// <returns>A configurator that can be used to configure the application further.</returns>
|
||||
public static IConfigurator TrimTrailingPeriods(this IConfigurator configurator, bool trimTrailingPeriods)
|
||||
{
|
||||
if (configurator == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configurator));
|
||||
}
|
||||
|
||||
configurator.Settings.TrimTrailingPeriod = trimTrailingPeriods;
|
||||
return configurator;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tells the command line application to propagate all
|
||||
/// exceptions to the user.
|
||||
|
@ -36,6 +36,11 @@ public interface ICommandAppSettings
|
||||
/// </summary>
|
||||
CaseSensitivity CaseSensitivity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether trailing period of a description is trimmed.
|
||||
/// </summary>
|
||||
bool TrimTrailingPeriod { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether or not parsing is strict.
|
||||
/// </summary>
|
||||
|
@ -10,6 +10,7 @@ internal sealed class CommandAppSettings : ICommandAppSettings
|
||||
public CaseSensitivity CaseSensitivity { get; set; }
|
||||
public bool PropagateExceptions { get; set; }
|
||||
public bool ValidateExamples { get; set; }
|
||||
public bool TrimTrailingPeriod { get; set; } = true;
|
||||
public bool StrictParsing { get; set; }
|
||||
|
||||
public ParsingMode ParsingMode =>
|
||||
|
@ -375,10 +375,19 @@ internal static class HelpWriter
|
||||
arguments.Space();
|
||||
}
|
||||
|
||||
if (model.TrimTrailingPeriod)
|
||||
{
|
||||
grid.AddRow(
|
||||
arguments.ToString().TrimEnd(),
|
||||
child.Description?.TrimEnd('.') ?? " ");
|
||||
}
|
||||
else
|
||||
{
|
||||
grid.AddRow(
|
||||
arguments.ToString().TrimEnd(),
|
||||
child.Description ?? " ");
|
||||
}
|
||||
}
|
||||
|
||||
result.Add(grid);
|
||||
|
||||
|
@ -7,6 +7,7 @@ internal sealed class CommandModel : ICommandContainer
|
||||
public CommandInfo? DefaultCommand { get; }
|
||||
public IList<CommandInfo> Commands { get; }
|
||||
public IList<string[]> Examples { get; }
|
||||
public bool TrimTrailingPeriod { get; }
|
||||
|
||||
public CommandModel(
|
||||
CommandAppSettings settings,
|
||||
@ -16,6 +17,7 @@ internal sealed class CommandModel : ICommandContainer
|
||||
{
|
||||
ApplicationName = settings.ApplicationName;
|
||||
ParsingMode = settings.ParsingMode;
|
||||
TrimTrailingPeriod = settings.TrimTrailingPeriod;
|
||||
DefaultCommand = defaultCommand;
|
||||
Commands = new List<CommandInfo>(commands ?? Array.Empty<CommandInfo>());
|
||||
Examples = new List<string[]>(examples ?? Array.Empty<string[]>());
|
||||
|
@ -1,4 +1,4 @@
|
||||
DESCRIPTION:
|
||||
DESCRIPTION:
|
||||
Contains settings for a cat.
|
||||
|
||||
USAGE:
|
||||
@ -14,4 +14,4 @@ OPTIONS:
|
||||
--agility <VALUE> The agility between 0 and 100
|
||||
|
||||
COMMANDS:
|
||||
lion <TEETH> The lion command.
|
||||
lion <TEETH> The lion command
|
@ -1,4 +1,4 @@
|
||||
DESCRIPTION:
|
||||
DESCRIPTION:
|
||||
The animal command.
|
||||
|
||||
USAGE:
|
||||
@ -15,5 +15,5 @@ OPTIONS:
|
||||
-a, --alive Indicates whether or not the animal is alive
|
||||
|
||||
COMMANDS:
|
||||
dog <AGE> The dog command.
|
||||
horse The horse command.
|
||||
dog <AGE> The dog command
|
||||
horse The horse command
|
@ -0,0 +1,10 @@
|
||||
USAGE:
|
||||
myapp [OPTIONS] <COMMAND>
|
||||
|
||||
OPTIONS:
|
||||
-h, --help Prints help information
|
||||
-v, --version Prints version information
|
||||
|
||||
COMMANDS:
|
||||
dog <AGE> The dog command.
|
||||
horse The horse command.
|
@ -6,5 +6,5 @@ OPTIONS:
|
||||
-v, --version Prints version information
|
||||
|
||||
COMMANDS:
|
||||
dog <AGE> The dog command.
|
||||
horse The horse command.
|
||||
dog <AGE> The dog command
|
||||
horse The horse command
|
@ -6,6 +6,6 @@ OPTIONS:
|
||||
-v, --version Prints version information
|
||||
|
||||
COMMANDS:
|
||||
dog <AGE> The dog command.
|
||||
horse The horse command.
|
||||
giraffe <LENGTH> The giraffe command.
|
||||
dog <AGE> The dog command
|
||||
horse The horse command
|
||||
giraffe <LENGTH> The giraffe command
|
@ -10,5 +10,5 @@ OPTIONS:
|
||||
-v, --version Prints version information
|
||||
|
||||
COMMANDS:
|
||||
dog <AGE> The dog command.
|
||||
horse The horse command.
|
||||
dog <AGE> The dog command
|
||||
horse The horse command
|
@ -10,5 +10,5 @@ OPTIONS:
|
||||
-v, --version Prints version information
|
||||
|
||||
COMMANDS:
|
||||
dog <AGE> The dog command.
|
||||
horse The horse command.
|
||||
dog <AGE> The dog command
|
||||
horse The horse command
|
@ -10,4 +10,4 @@ OPTIONS:
|
||||
-v, --version Prints version information
|
||||
|
||||
COMMANDS:
|
||||
animal The animal command.
|
||||
animal The animal command
|
@ -31,4 +31,15 @@
|
||||
<ProjectReference Include="..\..\src\Spectre.Console\Spectre.Console.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Expectations\Help\Description_No_Trailing_Period.Output.received.txt">
|
||||
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
|
||||
<DependentUpon>%(ParentFile).cs</DependentUpon>
|
||||
</None>
|
||||
<None Update="Expectations\Help\Description_No_Trailing_Period.Output.verified.txt">
|
||||
<ParentFile>$([System.String]::Copy('%(FileName)').Split('.')[0])</ParentFile>
|
||||
<DependentUpon>%(ParentFile).cs</DependentUpon>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,3 +1,5 @@
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli;
|
||||
|
||||
public sealed partial class CommandAppTests
|
||||
@ -50,6 +52,30 @@ public sealed partial class CommandAppTests
|
||||
return Verifier.Verify(result.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Expectation("Description_No_Trailing_Period")]
|
||||
public Task Should_Not_Trim_Description_Trailing_Period()
|
||||
{
|
||||
// Given
|
||||
var fixture = new CommandAppTester();
|
||||
fixture.Configure(configurator =>
|
||||
{
|
||||
configurator.SetApplicationName("myapp");
|
||||
configurator.AddCommand<DogCommand>("dog");
|
||||
configurator.AddCommand<HorseCommand>("horse");
|
||||
configurator.AddCommand<GiraffeCommand>("giraffe")
|
||||
.WithExample(new[] { "giraffe", "123" })
|
||||
.IsHidden();
|
||||
configurator.TrimTrailingPeriods(false);
|
||||
});
|
||||
|
||||
// When
|
||||
var result = fixture.Run("--help");
|
||||
|
||||
// Then
|
||||
return Verifier.Verify(result.Output);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
[Expectation("Command")]
|
||||
public Task Should_Output_Command_Correctly()
|
||||
|
Loading…
x
Reference in New Issue
Block a user