Skip to content

Commit

Permalink
Fix issues with nullability and netstandard2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
patriksvensson committed Nov 10, 2022
1 parent 7dce4af commit a70cc90
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 27 deletions.
10 changes: 8 additions & 2 deletions src/Spectre.Console.Cli/Internal/Binding/CommandValueResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static CommandValueLookup GetParameterValues(CommandTree? tree, ITypeReso
}

// Assign the value to the parameter.
binder.Bind(mapped.Parameter, resolver, converter.ConvertFromInvariantString(mapped.Value));
binder.Bind(mapped.Parameter, resolver, converter.ConvertFromInvariantString(mapped.Value ?? string.Empty));
}
}

Expand Down Expand Up @@ -130,7 +130,13 @@ public static CommandValueLookup GetParameterValues(CommandTree? tree, ITypeReso
if (parameter.ParameterType.IsArray)
{
// Return a converter for each array item (not the whole array)
return TypeDescriptor.GetConverter(parameter.ParameterType.GetElementType());
var elementType = parameter.ParameterType.GetElementType();
if (elementType == null)
{
throw new InvalidOperationException("Could not get element type");
}

return TypeDescriptor.GetConverter(elementType);
}

if (parameter.IsFlagValue())
Expand Down
6 changes: 3 additions & 3 deletions src/Spectre.Console.Cli/Internal/HelpWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public static IReadOnlyList<HelpArgument> Get(CommandInfo? command)

private sealed class HelpOption
{
public string Short { get; }
public string Long { get; }
public string? Short { get; }
public string? Long { get; }
public string? Value { get; }
public bool? ValueIsOptional { get; }
public string? Description { get; }

public HelpOption(string @short, string @long, string? @value, bool? valueIsOptional, string? description)
public HelpOption(string? @short, string? @long, string? @value, bool? valueIsOptional, string? description)
{
Short = @short;
Long = @long;
Expand Down
12 changes: 6 additions & 6 deletions src/Spectre.Console.Cli/Spectre.Console.Cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
<InternalsVisibleTo Include="Spectre.Console.Cli.Tests" />
</ItemGroup>

<ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />
<PackageReference Include="Nullable" Version="1.3.1">
Expand All @@ -26,9 +31,4 @@
</PackageReference>
</ItemGroup>

<PropertyGroup>
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>
</PropertyGroup>

</Project>
16 changes: 11 additions & 5 deletions src/Spectre.Console/Internal/TypeConverterHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ internal static class TypeConverterHelper
{
public static string ConvertToString<T>(T input)
{
return GetTypeConverter<T>().ConvertToInvariantString(input);
var result = GetTypeConverter<T>().ConvertToInvariantString(input);
if (result == null)
{
throw new InvalidOperationException("Could not convert input to a string");
}

return result;
}

public static bool TryConvertFromString<T>(string input, [MaybeNull] out T result)
public static bool TryConvertFromString<T>(string input, [MaybeNull] out T? result)
{
try
{
result = (T)GetTypeConverter<T>().ConvertFromInvariantString(input);
result = (T?)GetTypeConverter<T>().ConvertFromInvariantString(input);
return true;
}
catch
Expand All @@ -21,7 +27,7 @@ public static bool TryConvertFromString<T>(string input, [MaybeNull] out T resul
}
}

public static bool TryConvertFromStringWithCulture<T>(string input, CultureInfo? info, [MaybeNull] out T result)
public static bool TryConvertFromStringWithCulture<T>(string input, CultureInfo? info, [MaybeNull] out T? result)
{
try
{
Expand All @@ -31,7 +37,7 @@ public static bool TryConvertFromStringWithCulture<T>(string input, CultureInfo?
}
else
{
result = (T)GetTypeConverter<T>().ConvertFromString(null!, info, input);
result = (T?)GetTypeConverter<T>().ConvertFromString(null!, info, input);
}

return true;
Expand Down
17 changes: 10 additions & 7 deletions src/Spectre.Console/Spectre.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@

<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Wcwidth.Sources" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<AnnotatedReferenceAssemblyVersion>3.0.0</AnnotatedReferenceAssemblyVersion>
<GenerateNullableAttributes>False</GenerateNullableAttributes>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="TunnelVisionLabs.ReferenceAssemblyAnnotator" Version="1.0.0-alpha.160" PrivateAssets="all" />
<PackageDownload Include="Microsoft.NETCore.App.Ref" Version="[$(AnnotatedReferenceAssemblyVersion)]" />
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants)TRACE;WCWIDTH_VISIBILITY_INTERNAL</DefineConstants>
</PropertyGroup>
Expand Down
10 changes: 8 additions & 2 deletions src/Spectre.Console/Widgets/Figlet/FigletFontParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ internal static class FigletFontParser
public static FigletFont Parse(string source)
{
var lines = source.SplitLines();
var header = ParseHeader(lines.FirstOrDefault());

var characters = new List<FigletCharacter>();
var headerLine = lines.FirstOrDefault();
if (headerLine == null)
{
throw new InvalidOperationException("Could not read header line");
}

var header = ParseHeader(headerLine);

var index = 32;
var indexOverridden = false;
var hasOverriddenIndex = false;
var buffer = new List<string>();
var characters = new List<FigletCharacter>();

foreach (var line in lines.Skip(header.CommentLines + 1))
{
Expand Down
3 changes: 1 addition & 2 deletions src/Spectre.Console/Widgets/Table/TableRowCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ public void Update(int row, int column, IRenderable cellData)
throw new IndexOutOfRangeException("Table row index cannot exceed the number of rows in the table.");
}

var tableRow = _list.ElementAtOrDefault(row);

var tableRow = _list.ElementAt(row);
var currentRenderables = tableRow.ToList();

if (column < 0)
Expand Down

0 comments on commit a70cc90

Please sign in to comment.