Files
CliFx/CliFx.Demo/Commands/BookCommand.cs

28 lines
751 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(LibraryProvider libraryProvider) : ICommand
{
[CommandParameter(0, Name = "title", Description = "Title of the book to retrieve.")]
public required string Title { get; init; }
public ValueTask ExecuteAsync(IConsole console)
{
var book = libraryProvider.TryGetBook(Title);
if (book is null)
throw new CommandException($"Book '{Title}' not found.", 10);
console.WriteBook(book);
return default;
}
}