mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Cleanup
This commit is contained in:
@@ -10,7 +10,6 @@ namespace CliFx.Domain
|
||||
{
|
||||
internal abstract partial class CommandArgumentSchema
|
||||
{
|
||||
private IReadOnlyList<string>? _validValues;
|
||||
public PropertyInfo Property { get; }
|
||||
|
||||
public string? Description { get; }
|
||||
@@ -19,52 +18,12 @@ namespace CliFx.Domain
|
||||
|
||||
public bool IsScalar => TryGetEnumerableArgumentUnderlyingType() == null;
|
||||
|
||||
public IReadOnlyList<string> GetValidValues() => _validValues ??
|
||||
(_validValues = EnumerateValidValues().ToList().AsReadOnly());
|
||||
|
||||
protected CommandArgumentSchema(PropertyInfo property, string? description)
|
||||
{
|
||||
Property = property;
|
||||
Description = description;
|
||||
}
|
||||
|
||||
private IEnumerable<string> EnumerateValidValues()
|
||||
{
|
||||
var propertyType = Property?.PropertyType;
|
||||
|
||||
// Property can actually be null here due to damn it operators
|
||||
// in CommandOptionSchema lines 103 and 106, so we have to check
|
||||
// for now. In such case that it is null, let's end early.
|
||||
if (propertyType is null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
// If 'propertyType' is nullable, this will return a non-null value.
|
||||
var underlyingType = propertyType.GetNullableUnderlyingType();
|
||||
|
||||
// If 'propertyType' is nullable, 'underlying' type will be not null.
|
||||
if (underlyingType is object)
|
||||
{
|
||||
// Handle nullable num.
|
||||
if (underlyingType.IsEnum)
|
||||
{
|
||||
// Reasign so we can do the 'foreach' over the enum values
|
||||
// only once at the end of the method.
|
||||
propertyType = underlyingType;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle non-nullable enums or nullable enums that were "unwrapped".
|
||||
if (propertyType.IsEnum)
|
||||
{
|
||||
foreach (var value in Enum.GetValues(propertyType))
|
||||
{
|
||||
yield return value.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Type? TryGetEnumerableArgumentUnderlyingType() =>
|
||||
Property.PropertyType != typeof(string)
|
||||
? Property.PropertyType.GetEnumerableUnderlyingType()
|
||||
@@ -157,6 +116,24 @@ namespace CliFx.Domain
|
||||
|
||||
public void Inject(ICommand command, params string[] values) =>
|
||||
Inject(command, (IReadOnlyList<string>) values);
|
||||
|
||||
public IReadOnlyList<string> GetValidValues()
|
||||
{
|
||||
var result = new List<string>();
|
||||
|
||||
// Some arguments may have this as null due to a hack that enables built-in options
|
||||
if (Property == null)
|
||||
return result;
|
||||
|
||||
var underlyingPropertyType =
|
||||
Property.PropertyType.GetNullableUnderlyingType() ?? Property.PropertyType;
|
||||
|
||||
// Enum
|
||||
if (underlyingPropertyType.IsEnum)
|
||||
result.AddRange(Enum.GetNames(underlyingPropertyType));
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
internal partial class CommandArgumentSchema
|
||||
|
||||
@@ -43,10 +43,6 @@ namespace CliFx.Domain
|
||||
ShortName != null &&
|
||||
ShortName == shortName;
|
||||
|
||||
public bool MatchesNameOrShortName(string? name, char? shortName) =>
|
||||
MatchesName(name) ||
|
||||
MatchesShortName(shortName);
|
||||
|
||||
public bool MatchesNameOrShortName(string alias) =>
|
||||
MatchesName(alias) ||
|
||||
alias.Length == 1 && MatchesShortName(alias.Single());
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace CliFx.Domain
|
||||
}
|
||||
else
|
||||
{
|
||||
Render(" ");
|
||||
RenderIndent(margin);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,7 +264,7 @@ namespace CliFx.Domain
|
||||
// Environment variable
|
||||
if (!string.IsNullOrWhiteSpace(option.EnvironmentVariableName))
|
||||
{
|
||||
Render($"(Environment variable: {option.EnvironmentVariableName})");
|
||||
Render($"Environment variable: {option.EnvironmentVariableName}");
|
||||
}
|
||||
|
||||
RenderNewLine();
|
||||
|
||||
Reference in New Issue
Block a user