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

Document if one can define their custom CommandSettings subtypes as "internal". #1467

Closed
AraHaan opened this issue Feb 18, 2024 · 3 comments
Closed

Comments

@AraHaan
Copy link

AraHaan commented Feb 18, 2024

I would love to be able to do this on one of my programs:

WorkloadSettings.cs:
namespace Elskom.Check;

internal class WorkloadSettings : CommandSettings
{
    [CommandOption("--sdk")]
    public string? SdkVersion { get; set; }

    [CommandOption("--rid")]
    public string? RuntimeIdentifier { get; set; }
}
InstallCommand.cs:
namespace Elskom.Check;

public class InstallCommand : AsyncCommand<WorkloadSettings>
{
    public override async Task<int> ExecuteAsync([NotNull] CommandContext context, [NotNull] WorkloadSettings settings)
    {
        DotNetSdkHelper.GetOrUpdateSdkVersion(ref settings);
        var workloadInfos = await NuGetHelper.ResolveWildcardWorkloadPackageVersionsAsync(settings.RuntimeIdentifier!).ConfigureAwait(false);
        await workloadInfos.InstallAsync(settings.SdkVersion!, settings.RuntimeIdentifier!).ConfigureAwait(false);
        return 0;
    }
}
Program.cs:
// set the console title.
Console.Title = "Elskom workload cross-platform installer";

// Need to register the code pages provider for code that parses
// and later needs ISO-8859-2
Encoding.RegisterProvider(
    CodePagesEncodingProvider.Instance);

// Test that it loads
_ = Encoding.GetEncoding("ISO-8859-2");
var app = new CommandApp();
app.Configure(config =>
{
    config.AddCommand<InstallCommand>("install").WithDescription("Installs the Workload.");
    config.AddCommand<UninstallCommand>("uninstall").WithDescription("Uninstalls the Workload.");
    config.AddCommand<UpdateCommand>("update").WithDescription("Updates the Workload.");
});

// This args stuff could probably be simplified.
var finalArgs = new List<string>();
var firstArg = args.FirstOrDefault()?.Trim().ToLowerInvariant() ?? string.Empty;
if (firstArg != "install" && firstArg != "uninstall" && firstArg != "update")
{
    finalArgs.Add("install");
}

if (args.Any())
{
    finalArgs.AddRange(args);
}

using (NuGetHelper.HttpClient = new HttpClient())
{
    var result = await app.RunAsync(finalArgs).ConfigureAwait(false);
    Console.Title = "";
    return result;
}
@AraHaan
Copy link
Author

AraHaan commented Feb 18, 2024

It seems if I made the settings type internal without making the types in my cli program (like InstallCommand) internal as well that the code does not compile, but unclear that if I do that as well if Spectre would recognize those as commands too.

@AraHaan
Copy link
Author

AraHaan commented Feb 18, 2024

Ok, I did some testing by moving all the real code that the cli program project itself uses into a separate library project (including the command classes and made them all internal, added InternalsVisibleTo on that separate project for the cli project to see the internals), then built and debug went smoothly.

@patriksvensson
Copy link
Contributor

Either both command and settings need to be internal, or command and settings public. Mixing internal and public will not compile, and is nothibg that we will try to support.

@github-project-automation github-project-automation bot moved this from Todo 🕑 to Done 🚀 in Spectre Console Feb 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done 🚀
Development

No branches or pull requests

2 participants