spectre.console icon indicating copy to clipboard operation
spectre.console copied to clipboard

Cannot convert from 'Spectre.Console.Table' to 'string'

Open gjaryczewski opened this issue 1 year ago • 5 comments

The following code returns the error:

using Spectre.Console;

namespace NewProject.Services;

public class ContextService : IContextService
{
    private readonly IRootService _rootService;

    public ContextService(IRootService rootService)
    {
        _rootService = rootService ?? throw new ArgumentNullException(nameof(rootService));
    }

    public void List()
    {
        var root = _rootService.GetPath();
        DirectoryInfo info = new DirectoryInfo(root);
        var directories = info.EnumerateDirectories();

        var table = new Table();
        table.Border = TableBorder.SimpleHeavy;
        table.AddColumn("Context");
        table.AddColumn("Path");

        foreach (var item in directories)
        {
            table.AddRow(item.Name, item.FullName);
        }

        AnsiConsole.Write(table); // Here is C:\Repos\NewProject\Services\ContextService.cs(36,27)
    }
}

The build output:

C:\Repos\NewProject\Services\ContextService.cs(36,27): error CS1503: Argument 1: cannot convert from 'Spectre.Console.Table' to 'string' [C:\Repos\NewProject\NewProject.csproj]

The build failed. Fix the build errors and run again.

Could anyone help me?

gjaryczewski avatar Sep 20 '22 20:09 gjaryczewski

What version of Spectre.Console are you using? .NET 6?

patriksvensson avatar Sep 20 '22 20:09 patriksvensson

Thank you for so fast reply, it's awesome! Yes: .NET 6.0.203, Spectre.Console 0.41.0.

gjaryczewski avatar Sep 20 '22 20:09 gjaryczewski

One of the versions since 0.41.0 we switched around some syntax to make things consistent in their behavior. Back then Write only took a string and you'd need to use Render for any of the IRenderable elements.

I'd recommend updating to a new version as we've done a ton of improvements in the past year.

phil-scott-78 avatar Sep 21 '22 01:09 phil-scott-78

Great idea, but I couldn't use other examples from documentation with the recent versions... All right, let's forget about it. Thank you for your support.

gjaryczewski avatar Sep 21 '22 12:09 gjaryczewski

What examples didn't work? If that's the case then we want to address that.

patriksvensson avatar Sep 21 '22 12:09 patriksvensson

Need to figure out what part of the documentation is wrong with the latest version. @gjaryczewski Any help would be appreciated here.

patriksvensson avatar Sep 22 '22 07:09 patriksvensson

:-) All right, I will repeat the exercise and find something useful during the weekend.

gjaryczewski avatar Sep 23 '22 20:09 gjaryczewski

All right, so the Program.cs looks like this:

using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;
using Spectre.Console.Cli;

public static class Program
{
    public static int Main(string[] args)
    {
        var app = new CommandApp();
        app.Configure(config =>
        {
            config.PropagateExceptions();
        });

        try
        {
            return app.Run(args);
        }
        catch (Exception ex)
        {
            AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);
            return -99;
        }
    }
}

The .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>test_spectre</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
    <PackageReference Include="Spectre.Console" Version="0.45.0" />
  </ItemGroup>

</Project>

The output of the build process contains: C:\Users\gjary\Repos\test-spectre\Program.cs(3,23): error CS0234: The type or namespace name 'Cli' does not exist in the namespace 'Spectre.Console' (are you missing an assembly reference? [C:\Users\gjary\Repos\test-spectre\test-spectre.csproj].

gjaryczewski avatar Sep 24 '22 10:09 gjaryczewski

According to the tutorial documentation, CommandApp is the entry point for a Spectre.Console.Cli command line application, this is also repeated in API Reference.

gjaryczewski avatar Sep 24 '22 10:09 gjaryczewski

The previous code works with 0.44. Does not work with 0.45.

gjaryczewski avatar Sep 24 '22 11:09 gjaryczewski

We split the Spectre.Console.Cli stuff into its own NuGet package for 0.45: https://www.nuget.org/packages/spectre.console.cli

patriksvensson avatar Sep 24 '22 14:09 patriksvensson

This is a good catch though I'll update the docs tonight to include a reference to the new package

phil-scott-78 avatar Sep 24 '22 14:09 phil-scott-78