Show choices on non-scalar enum parameters and options (#102)

This commit is contained in:
Robert Dailey
2021-04-08 12:51:17 -05:00
committed by GitHub
parent d7460244b7
commit 038f48b78e
2 changed files with 58 additions and 3 deletions

View File

@@ -577,6 +577,51 @@ public class Command : ICommand
);
}
[Fact]
public async Task Help_text_shows_all_valid_values_for_non_scalar_enum_parameters_and_options()
{
// Arrange
var commandType = DynamicCommandBuilder.Compile(
// language=cs
@"
public enum CustomEnum { One, Two, Three }
[Command]
public class Command : ICommand
{
[CommandParameter(0)]
public List<CustomEnum> Foo { get; set; }
[CommandOption(""bar"")]
public List<CustomEnum> Bar { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
");
var application = new CliApplicationBuilder()
.AddCommand(commandType)
.UseConsole(FakeConsole)
.Build();
// Act
var exitCode = await application.RunAsync(
new[] {"--help"},
new Dictionary<string, string>()
);
var stdOut = FakeConsole.ReadOutputString();
// Assert
exitCode.Should().Be(0);
stdOut.Should().ContainAllInOrder(
"PARAMETERS",
"foo", "Choices:", "One", "Two", "Three",
"OPTIONS",
"--bar", "Choices:", "One", "Two", "Three"
);
}
[Fact]
public async Task Help_text_shows_environment_variables_for_options_that_have_them_configured_as_fallback()
{

View File

@@ -21,7 +21,17 @@ namespace CliFx.Schema
public IReadOnlyList<object?> GetValidValues()
{
var underlyingType = Type.TryGetNullableUnderlyingType() ?? Type;
Type typeToCheck = Type;
foreach (var inf in typeToCheck.GetInterfaces())
{
if (inf.IsGenericType && inf.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
typeToCheck = inf.GenericTypeArguments[0];
break;
}
}
var underlyingType = typeToCheck.TryGetNullableUnderlyingType() ?? typeToCheck;
// We can only get valid values for enums
if (underlyingType.IsEnum)