Skip to content

Commit

Permalink
Alias for command branches (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyahryapko authored Feb 9, 2023
1 parent f4183e0 commit 04610cf
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 189 deletions.
16 changes: 9 additions & 7 deletions src/Spectre.Console.Cli/ConfiguratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ public static IConfigurator UseStrictParsing(this IConfigurator configurator)

configurator.Settings.StrictParsing = true;
return configurator;
}

}

/// <summary>
/// Tells the help writer whether or not to trim trailing period.
/// </summary>
/// <param name="configurator">The configurator.</param>
/// <param name="configurator">The configurator.</param>
/// <param name="trimTrailingPeriods">True to trim trailing period (default), false to not.</param>
/// <returns>A configurator that can be used to configure the application further.</returns>
public static IConfigurator TrimTrailingPeriods(this IConfigurator configurator, bool trimTrailingPeriods)
Expand Down Expand Up @@ -181,7 +181,8 @@ public static IConfigurator SetInterceptor(this IConfigurator configurator, ICom
/// <param name="configurator">The configurator.</param>
/// <param name="name">The name of the command branch.</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,
string name,
Action<IConfigurator<CommandSettings>> action)
Expand All @@ -191,7 +192,7 @@ public static void AddBranch(
throw new ArgumentNullException(nameof(configurator));
}

configurator.AddBranch(name, action);
return configurator.AddBranch(name, action);
}

/// <summary>
Expand All @@ -201,7 +202,8 @@ public static void AddBranch(
/// <param name="configurator">The configurator.</param>
/// <param name="name">The name of the command branch.</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,
string name,
Action<IConfigurator<TSettings>> action)
Expand All @@ -212,7 +214,7 @@ public static void AddBranch<TSettings>(
throw new ArgumentNullException(nameof(configurator));
}

configurator.AddBranch(name, action);
return configurator.AddBranch(name, action);
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions src/Spectre.Console.Cli/IBranchConfigurator.cs
Original file line number Diff line number Diff line change
@@ -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);
}
3 changes: 2 additions & 1 deletion src/Spectre.Console.Cli/IConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandContext, TS
/// <typeparam name="TSettings">The command setting type.</typeparam>
/// <param name="name">The name of the command branch.</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;
}
3 changes: 2 additions & 1 deletion src/Spectre.Console.Cli/IConfiguratorOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<CommandCont
/// <typeparam name="TDerivedSettings">The derived command setting type.</typeparam>
/// <param name="name">The name of the command branch.</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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
10 changes: 6 additions & 4 deletions src/Spectre.Console.Cli/Internal/Configuration/Configurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ public ICommandConfigurator AddDelegate<TSettings>(string name, Func<CommandCont
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
{
var command = ConfiguredCommand.FromBranch<TSettings>(name);
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)
Expand All @@ -74,7 +75,7 @@ ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
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);

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

action(configurator);
Commands.Add(command);
var added = Commands.AddAndReturn(command);
return new BranchConfigurator(added);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ public ICommandConfigurator AddDelegate<TDerivedSettings>(string name, Func<Comm
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
{
var command = ConfiguredCommand.FromBranch<TDerivedSettings>(name);
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)
Expand All @@ -73,7 +74,7 @@ ICommandConfigurator IUnsafeConfigurator.AddCommand(string name, Type command)
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);

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

action(configurator);
_command.Children.Add(command);
var added = _command.Children.AddAndReturn(command);
return new BranchConfigurator(added);
}
}
3 changes: 2 additions & 1 deletion src/Spectre.Console.Cli/Unsafe/IUnsafeConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public interface IUnsafeConfigurator
/// <param name="name">The name of the command branch.</param>
/// <param name="settings">The command setting type.</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);
}
Loading

0 comments on commit 04610cf

Please sign in to comment.