mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Add demo project
This commit is contained in:
18
CliFx.Demo/CliFx.Demo.csproj
Normal file
18
CliFx.Demo/CliFx.Demo.csproj
Normal file
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CliFx\CliFx.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
75
CliFx.Demo/Commands/BookAddCommand.cs
Normal file
75
CliFx.Demo/Commands/BookAddCommand.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Demo.Internal;
|
||||
using CliFx.Demo.Models;
|
||||
using CliFx.Demo.Services;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Services;
|
||||
|
||||
namespace CliFx.Demo.Commands
|
||||
{
|
||||
[Command("book add", Description = "Add a book to the library.")]
|
||||
public partial class BookAddCommand : ICommand
|
||||
{
|
||||
private readonly LibraryService _libraryService;
|
||||
|
||||
[CommandOption("title", 't', IsRequired = true, Description = "Book title.")]
|
||||
public string Title { get; set; }
|
||||
|
||||
[CommandOption("author", 'a', IsRequired = true, Description = "Book author.")]
|
||||
public string Author { get; set; }
|
||||
|
||||
[CommandOption("published", 'p', Description = "Book publish date.")]
|
||||
public DateTimeOffset Published { get; set; }
|
||||
|
||||
[CommandOption("isbn", 'n', Description = "Book ISBN.")]
|
||||
public Isbn Isbn { get; set; }
|
||||
|
||||
public BookAddCommand(LibraryService libraryService)
|
||||
{
|
||||
_libraryService = libraryService;
|
||||
}
|
||||
|
||||
public Task ExecuteAsync(IConsole console)
|
||||
{
|
||||
// To make the demo simpler, we will just generate random publish date and ISBN if they were not set
|
||||
if (Published == default)
|
||||
Published = CreateRandomDate();
|
||||
if (Isbn == default)
|
||||
Isbn = CreateRandomIsbn();
|
||||
|
||||
if (_libraryService.GetBook(Title) != null)
|
||||
throw new CommandErrorException(1, "Book already exists.");
|
||||
|
||||
var book = new Book(Title, Author, Published, Isbn);
|
||||
_libraryService.AddBook(book);
|
||||
|
||||
console.Output.WriteLine("Book added.");
|
||||
console.RenderBook(book);
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class BookAddCommand
|
||||
{
|
||||
private static readonly Random Random = new Random();
|
||||
|
||||
private static DateTimeOffset CreateRandomDate() => new DateTimeOffset(
|
||||
Random.Next(1800, 2020),
|
||||
Random.Next(1, 12),
|
||||
Random.Next(1, 28),
|
||||
Random.Next(1, 23),
|
||||
Random.Next(1, 59),
|
||||
Random.Next(1, 59),
|
||||
TimeSpan.Zero);
|
||||
|
||||
public static Isbn CreateRandomIsbn() => new Isbn(
|
||||
Random.Next(0, 999),
|
||||
Random.Next(0, 99),
|
||||
Random.Next(0, 99999),
|
||||
Random.Next(0, 99),
|
||||
Random.Next(0, 9));
|
||||
}
|
||||
}
|
||||
36
CliFx.Demo/Commands/BookCommand.cs
Normal file
36
CliFx.Demo/Commands/BookCommand.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
39
CliFx.Demo/Commands/BookListCommand.cs
Normal file
39
CliFx.Demo/Commands/BookListCommand.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Demo.Internal;
|
||||
using CliFx.Demo.Services;
|
||||
using CliFx.Services;
|
||||
|
||||
namespace CliFx.Demo.Commands
|
||||
{
|
||||
[Command("book list", Description = "List all books in the library.")]
|
||||
public class BookListCommand : ICommand
|
||||
{
|
||||
private readonly LibraryService _libraryService;
|
||||
|
||||
public BookListCommand(LibraryService libraryService)
|
||||
{
|
||||
_libraryService = libraryService;
|
||||
}
|
||||
|
||||
public Task ExecuteAsync(IConsole console)
|
||||
{
|
||||
var library = _libraryService.GetLibrary();
|
||||
|
||||
var isFirst = true;
|
||||
foreach (var book in library.Books)
|
||||
{
|
||||
// Margin
|
||||
if (!isFirst)
|
||||
console.Output.WriteLine();
|
||||
isFirst = false;
|
||||
|
||||
// Render book
|
||||
console.RenderBook(book);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
37
CliFx.Demo/Commands/BookRemoveCommand.cs
Normal file
37
CliFx.Demo/Commands/BookRemoveCommand.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Attributes;
|
||||
using CliFx.Demo.Services;
|
||||
using CliFx.Exceptions;
|
||||
using CliFx.Services;
|
||||
|
||||
namespace CliFx.Demo.Commands
|
||||
{
|
||||
[Command("book remove", Description = "Remove a book from the library.")]
|
||||
public class BookRemoveCommand : ICommand
|
||||
{
|
||||
private readonly LibraryService _libraryService;
|
||||
|
||||
[CommandOption("title", 't', IsRequired = true, Description = "Book title.")]
|
||||
public string Title { get; set; }
|
||||
|
||||
public BookRemoveCommand(LibraryService libraryService)
|
||||
{
|
||||
_libraryService = libraryService;
|
||||
}
|
||||
|
||||
public Task ExecuteAsync(IConsole console)
|
||||
{
|
||||
var book = _libraryService.GetBook(Title);
|
||||
|
||||
if (book == null)
|
||||
throw new CommandErrorException(1, "Book not found.");
|
||||
|
||||
_libraryService.RemoveBook(book);
|
||||
|
||||
console.Output.WriteLine($"Book {Title} removed.");
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
CliFx.Demo/Internal/ConsoleExtensions.cs
Normal file
30
CliFx.Demo/Internal/ConsoleExtensions.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using CliFx.Demo.Models;
|
||||
using CliFx.Services;
|
||||
|
||||
namespace CliFx.Demo.Internal
|
||||
{
|
||||
internal static class ConsoleExtensions
|
||||
{
|
||||
public static void RenderBook(this IConsole console, Book book)
|
||||
{
|
||||
// Title
|
||||
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Title));
|
||||
|
||||
// Author
|
||||
console.Output.Write(" ");
|
||||
console.Output.Write("Author: ");
|
||||
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Author));
|
||||
|
||||
// Published
|
||||
console.Output.Write(" ");
|
||||
console.Output.Write("Published: ");
|
||||
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine($"{book.Published:d}"));
|
||||
|
||||
// ISBN
|
||||
console.Output.Write(" ");
|
||||
console.Output.Write("ISBN: ");
|
||||
console.WithForegroundColor(ConsoleColor.White, () => console.Output.WriteLine(book.Isbn));
|
||||
}
|
||||
}
|
||||
}
|
||||
23
CliFx.Demo/Models/Book.cs
Normal file
23
CliFx.Demo/Models/Book.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
|
||||
namespace CliFx.Demo.Models
|
||||
{
|
||||
public class Book
|
||||
{
|
||||
public string Title { get; }
|
||||
|
||||
public string Author { get; }
|
||||
|
||||
public DateTimeOffset Published { get; }
|
||||
|
||||
public Isbn Isbn { get; }
|
||||
|
||||
public Book(string title, string author, DateTimeOffset published, Isbn isbn)
|
||||
{
|
||||
Title = title;
|
||||
Author = author;
|
||||
Published = published;
|
||||
Isbn = isbn;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
CliFx.Demo/Models/Extensions.cs
Normal file
22
CliFx.Demo/Models/Extensions.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace CliFx.Demo.Models
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static Library WithBook(this Library library, Book book)
|
||||
{
|
||||
var books = library.Books.ToList();
|
||||
books.Add(book);
|
||||
|
||||
return new Library(books);
|
||||
}
|
||||
|
||||
public static Library WithoutBook(this Library library, Book book)
|
||||
{
|
||||
var books = library.Books.Where(b => b != book).ToArray();
|
||||
|
||||
return new Library(books);
|
||||
}
|
||||
}
|
||||
}
|
||||
44
CliFx.Demo/Models/Isbn.cs
Normal file
44
CliFx.Demo/Models/Isbn.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace CliFx.Demo.Models
|
||||
{
|
||||
public partial class Isbn
|
||||
{
|
||||
public int EanPrefix { get; }
|
||||
|
||||
public int RegistrationGroup { get; }
|
||||
|
||||
public int Registrant { get; }
|
||||
|
||||
public int Publication { get; }
|
||||
|
||||
public int CheckDigit { get; }
|
||||
|
||||
public Isbn(int eanPrefix, int registrationGroup, int registrant, int publication, int checkDigit)
|
||||
{
|
||||
EanPrefix = eanPrefix;
|
||||
RegistrationGroup = registrationGroup;
|
||||
Registrant = registrant;
|
||||
Publication = publication;
|
||||
CheckDigit = checkDigit;
|
||||
}
|
||||
|
||||
public override string ToString() => $"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}";
|
||||
}
|
||||
|
||||
public partial class Isbn
|
||||
{
|
||||
public static Isbn Parse(string value)
|
||||
{
|
||||
var components = value.Split('-', 5, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
return new Isbn(
|
||||
int.Parse(components[0], CultureInfo.InvariantCulture),
|
||||
int.Parse(components[1], CultureInfo.InvariantCulture),
|
||||
int.Parse(components[2], CultureInfo.InvariantCulture),
|
||||
int.Parse(components[3], CultureInfo.InvariantCulture),
|
||||
int.Parse(components[4], CultureInfo.InvariantCulture));
|
||||
}
|
||||
}
|
||||
}
|
||||
20
CliFx.Demo/Models/Library.cs
Normal file
20
CliFx.Demo/Models/Library.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CliFx.Demo.Models
|
||||
{
|
||||
public partial class Library
|
||||
{
|
||||
public IReadOnlyList<Book> Books { get; }
|
||||
|
||||
public Library(IReadOnlyList<Book> books)
|
||||
{
|
||||
Books = books;
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Library
|
||||
{
|
||||
public static Library Empty { get; } = new Library(Array.Empty<Book>());
|
||||
}
|
||||
}
|
||||
33
CliFx.Demo/Program.cs
Normal file
33
CliFx.Demo/Program.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Threading.Tasks;
|
||||
using CliFx.Demo.Commands;
|
||||
using CliFx.Demo.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace CliFx.Demo
|
||||
{
|
||||
public static class Program
|
||||
{
|
||||
public static Task<int> Main(string[] args)
|
||||
{
|
||||
// We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands
|
||||
var services = new ServiceCollection();
|
||||
|
||||
// Register services
|
||||
services.AddSingleton<LibraryService>();
|
||||
|
||||
// Register commands
|
||||
services.AddTransient<BookCommand>();
|
||||
services.AddTransient<BookAddCommand>();
|
||||
services.AddTransient<BookRemoveCommand>();
|
||||
services.AddTransient<BookListCommand>();
|
||||
|
||||
var serviceProvider = new DefaultServiceProviderFactory().CreateServiceProvider(services);
|
||||
|
||||
return new CliApplicationBuilder()
|
||||
.WithCommandsFromThisAssembly()
|
||||
.UseCommandFactory(type => (ICommand) serviceProvider.GetRequiredService(type))
|
||||
.Build()
|
||||
.RunAsync(args);
|
||||
}
|
||||
}
|
||||
}
|
||||
7
CliFx.Demo/Readme.md
Normal file
7
CliFx.Demo/Readme.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# CliFx Demo Project
|
||||
|
||||
Sample command line interface for managing a library of books.
|
||||
|
||||
This demo project shows basic CliFx functionality such as command routing, option parsing, autogenerated help text, and some other things.
|
||||
|
||||
You can get a list of available commands by running `CliFx.Demo --help`.
|
||||
43
CliFx.Demo/Services/LibraryService.cs
Normal file
43
CliFx.Demo/Services/LibraryService.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using CliFx.Demo.Models;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace CliFx.Demo.Services
|
||||
{
|
||||
public class LibraryService
|
||||
{
|
||||
private string StorageFilePath => Path.Combine(Directory.GetCurrentDirectory(), "Data.json");
|
||||
|
||||
private void StoreLibrary(Library library)
|
||||
{
|
||||
var data = JsonConvert.SerializeObject(library);
|
||||
File.WriteAllText(StorageFilePath, data);
|
||||
}
|
||||
|
||||
public Library GetLibrary()
|
||||
{
|
||||
if (!File.Exists(StorageFilePath))
|
||||
return Library.Empty;
|
||||
|
||||
var data = File.ReadAllText(StorageFilePath);
|
||||
|
||||
return JsonConvert.DeserializeObject<Library>(data);
|
||||
}
|
||||
|
||||
public Book GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
|
||||
|
||||
public void AddBook(Book book)
|
||||
{
|
||||
var updatedLibrary = GetLibrary().WithBook(book);
|
||||
StoreLibrary(updatedLibrary);
|
||||
}
|
||||
|
||||
public void RemoveBook(Book book)
|
||||
{
|
||||
var updatedLibrary = GetLibrary().WithoutBook(book);
|
||||
StoreLibrary(updatedLibrary);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user