Files
CliFx/CliFx.Demo/Commands/BookCommand.cs
2019-08-14 17:47:05 +03:00

36 lines
948 B
C#

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