mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-07-04 11:48:16 +08:00
Add support for converting command parameters into FileInfo and DirectoryInfo (#1145)
Add support for converting command parameters that doesn't have a built-in TypeConverter but has a constructor that takes a string. For CLI apps, FileInfo and DirectoryInfo will likely be the most useful ones, but there may be others.
This commit is contained in:
@ -1,7 +1,15 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Spectre.Console.Tests.Data;
|
||||
|
||||
public class HorseSettings : MammalSettings
|
||||
{
|
||||
[CommandOption("-d|--day")]
|
||||
public DayOfWeek Day { get; set; }
|
||||
|
||||
[CommandOption("--file")]
|
||||
public FileInfo File { get; set; }
|
||||
|
||||
[CommandOption("--directory")]
|
||||
public DirectoryInfo Directory { get; set; }
|
||||
}
|
@ -30,6 +30,8 @@
|
||||
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Parameters>
|
||||
<Option Short="d" Long="day" Value="NULL" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
<Option Short="" Long="file" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.FileInfo" />
|
||||
</Parameters>
|
||||
</Command>
|
||||
</Command>
|
||||
|
@ -26,6 +26,8 @@
|
||||
<Command Name="horse" IsBranch="false" ClrType="Spectre.Console.Tests.Data.HorseCommand" Settings="Spectre.Console.Tests.Data.HorseSettings">
|
||||
<Parameters>
|
||||
<Option Short="d" Long="day" Value="NULL" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
<Option Short="" Long="file" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.FileInfo" />
|
||||
<Option Short="n,p" Long="name,pet-name" Value="VALUE" Required="false" Kind="scalar" ClrType="System.String" />
|
||||
</Parameters>
|
||||
</Command>
|
||||
|
@ -32,6 +32,8 @@
|
||||
<Description>Indicates whether or not the animal is alive.</Description>
|
||||
</Option>
|
||||
<Option Short="d" Long="day" Value="NULL" Required="false" Kind="scalar" ClrType="System.DayOfWeek" />
|
||||
<Option Short="" Long="directory" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.DirectoryInfo" />
|
||||
<Option Short="" Long="file" Value="NULL" Required="false" Kind="scalar" ClrType="System.IO.FileInfo" />
|
||||
<Option Short="n,p" Long="name,pet-name" Value="VALUE" Required="false" Kind="scalar" ClrType="System.String" />
|
||||
</Parameters>
|
||||
</Command>
|
||||
|
@ -1,3 +1,5 @@
|
||||
using System.IO;
|
||||
|
||||
namespace Spectre.Console.Tests.Unit.Cli;
|
||||
|
||||
public sealed partial class CommandAppTests
|
||||
@ -76,5 +78,27 @@ public sealed partial class CommandAppTests
|
||||
result.Output.ShouldContain(nameof(DayOfWeek.Friday));
|
||||
result.Output.ShouldContain(nameof(DayOfWeek.Saturday));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Should_Convert_FileInfo_And_DirectoryInfo()
|
||||
{
|
||||
// Given
|
||||
var app = new CommandAppTester();
|
||||
app.Configure(config =>
|
||||
{
|
||||
config.AddCommand<HorseCommand>("horse");
|
||||
});
|
||||
|
||||
// When
|
||||
var result = app.Run(new[] { "horse", "--file", "ntp.conf", "--directory", "etc" });
|
||||
|
||||
// Then
|
||||
result.ExitCode.ShouldBe(0);
|
||||
result.Settings.ShouldBeOfType<HorseSettings>().And(horse =>
|
||||
{
|
||||
horse.File.Name.ShouldBe("ntp.conf");
|
||||
horse.Directory.Name.ShouldBe("etc");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user