Files
CliFx/CliFx.Demo/Commands/BookCommand.cs
Alexey Golub 3883c831e9 Rework (#36)
2020-01-27 21:10:14 +02:00

34 lines
896 B
C#

using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Demo.Internal;
using CliFx.Demo.Services;
using CliFx.Exceptions;
namespace CliFx.Demo.Commands
{
[Command("book", Description = "View, list, add or remove books.")]
public class BookCommand : ICommand
{
private readonly LibraryService _libraryService;
[CommandParameter(0, Name = "title", Description = "Book title.")]
public string Title { get; set; } = "";
public BookCommand(LibraryService libraryService)
{
_libraryService = libraryService;
}
public ValueTask ExecuteAsync(IConsole console)
{
var book = _libraryService.GetBook(Title);
if (book == null)
throw new CommandException("Book not found.", 1);
console.RenderBook(book);
return default;
}
}
}