Skip to content

Commit

Permalink
Make it possible to implement ICommand<TParameters> directly
Browse files Browse the repository at this point in the history
  • Loading branch information
wjrogers committed Jun 11, 2024
1 parent 4356713 commit 239f292
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
9 changes: 1 addition & 8 deletions XO.Console.Cli/AsyncCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class AsyncCommand : ICommand<CommandParameters>
public abstract Task<int> ExecuteAsync(ICommandContext context, CancellationToken cancellationToken);

/// <inheritdoc/>
Task<int> ICommand.ExecuteAsync(
Task<int> ICommand<CommandParameters>.ExecuteAsync(
ICommandContext context,
CommandParameters _,
CancellationToken cancellationToken)
Expand All @@ -37,11 +37,4 @@ public abstract class AsyncCommand<TParameters> : ICommand<TParameters>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that may request that command execution be canceled.</param>
/// <returns>A <see cref="Task{T}"/> whose result is the exit code of the command.</returns>
public abstract Task<int> ExecuteAsync(ICommandContext context, TParameters parameters, CancellationToken cancellationToken);

/// <inheritdoc/>
Task<int> ICommand.ExecuteAsync(
ICommandContext context,
CommandParameters parameters,
CancellationToken cancellationToken)
=> ExecuteAsync(context, (TParameters)parameters, cancellationToken);
}
8 changes: 4 additions & 4 deletions XO.Console.Cli/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract class Command : ICommand<CommandParameters>
public abstract int Execute(ICommandContext context, CancellationToken cancellationToken);

/// <inheritdoc/>
Task<int> ICommand.ExecuteAsync(
Task<int> ICommand<CommandParameters>.ExecuteAsync(
ICommandContext context,
CommandParameters _,
CancellationToken cancellationToken)
Expand All @@ -42,12 +42,12 @@ public abstract class Command<TParameters> : ICommand<TParameters>
public abstract int Execute(ICommandContext context, TParameters parameters, CancellationToken cancellationToken);

/// <inheritdoc/>
Task<int> ICommand.ExecuteAsync(
Task<int> ICommand<TParameters>.ExecuteAsync(
ICommandContext context,
CommandParameters parameters,
TParameters parameters,
CancellationToken cancellationToken)
{
var result = Execute(context, (TParameters)parameters, cancellationToken);
var result = Execute(context, parameters, cancellationToken);
return Task.FromResult(result);
}
}
29 changes: 25 additions & 4 deletions XO.Console.Cli/ICommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace XO.Console.Cli;
/// Represents a command.
/// </summary>
/// <remarks>
/// This interface cannot be implemented directly. These abstract classes are provided for implementing commands:
/// This interface cannot be implemented directly. You must implement <see cref="ICommand{TParameters}"/> or one of
/// these abstract classes:
/// <list type="bullet">
/// <item><see cref="AsyncCommand"/>: asynchronous command, no parameters</item>
/// <item><see cref="AsyncCommand{TParameters}"/>: asynchronous command with parameters</item>
Expand All @@ -28,15 +29,15 @@ public interface ICommand
/// <returns>A <see cref="Task{T}"/> whose result is the exit code of the command.</returns>
internal Task<int> ExecuteAsync(
ICommandContext context,
CommandParameters parameters,
object parameters,
CancellationToken cancellationToken = default);
}

/// <summary>
/// Represents a command with parameters.
/// </summary>
/// <remarks>
/// This interface cannot be implemented directly. These abstract classes are provided for implementing commands:
/// These abstract classes are provided for implementing commands:
/// <list type="bullet">
/// <item><see cref="AsyncCommand{TParameters}"/>: asynchronous command with parameters</item>
/// <item><see cref="Command{TParameters}"/>: synchronous command with parameters</item>
Expand All @@ -45,4 +46,24 @@ internal Task<int> ExecuteAsync(
/// <typeparam name="TParameters">A class whose properties describe the command parameters.</typeparam>
public interface ICommand<TParameters> : ICommand
where TParameters : CommandParameters
{ }
{
/// <summary>
/// Executes the command.
/// </summary>
/// <param name="context">The command execution context.</param>
/// <param name="parameters">The parameters bound from the command-line arguments.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that may request that command execution be canceled.</param>
/// <returns>A <see cref="Task{T}"/> whose result is the exit code of the command.</returns>
Task<int> ExecuteAsync(
ICommandContext context,
TParameters parameters,
CancellationToken cancellationToken = default);

/// <inheritdoc/>
Task<int> ICommand.ExecuteAsync(
ICommandContext context,
object parameters,
CancellationToken cancellationToken)
=> ExecuteAsync(context, (TParameters)parameters, cancellationToken);
}

0 comments on commit 239f292

Please sign in to comment.