spectreconsole / spectre.console

A .NET library that makes it easier to create beautiful console applications.
https://spectreconsole.net
MIT License
8.92k stars 454 forks source link

Add overload to allow one to set default command within the configuration delegate. #1469

Closed AraHaan closed 3 months ago

AraHaan commented 4 months ago

Is your feature request related to a problem? Please describe. For basic apps one can set the default command outside of the .Configure function. That is alright until you want for:

Describe the solution you'd like For a change like this to be accepted, https://github.com/AraHaan/spectre.console/commit/636ddb544b49d8ff5775ead2d8c2ec3a1c68f6eb

Describe alternatives you've considered Perhaps having it as an extension method that extends CommandApp?

Additional context

// 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.");
});

var finalArgs = new List<string>();
var firstArg = args.FirstOrDefault()?.Trim().ToLowerInvariant() ?? string.Empty;
if (firstArg is not "install" and not "uninstall" and not "update")
{
    finalArgs.Add("install");
}

if (args.Length != 0)
{
    finalArgs.AddRange(args);
}

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

This code above could be greatly simplified if I could configure using the overload version proposed here to set the default command instead of making a dummy list and possibly having bugs in my code, I then could also directly pass in args to app.RunAsync which will also improve performance of my application for free as well.

AraHaan commented 4 months ago

I guess another alternative would be adding a way to have AddCommand where one can specify it to be a default command (bool value with default of it being false) as well, with a name different from the default name for said default command. 😄

patriksvensson commented 4 months ago

@AraHaan You can set the default command by using CommandApp<T>.

var app = new CommandApp<MyDefaultCommand>();
AraHaan commented 4 months ago

@AraHaan You can set the default command by using CommandApp<T>.

var app = new CommandApp<MyDefaultCommand>();

Could, but that does not allow one to specify a specific name for the default command either. So I am thinking of ways around that as well.

patriksvensson commented 4 months ago

@AraHaan I'm sorry, but I don't quite follow what you mean.

patriksvensson commented 4 months ago

This is how Spectre.Console default commands are designed to work. If you want to add a named command as well, you do it in the configure phase.

var app = new CommandApp<InstallCommand>(); // Default command
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.");
});
FrankRay78 commented 4 months ago

This is how Spectre.Console default commands are designed to work. If you want to add a named command as well, you do it in the configure phase.

Has this now cleared up your issue @AraHaan?

AraHaan commented 3 months ago

Yes, but I would like a way to do it with and without the generic version of CommandApp which would be great (not to mention help unify both versions similarly which would help make it more maintainable as then the generic version could be defined as this exactly:

public class CommandApp<T> : CommandApp
{
   // ctor that bootstraps setting the passed in T as the default command.
}
patriksvensson commented 3 months ago

You can do that now as well:

var app = new CommandApp();
app.SetDefaultCommand<InstallCommand>();
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.");
});
FrankRay78 commented 3 months ago

Hi @AraHaan, I'm just triaging the open CLI issues. Has Patrik's response fully addressed your ask?

FrankRay78 commented 3 months ago

Closing. Feel free to reopen if something remains outstanding.