Introduce ICommand

This commit is contained in:
Alexey Golub
2019-07-17 23:06:16 +03:00
parent 4ba9413012
commit 77c7faa759
5 changed files with 14 additions and 6 deletions

View File

@@ -4,7 +4,7 @@ using CliFx.Models;
namespace CliFx namespace CliFx
{ {
public abstract class Command public abstract class Command : ICommand
{ {
public virtual ExitCode Execute() => throw new InvalidOperationException( public virtual ExitCode Execute() => throw new InvalidOperationException(
"Can't execute command because its execution method is not defined. " + "Can't execute command because its execution method is not defined. " +

10
CliFx/ICommand.cs Normal file
View File

@@ -0,0 +1,10 @@
using System.Threading.Tasks;
using CliFx.Models;
namespace CliFx
{
public interface ICommand
{
Task<ExitCode> ExecuteAsync();
}
}

View File

@@ -33,9 +33,7 @@ namespace CliFx.Internal
internal partial class CommandType internal partial class CommandType
{ {
public static bool IsValid(Type type) => public static bool IsValid(Type type) =>
// Derives from Command type.GetInterfaces().Contains(typeof(ICommand)) &&
type.IsDerivedFrom(typeof(Command)) &&
// Marked with DefaultCommandAttribute or CommandAttribute
type.IsDefined(typeof(CommandAttribute)); type.IsDefined(typeof(CommandAttribute));
public static CommandType Initialize(Type type) public static CommandType Initialize(Type type)

View File

@@ -74,7 +74,7 @@ namespace CliFx.Services
$"Apply {nameof(CommandAttribute)} to give command a name."); $"Apply {nameof(CommandAttribute)} to give command a name.");
} }
public Command ResolveCommand(CommandOptionSet optionSet) public ICommand ResolveCommand(CommandOptionSet optionSet)
{ {
// Get command type // Get command type
var commandType = !optionSet.CommandName.IsNullOrWhiteSpace() var commandType = !optionSet.CommandName.IsNullOrWhiteSpace()

View File

@@ -4,6 +4,6 @@ namespace CliFx.Services
{ {
public interface ICommandResolver public interface ICommandResolver
{ {
Command ResolveCommand(CommandOptionSet optionSet); ICommand ResolveCommand(CommandOptionSet optionSet);
} }
} }