This commit is contained in:
Tyrrrz
2021-04-24 20:43:35 +03:00
parent 2bb7e13e51
commit 2d479c9cb6
3 changed files with 18 additions and 6 deletions

View File

@@ -21,7 +21,7 @@ namespace CliFx.Demo.Domain
var data = File.ReadAllText(StorageFilePath);
return JsonConvert.DeserializeObject<Library>(data);
return JsonConvert.DeserializeObject<Library>(data) ?? Library.Empty;
}
public Book? TryGetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);

View File

@@ -590,10 +590,10 @@ public enum CustomEnum { One, Two, Three }
public class Command : ICommand
{
[CommandParameter(0)]
public List<CustomEnum> Foo { get; set; }
public IReadOnlyList<CustomEnum> Foo { get; set; }
[CommandOption(""bar"")]
public List<CustomEnum> Bar { get; set; }
public IReadOnlyList<CustomEnum> Bar { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}
@@ -638,7 +638,7 @@ public class Command : ICommand
public CustomEnum? Foo { get; set; }
[CommandOption(""bar"")]
public List<CustomEnum?> Bar { get; set; }
public IReadOnlyList<CustomEnum?> Bar { get; set; }
public ValueTask ExecuteAsync(IConsole console) => default;
}

View File

@@ -21,8 +21,20 @@ namespace CliFx.Schema
public IReadOnlyList<object?> GetValidValues()
{
var underlyingType = Type.TryGetEnumerableUnderlyingType() ?? Type;
underlyingType = underlyingType.TryGetNullableUnderlyingType() ?? underlyingType;
static Type GetUnderlyingType(Type type)
{
var enumerableUnderlyingType = type.TryGetEnumerableUnderlyingType();
if (enumerableUnderlyingType is not null)
return GetUnderlyingType(enumerableUnderlyingType);
var nullableUnderlyingType = type.TryGetNullableUnderlyingType();
if (nullableUnderlyingType is not null)
return GetUnderlyingType(nullableUnderlyingType);
return type;
}
var underlyingType = GetUnderlyingType(Type);
// We can only get valid values for enums
if (underlyingType.IsEnum)