Files
CliFx/CliFx.Demo/Commands/BookCommand.cs
2023-08-22 21:20:04 +03:00

35 lines
884 B
C#

using System.Threading.Tasks;
using CliFx.Attributes;
using CliFx.Demo.Domain;
using CliFx.Demo.Utils;
using CliFx.Exceptions;
using CliFx.Infrastructure;
namespace CliFx.Demo.Commands;
[Command("book", Description = "Retrieves a book from the library.")]
public class BookCommand : ICommand
{
private readonly LibraryProvider _libraryProvider;
[CommandParameter(0, Name = "title", Description = "Title of the book to retrieve.")]
public required string Title { get; init; }
public BookCommand(LibraryProvider libraryProvider)
{
_libraryProvider = libraryProvider;
}
public ValueTask ExecuteAsync(IConsole console)
{
var book = _libraryProvider.TryGetBook(Title);
if (book is null)
throw new CommandException("Book not found.", 10);
console.Output.WriteBook(book);
return default;
}
}