Skip to content

Commit 04610cf

Browse files
authored
Alias for command branches (#411)
1 parent f4183e0 commit 04610cf

File tree

9 files changed

+288
-189
lines changed

9 files changed

+288
-189
lines changed

src/Spectre.Console.Cli/ConfiguratorExtensions.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ public static IConfigurator UseStrictParsing(this IConfigurator configurator)
8888

8989
configurator.Settings.StrictParsing = true;
9090
return configurator;
91-
}
92-
91+
}
92+
9393
/// <summary>
9494
/// Tells the help writer whether or not to trim trailing period.
9595
/// </summary>
96-
/// <param name="configurator">The configurator.</param>
96+
/// <param name="configurator">The configurator.</param>
9797
/// <param name="trimTrailingPeriods">True to trim trailing period (default), false to not.</param>
9898
/// <returns>A configurator that can be used to configure the application further.</returns>
9999
public static IConfigurator TrimTrailingPeriods(this IConfigurator configurator, bool trimTrailingPeriods)
@@ -181,7 +181,8 @@ public static IConfigurator SetInterceptor(this IConfigurator configurator, ICom
181181
/// <param name="configurator">The configurator.</param>
182182
/// <param name="name">The name of the command branch.</param>
183183
/// <param name="action">The command branch configuration.</param>
184-
public static void AddBranch(
184+
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
185+
public static IBranchConfigurator AddBranch(
185186
this IConfigurator configurator,
186187
string name,
187188
Action<IConfigurator<CommandSettings>> action)
@@ -191,7 +192,7 @@ public static void AddBranch(
191192
throw new ArgumentNullException(nameof(configurator));
192193
}
193194

194-
configurator.AddBranch(name, action);
195+
return configurator.AddBranch(name, action);
195196
}
196197

197198
/// <summary>
@@ -201,7 +202,8 @@ public static void AddBranch(
201202
/// <param name="configurator">The configurator.</param>
202203
/// <param name="name">The name of the command branch.</param>
203204
/// <param name="action">The command branch configuration.</param>
204-
public static void AddBranch<TSettings>(
205+
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
206+
public static IBranchConfigurator AddBranch<TSettings>(
205207
this IConfigurator<TSettings> configurator,
206208
string name,
207209
Action<IConfigurator<TSettings>> action)
@@ -212,7 +214,7 @@ public static void AddBranch<TSettings>(
212214
throw new ArgumentNullException(nameof(configurator));
213215
}
214216

215-
configurator.AddBranch(name, action);
217+
return configurator.AddBranch(name, action);
216218
}
217219

218220
/// <summary>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Spectre.Console.Cli;
2+
3+
/// <summary>
4+
/// Represents a branch configurator.
5+
/// </summary>
6+
public interface IBranchConfigurator
7+
{
8+
/// <summary>
9+
/// Adds an alias (an alternative name) to the branch being configured.
10+
/// </summary>
11+
/// <param name="name">The alias to add to the branch being configured.</param>
12+
/// <returns>The same <see cref="IBranchConfigurator"/> instance so that multiple calls can be chained.</returns>
13+
IBranchConfigurator WithAlias(string name);
14+
}

src/Spectre.Console.Cli/IConfigurator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandContext, TS
4141
/// <typeparam name="TSettings">The command setting type.</typeparam>
4242
/// <param name="name">The name of the command branch.</param>
4343
/// <param name="action">The command branch configurator.</param>
44-
void AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
44+
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
45+
IBranchConfigurator AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
4546
where TSettings : CommandSettings;
4647
}

src/Spectre.Console.Cli/IConfiguratorOfT.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandCont
5151
/// <typeparam name="TDerivedSettings">The derived command setting type.</typeparam>
5252
/// <param name="name">The name of the command branch.</param>
5353
/// <param name="action">The command branch configuration.</param>
54-
void AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
54+
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
55+
IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
5556
where TDerivedSettings : TSettings;
5657
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace Spectre.Console.Cli;
2+
3+
internal sealed class BranchConfigurator : IBranchConfigurator
4+
{
5+
public ConfiguredCommand Command { get; }
6+
7+
public BranchConfigurator(ConfiguredCommand command)
8+
{
9+
Command = command;
10+
}
11+
12+
public IBranchConfigurator WithAlias(string alias)
13+
{
14+
Command.Aliases.Add(alias);
15+
return this;
16+
}
17+
}

src/Spectre.Console.Cli/Internal/Configuration/Configurator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandCont
4848
return new CommandConfigurator(command);
4949
}
5050

51-
public void AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
51+
public IBranchConfigurator AddBranch<TSettings>(string name, Action<IConfigurator<TSettings>> action)
5252
where TSettings : CommandSettings
5353
{
5454
var command = ConfiguredCommand.FromBranch<TSettings>(name);
5555
action(new Configurator<TSettings>(command, _registrar));
56-
Commands.Add(command);
56+
var added = Commands.AddAndReturn(command);
57+
return new BranchConfigurator(added);
5758
}
5859

5960
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
@@ -74,7 +75,7 @@ ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
7475
return result;
7576
}
7677

77-
void IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
78+
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
7879
{
7980
var command = ConfiguredCommand.FromBranch(settings, name);
8081

@@ -86,6 +87,7 @@ void IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBra
8687
}
8788

8889
action(configurator);
89-
Commands.Add(command);
90+
var added = Commands.AddAndReturn(command);
91+
return new BranchConfigurator(added);
9092
}
9193
}

src/Spectre.Console.Cli/Internal/Configuration/ConfiguratorOfT.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ public ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<Comm
4747
return new CommandConfigurator(command);
4848
}
4949

50-
public void AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
50+
public IBranchConfigurator AddBranch<TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
5151
where TDerivedSettings : TSettings
5252
{
5353
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
5454
action(new Configurator<TDerivedSettings>(command, _registrar));
55-
_command.Children.Add(command);
55+
var added = _command.Children.AddAndReturn(command);
56+
return new BranchConfigurator(added);
5657
}
5758

5859
ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
@@ -73,7 +74,7 @@ ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
7374
return result;
7475
}
7576

76-
void IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
77+
IBranchConfigurator IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action)
7778
{
7879
var command = ConfiguredCommand.FromBranch(settings, name);
7980

@@ -85,6 +86,7 @@ void IUnsafeConfigurator.AddBranch(string name, Type settings, Action<IUnsafeBra
8586
}
8687

8788
action(configurator);
88-
_command.Children.Add(command);
89+
var added = _command.Children.AddAndReturn(command);
90+
return new BranchConfigurator(added);
8991
}
9092
}

src/Spectre.Console.Cli/Unsafe/IUnsafeConfigurator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public interface IUnsafeConfigurator
1919
/// <param name="name">The name of the command branch.</param>
2020
/// <param name="settings">The command setting type.</param>
2121
/// <param name="action">The command branch configurator.</param>
22-
void AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action);
22+
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
23+
IBranchConfigurator AddBranch(string name, Type settings, Action<IUnsafeBranchConfigurator> action);
2324
}

0 commit comments

Comments
 (0)