mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
Alias for command branches (#411)
This commit is contained in:
parent
f4183e0462
commit
04610cf492
@ -181,7 +181,8 @@ public static class ConfiguratorExtensions
|
|||||||
/// <param name="configurator">The configurator.</param>
|
/// <param name="configurator">The configurator.</param>
|
||||||
/// <param name="name">The name of the command branch.</param>
|
/// <param name="name">The name of the command branch.</param>
|
||||||
/// <param name="action">The command branch configuration.</param>
|
/// <param name="action">The command branch configuration.</param>
|
||||||
public static void AddBranch(
|
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
|
||||||
|
public static IBranchConfigurator AddBranch(
|
||||||
this IConfigurator configurator,
|
this IConfigurator configurator,
|
||||||
string name,
|
string name,
|
||||||
Action<IConfigurator<CommandSettings>> action)
|
Action<IConfigurator<CommandSettings>> action)
|
||||||
@ -191,7 +192,7 @@ public static class ConfiguratorExtensions
|
|||||||
throw new ArgumentNullException(nameof(configurator));
|
throw new ArgumentNullException(nameof(configurator));
|
||||||
}
|
}
|
||||||
|
|
||||||
configurator.AddBranch(name, action);
|
return configurator.AddBranch(name, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -201,7 +202,8 @@ public static class ConfiguratorExtensions
|
|||||||
/// <param name="configurator">The configurator.</param>
|
/// <param name="configurator">The configurator.</param>
|
||||||
/// <param name="name">The name of the command branch.</param>
|
/// <param name="name">The name of the command branch.</param>
|
||||||
/// <param name="action">The command branch configuration.</param>
|
/// <param name="action">The command branch configuration.</param>
|
||||||
public static void AddBranch<TSettings>(
|
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
|
||||||
|
public static IBranchConfigurator AddBranch<TSettings>(
|
||||||
this IConfigurator<TSettings> configurator,
|
this IConfigurator<TSettings> configurator,
|
||||||
string name,
|
string name,
|
||||||
Action<IConfigurator<TSettings>> action)
|
Action<IConfigurator<TSettings>> action)
|
||||||
@ -212,7 +214,7 @@ public static class ConfiguratorExtensions
|
|||||||
throw new ArgumentNullException(nameof(configurator));
|
throw new ArgumentNullException(nameof(configurator));
|
||||||
}
|
}
|
||||||
|
|
||||||
configurator.AddBranch(name, action);
|
return configurator.AddBranch(name, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
14
src/Spectre.Console.Cli/IBranchConfigurator.cs
Normal file
14
src/Spectre.Console.Cli/IBranchConfigurator.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
namespace Spectre.Console.Cli;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a branch configurator.
|
||||||
|
/// </summary>
|
||||||
|
public interface IBranchConfigurator
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Adds an alias (an alternative name) to the branch being configured.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name">The alias to add to the branch being configured.</param>
|
||||||
|
/// <returns>The same <see cref="IBranchConfigurator"/> instance so that multiple calls can be chained.</returns>
|
||||||
|
IBranchConfigurator WithAlias(string name);
|
||||||
|
}
|
@ -41,6 +41,7 @@ public interface IConfigurator
|
|||||||
/// <typeparam name="TSettings">The command setting type.</typeparam>
|
/// <typeparam name="TSettings">The command setting type.</typeparam>
|
||||||
/// <param name="name">The name of the command branch.</param>
|
/// <param name="name">The name of the command branch.</param>
|
||||||
/// <param name="action">The command branch configurator.</param>
|
/// <param name="action">The command branch configurator.</param>
|
||||||
void AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
|
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
|
||||||
|
IBranchConfigurator AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
|
||||||
where TSettings : CommandSettings;
|
where TSettings : CommandSettings;
|
||||||
}
|
}
|
@ -51,6 +51,7 @@ public interface IConfigurator<in TSettings>
|
|||||||
/// <typeparam name="TDerivedSettings">The derived command setting type.</typeparam>
|
/// <typeparam name="TDerivedSettings">The derived command setting type.</typeparam>
|
||||||
/// <param name="name">The name of the command branch.</param>
|
/// <param name="name">The name of the command branch.</param>
|
||||||
/// <param name="action">The command branch configuration.</param>
|
/// <param name="action">The command branch configuration.</param>
|
||||||
void AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
|
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
|
||||||
|
IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
|
||||||
where TDerivedSettings : TSettings;
|
where TDerivedSettings : TSettings;
|
||||||
}
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
namespace Spectre.Console.Cli;
|
||||||
|
|
||||||
|
internal sealed class BranchConfigurator : IBranchConfigurator
|
||||||
|
{
|
||||||
|
public ConfiguredCommand Command { get; }
|
||||||
|
|
||||||
|
public BranchConfigurator(ConfiguredCommand command)
|
||||||
|
{
|
||||||
|
Command = command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IBranchConfigurator WithAlias(string alias)
|
||||||
|
{
|
||||||
|
Command.Aliases.Add(alias);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
@ -48,12 +48,13 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
|
|||||||
return new CommandConfigurator(command);
|
return new CommandConfigurator(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
|
public IBranchConfigurator AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
|
||||||
where TSettings : CommandSettings
|
where TSettings : CommandSettings
|
||||||
{
|
{
|
||||||
var command = ConfiguredCommand.FromBranch<TSettings>(name);
|
var command = ConfiguredCommand.FromBranch<TSettings>(name);
|
||||||
action(new Configurator<TSettings>(command, _registrar));
|
action(new Configurator<TSettings>(command, _registrar));
|
||||||
Commands.Add(command);
|
var added = Commands.AddAndReturn(command);
|
||||||
|
return new BranchConfigurator(added);
|
||||||
}
|
}
|
||||||
|
|
||||||
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
|
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
|
||||||
@ -74,7 +75,7 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
|
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
|
||||||
{
|
{
|
||||||
var command = ConfiguredCommand.FromBranch(settings, name);
|
var command = ConfiguredCommand.FromBranch(settings, name);
|
||||||
|
|
||||||
@ -86,6 +87,7 @@ internal sealed class Configurator : IUnsafeConfigurator, IConfigurator, IConfig
|
|||||||
}
|
}
|
||||||
|
|
||||||
action(configurator);
|
action(configurator);
|
||||||
Commands.Add(command);
|
var added = Commands.AddAndReturn(command);
|
||||||
|
return new BranchConfigurator(added);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -47,12 +47,13 @@ internal sealed class Configurator<TSettings> : IUnsafeBranchConfigurator, IConf
|
|||||||
return new CommandConfigurator(command);
|
return new CommandConfigurator(command);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
|
public IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
|
||||||
where TDerivedSettings : TSettings
|
where TDerivedSettings : TSettings
|
||||||
{
|
{
|
||||||
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
|
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
|
||||||
action(new Configurator<TDerivedSettings>(command, _registrar));
|
action(new Configurator<TDerivedSettings>(command, _registrar));
|
||||||
_command.Children.Add(command);
|
var added = _command.Children.AddAndReturn(command);
|
||||||
|
return new BranchConfigurator(added);
|
||||||
}
|
}
|
||||||
|
|
||||||
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
|
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
|
||||||
@ -73,7 +74,7 @@ internal sealed class Configurator<TSettings> : IUnsafeBranchConfigurator, IConf
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
|
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
|
||||||
{
|
{
|
||||||
var command = ConfiguredCommand.FromBranch(settings, name);
|
var command = ConfiguredCommand.FromBranch(settings, name);
|
||||||
|
|
||||||
@ -85,6 +86,7 @@ internal sealed class Configurator<TSettings> : IUnsafeBranchConfigurator, IConf
|
|||||||
}
|
}
|
||||||
|
|
||||||
action(configurator);
|
action(configurator);
|
||||||
_command.Children.Add(command);
|
var added = _command.Children.AddAndReturn(command);
|
||||||
|
return new BranchConfigurator(added);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -19,5 +19,6 @@ public interface IUnsafeConfigurator
|
|||||||
/// <param name="name">The name of the command branch.</param>
|
/// <param name="name">The name of the command branch.</param>
|
||||||
/// <param name="settings">The command setting type.</param>
|
/// <param name="settings">The command setting type.</param>
|
||||||
/// <param name="action">The command branch configurator.</param>
|
/// <param name="action">The command branch configurator.</param>
|
||||||
void AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action);
|
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
|
||||||
|
IBranchConfigurator AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action);
|
||||||
}
|
}
|
@ -280,6 +280,65 @@ public sealed partial class CommandAppTests
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Be_Able_To_Use_Branch_Alias()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var app = new CommandAppTester();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.PropagateExceptions();
|
||||||
|
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||||
|
{
|
||||||
|
animal.AddCommand<DogCommand>("dog");
|
||||||
|
animal.AddCommand<HorseCommand>("horse");
|
||||||
|
}).WithAlias("a");
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = app.Run(new[]
|
||||||
|
{
|
||||||
|
"a", "dog", "12", "--good-boy",
|
||||||
|
"--name", "Rufus",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.ExitCode.ShouldBe(0);
|
||||||
|
result.Settings.ShouldBeOfType<DogSettings>().And(dog =>
|
||||||
|
{
|
||||||
|
dog.Age.ShouldBe(12);
|
||||||
|
dog.GoodBoy.ShouldBe(true);
|
||||||
|
dog.Name.ShouldBe("Rufus");
|
||||||
|
dog.IsAlive.ShouldBe(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Throw_If_Branch_Alias_Conflicts_With_Another_Command()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var app = new CommandApp();
|
||||||
|
app.Configure(config =>
|
||||||
|
{
|
||||||
|
config.PropagateExceptions();
|
||||||
|
config.AddCommand<DogCommand>("a");
|
||||||
|
config.AddBranch<AnimalSettings>("animal", animal =>
|
||||||
|
{
|
||||||
|
animal.AddCommand<DogCommand>("dog");
|
||||||
|
animal.AddCommand<HorseCommand>("horse");
|
||||||
|
}).WithAlias("a");
|
||||||
|
});
|
||||||
|
|
||||||
|
// When
|
||||||
|
var result = Record.Exception(() => app.Run(new[] { "a", "0", "12" }));
|
||||||
|
|
||||||
|
// Then
|
||||||
|
result.ShouldBeOfType<CommandConfigurationException>().And(ex =>
|
||||||
|
{
|
||||||
|
ex.Message.ShouldBe("The alias 'a' for 'animal' conflicts with another command.");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Should_Assign_Default_Value_To_Optional_Argument()
|
public void Should_Assign_Default_Value_To_Optional_Argument()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user