From 77c7faa759b721fd44c4a63cb84ebacb548a65cd Mon Sep 17 00:00:00 2001 From: Alexey Golub Date: Wed, 17 Jul 2019 23:06:16 +0300 Subject: [PATCH] Introduce ICommand --- CliFx/Command.cs | 2 +- CliFx/ICommand.cs | 10 ++++++++++ CliFx/Internal/CommandType.cs | 4 +--- CliFx/Services/CommandResolver.cs | 2 +- CliFx/Services/ICommandResolver.cs | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 CliFx/ICommand.cs diff --git a/CliFx/Command.cs b/CliFx/Command.cs index 7442e7f..f8797c3 100644 --- a/CliFx/Command.cs +++ b/CliFx/Command.cs @@ -4,7 +4,7 @@ using CliFx.Models; namespace CliFx { - public abstract class Command + public abstract class Command : ICommand { public virtual ExitCode Execute() => throw new InvalidOperationException( "Can't execute command because its execution method is not defined. " + diff --git a/CliFx/ICommand.cs b/CliFx/ICommand.cs new file mode 100644 index 0000000..8cbaccd --- /dev/null +++ b/CliFx/ICommand.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using CliFx.Models; + +namespace CliFx +{ + public interface ICommand + { + Task ExecuteAsync(); + } +} \ No newline at end of file diff --git a/CliFx/Internal/CommandType.cs b/CliFx/Internal/CommandType.cs index 602b1af..609d710 100644 --- a/CliFx/Internal/CommandType.cs +++ b/CliFx/Internal/CommandType.cs @@ -33,9 +33,7 @@ namespace CliFx.Internal internal partial class CommandType { public static bool IsValid(Type type) => - // Derives from Command - type.IsDerivedFrom(typeof(Command)) && - // Marked with DefaultCommandAttribute or CommandAttribute + type.GetInterfaces().Contains(typeof(ICommand)) && type.IsDefined(typeof(CommandAttribute)); public static CommandType Initialize(Type type) diff --git a/CliFx/Services/CommandResolver.cs b/CliFx/Services/CommandResolver.cs index 2ec7693..029265d 100644 --- a/CliFx/Services/CommandResolver.cs +++ b/CliFx/Services/CommandResolver.cs @@ -74,7 +74,7 @@ namespace CliFx.Services $"Apply {nameof(CommandAttribute)} to give command a name."); } - public Command ResolveCommand(CommandOptionSet optionSet) + public ICommand ResolveCommand(CommandOptionSet optionSet) { // Get command type var commandType = !optionSet.CommandName.IsNullOrWhiteSpace() diff --git a/CliFx/Services/ICommandResolver.cs b/CliFx/Services/ICommandResolver.cs index 1b3b4d0..983b848 100644 --- a/CliFx/Services/ICommandResolver.cs +++ b/CliFx/Services/ICommandResolver.cs @@ -4,6 +4,6 @@ namespace CliFx.Services { public interface ICommandResolver { - Command ResolveCommand(CommandOptionSet optionSet); + ICommand ResolveCommand(CommandOptionSet optionSet); } } \ No newline at end of file