mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-08-02 18:17:30 +08:00
Add possibility to set description and/or data for the default command (#1091)
This commit is contained in:
@ -1,3 +1,5 @@
|
||||
using Spectre.Console.Cli.Internal.Configuration;
|
||||
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
/// <summary>
|
||||
@ -39,10 +41,11 @@ public sealed class CommandApp : ICommandApp
|
||||
/// Sets the default command.
|
||||
/// </summary>
|
||||
/// <typeparam name="TCommand">The command type.</typeparam>
|
||||
public void SetDefaultCommand<TCommand>()
|
||||
/// <returns>A <see cref="DefaultCommandConfigurator"/> that can be used to configure the default command.</returns>
|
||||
public DefaultCommandConfigurator SetDefaultCommand<TCommand>()
|
||||
where TCommand : class, ICommand
|
||||
{
|
||||
GetConfigurator().SetDefaultCommand<TCommand>();
|
||||
return new DefaultCommandConfigurator(GetConfigurator().SetDefaultCommand<TCommand>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -1,3 +1,5 @@
|
||||
using Spectre.Console.Cli.Internal.Configuration;
|
||||
|
||||
namespace Spectre.Console.Cli;
|
||||
|
||||
/// <summary>
|
||||
@ -8,6 +10,7 @@ public sealed class CommandApp<TDefaultCommand> : ICommandApp
|
||||
where TDefaultCommand : class, ICommand
|
||||
{
|
||||
private readonly CommandApp _app;
|
||||
private readonly DefaultCommandConfigurator _defaultCommandConfigurator;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CommandApp{TDefaultCommand}"/> class.
|
||||
@ -16,7 +19,7 @@ public sealed class CommandApp<TDefaultCommand> : ICommandApp
|
||||
public CommandApp(ITypeRegistrar? registrar = null)
|
||||
{
|
||||
_app = new CommandApp(registrar);
|
||||
_app.GetConfigurator().SetDefaultCommand<TDefaultCommand>();
|
||||
_defaultCommandConfigurator = _app.SetDefaultCommand<TDefaultCommand>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -46,5 +49,32 @@ public sealed class CommandApp<TDefaultCommand> : ICommandApp
|
||||
public Task<int> RunAsync(IEnumerable<string> args)
|
||||
{
|
||||
return _app.RunAsync(args);
|
||||
}
|
||||
|
||||
internal Configurator GetConfigurator()
|
||||
{
|
||||
return _app.GetConfigurator();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the description of the default command.
|
||||
/// </summary>
|
||||
/// <param name="description">The default command description.</param>
|
||||
/// <returns>The same <see cref="CommandApp{TDefaultCommand}"/> instance so that multiple calls can be chained.</returns>
|
||||
public CommandApp<TDefaultCommand> WithDescription(string description)
|
||||
{
|
||||
_defaultCommandConfigurator.WithDescription(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="CommandApp{TDefaultCommand}"/> instance so that multiple calls can be chained.</returns>
|
||||
public CommandApp<TDefaultCommand> WithData(object data)
|
||||
{
|
||||
_defaultCommandConfigurator.WithData(data);
|
||||
return this;
|
||||
}
|
||||
}
|
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -25,11 +25,25 @@ public sealed class CommandAppTester
|
||||
/// <summary>
|
||||
/// Sets the default command.
|
||||
/// </summary>
|
||||
/// <param name="description">The optional default command description.</param>
|
||||
/// <param name="data">The optional default command data.</param>
|
||||
/// <typeparam name="T">The default command type.</typeparam>
|
||||
public void SetDefaultCommand<T>()
|
||||
public void SetDefaultCommand<T>(string? description = null, object? data = null)
|
||||
where T : class, ICommand
|
||||
{
|
||||
_appConfiguration = (app) => app.SetDefaultCommand<T>();
|
||||
_appConfiguration = (app) =>
|
||||
{
|
||||
var defaultCommandBuilder = app.SetDefaultCommand<T>();
|
||||
if (description != null)
|
||||
{
|
||||
defaultCommandBuilder.WithDescription(description);
|
||||
}
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
defaultCommandBuilder.WithData(data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user