Add possibility to set description and/or data for the default command (#1091)

This commit is contained in:
Cédric Luthi
2023-01-12 13:12:27 +01:00
committed by GitHub
parent f223f6061c
commit 7b9553dd22
6 changed files with 151 additions and 31 deletions

View File

@ -25,11 +25,12 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
Examples.Add(args);
}
public void SetDefaultCommand<TDefaultCommand>()
public ConfiguredCommand SetDefaultCommand<TDefaultCommand>()
where TDefaultCommand : class, ICommand
{
DefaultCommand = ConfiguredCommand.FromType<TDefaultCommand>(
CliConstants.DefaultCommandName, isDefaultCommand: true);
return DefaultCommand;
}
public ICommandConfigurator AddCommand<TCommand>(string name)

View File

@ -0,0 +1,36 @@
namespace Spectre.Console.Cli.Internal.Configuration;
/// <summary>
/// Fluent configurator for the default command.
/// </summary>
public sealed class DefaultCommandConfigurator
{
private readonly ConfiguredCommand _defaultCommand;
internal DefaultCommandConfigurator(ConfiguredCommand defaultCommand)
{
_defaultCommand = defaultCommand;
}
/// <summary>
/// Sets the description of the default command.
/// </summary>
/// <param name="description">The default command description.</param>
/// <returns>The same <see cref="DefaultCommandConfigurator"/> instance so that multiple calls can be chained.</returns>
public DefaultCommandConfigurator WithDescription(string description)
{
_defaultCommand.Description = description;
return this;
}
/// <summary>
/// Sets data that will be passed to the command via the <see cref="CommandContext"/>.
/// </summary>
/// <param name="data">The data to pass to the default command.</param>
/// <returns>The same <see cref="DefaultCommandConfigurator"/> instance so that multiple calls can be chained.</returns>
public DefaultCommandConfigurator WithData(object data)
{
_defaultCommand.Data = data;
return this;
}
}