Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gjaryczewski opened this issue Sep 20, 2022 · 12 comments · Fixed by #1413
Closed

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

gjaryczewski opened this issue Sep 20, 2022 · 12 comments · Fixed by #1413
Assignees
Labels
bug Something isn't working

Comments

@gjaryczewski
Copy link

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?

@patriksvensson
Copy link
Contributor

patriksvensson commented Sep 20, 2022

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

@gjaryczewski
Copy link
Author

gjaryczewski commented Sep 20, 2022

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

@phil-scott-78
Copy link
Contributor

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.

@gjaryczewski
Copy link
Author

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.

@patriksvensson
Copy link
Contributor

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

@patriksvensson patriksvensson added the bug Something isn't working label Sep 22, 2022
@patriksvensson
Copy link
Contributor

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

@gjaryczewski
Copy link
Author

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

@gjaryczewski
Copy link
Author

gjaryczewski commented Sep 24, 2022

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
Copy link
Author

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
Copy link
Author

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

@patriksvensson
Copy link
Contributor

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

@phil-scott-78
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done 🚀
Development

Successfully merging a pull request may close this issue.

4 participants