mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Show valid values for parameters too
This commit is contained in:
@@ -82,16 +82,19 @@ namespace CliFx.Tests
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
[Command("cmd-with-enum-opts")]
|
||||
private class EnumOptionsCommand : ICommand
|
||||
[Command("cmd-with-enum-args")]
|
||||
private class EnumArgumentsCommand : ICommand
|
||||
{
|
||||
public enum ValuesEnum { Value1, Value2, Value3 };
|
||||
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 ValuesEnum Value { get; set; } = ValuesEnum.Value1;
|
||||
public TestEnum OptionA { get; set; } = TestEnum.Value1;
|
||||
|
||||
[CommandOption("nullable-value", Description = "Nullable enum option.")]
|
||||
public ValuesEnum? NullableValue { get; set; }
|
||||
public TestEnum? OptionB { get; set; }
|
||||
|
||||
public ValueTask ExecuteAsync(IConsole console) => default;
|
||||
}
|
||||
|
||||
@@ -239,25 +239,27 @@ namespace CliFx.Tests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Help_text_shows_usage_format_which_lists_all_valid_values_for_enum_options()
|
||||
public async Task Help_text_shows_usage_format_which_lists_all_valid_values_for_enum_arguments()
|
||||
{
|
||||
// Arrange
|
||||
await using var stdOut = new MemoryStream();
|
||||
var console = new VirtualConsole(output: stdOut);
|
||||
|
||||
var application = new CliApplicationBuilder()
|
||||
.AddCommand(typeof(EnumOptionsCommand))
|
||||
.AddCommand(typeof(EnumArgumentsCommand))
|
||||
.UseConsole(console)
|
||||
.Build();
|
||||
|
||||
// Act
|
||||
await application.RunAsync(new[] { "cmd-with-enum-opts", "--help" });
|
||||
await application.RunAsync(new[] { "cmd-with-enum-args", "--help" });
|
||||
var stdOutData = console.Output.Encoding.GetString(stdOut.ToArray()).TrimEnd();
|
||||
|
||||
// Assert
|
||||
stdOutData.Should().ContainAll(
|
||||
"Usage",
|
||||
"cmd-with-enum-opts", "[options]",
|
||||
"cmd-with-enum-args", "[options]",
|
||||
"Parameters",
|
||||
"value", "Valid values: Value1, Value2, Value3.",
|
||||
"Options",
|
||||
"* --value", "Enum option.", "Valid values: Value1, Value2, Value3.",
|
||||
"--nullable-value", "Nullable enum option.", "Valid values: Value1, Value2, Value3."
|
||||
|
||||
@@ -199,6 +199,15 @@ namespace CliFx.Domain
|
||||
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();
|
||||
@@ -246,12 +255,14 @@ namespace CliFx.Domain
|
||||
|
||||
RenderColumnIndent();
|
||||
|
||||
// Description
|
||||
if (!string.IsNullOrWhiteSpace(option.Description))
|
||||
{
|
||||
Render(option.Description);
|
||||
Render(" ");
|
||||
}
|
||||
|
||||
// Valid values
|
||||
var validValues = option.GetValidValues();
|
||||
if (validValues.Any())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user