This commit is contained in:
Alexey Golub
2020-01-27 21:10:14 +02:00
committed by GitHub
parent 63441688fe
commit 3883c831e9
122 changed files with 3472 additions and 4180 deletions

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

View File

@@ -5,7 +5,6 @@ using CliFx.Demo.Internal;
using CliFx.Demo.Models;
using CliFx.Demo.Services;
using CliFx.Exceptions;
using CliFx.Services;
namespace CliFx.Demo.Commands
{
@@ -14,17 +13,17 @@ namespace CliFx.Demo.Commands
{
private readonly LibraryService _libraryService;
[CommandArgument(0, Name = "title", IsRequired = true, Description = "Book title.")]
public string Title { get; set; }
[CommandParameter(0, Name = "title", Description = "Book title.")]
public string Title { get; set; } = "";
[CommandOption("author", 'a', IsRequired = true, Description = "Book author.")]
public string Author { get; set; }
public string Author { get; set; } = "";
[CommandOption("published", 'p', Description = "Book publish date.")]
public DateTimeOffset Published { get; set; }
public DateTimeOffset Published { get; set; } = CreateRandomDate();
[CommandOption("isbn", 'n', Description = "Book ISBN.")]
public Isbn? Isbn { get; set; }
public Isbn Isbn { get; set; } = CreateRandomIsbn();
public BookAddCommand(LibraryService libraryService)
{
@@ -33,12 +32,6 @@ namespace CliFx.Demo.Commands
public ValueTask 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 CommandException("Book already exists.", 1);
@@ -65,7 +58,7 @@ namespace CliFx.Demo.Commands
Random.Next(1, 59),
TimeSpan.Zero);
public static Isbn CreateRandomIsbn() => new Isbn(
private static Isbn CreateRandomIsbn() => new Isbn(
Random.Next(0, 999),
Random.Next(0, 99),
Random.Next(0, 99999),

View File

@@ -3,7 +3,6 @@ using CliFx.Attributes;
using CliFx.Demo.Internal;
using CliFx.Demo.Services;
using CliFx.Exceptions;
using CliFx.Services;
namespace CliFx.Demo.Commands
{
@@ -12,8 +11,8 @@ namespace CliFx.Demo.Commands
{
private readonly LibraryService _libraryService;
[CommandOption("title", 't', IsRequired = true, Description = "Book title.")]
public string Title { get; set; }
[CommandParameter(0, Name = "title", Description = "Book title.")]
public string Title { get; set; } = "";
public BookCommand(LibraryService libraryService)
{

View File

@@ -2,7 +2,6 @@
using CliFx.Attributes;
using CliFx.Demo.Internal;
using CliFx.Demo.Services;
using CliFx.Services;
namespace CliFx.Demo.Commands
{

View File

@@ -2,7 +2,6 @@
using CliFx.Attributes;
using CliFx.Demo.Services;
using CliFx.Exceptions;
using CliFx.Services;
namespace CliFx.Demo.Commands
{
@@ -11,8 +10,8 @@ namespace CliFx.Demo.Commands
{
private readonly LibraryService _libraryService;
[CommandOption("title", 't', IsRequired = true, Description = "Book title.")]
public string Title { get; set; }
[CommandParameter(0, Name = "title", Description = "Book title.")]
public string Title { get; set; } = "";
public BookRemoveCommand(LibraryService libraryService)
{

View File

@@ -1,6 +1,5 @@
using System;
using CliFx.Demo.Models;
using CliFx.Services;
namespace CliFx.Demo.Internal
{

View File

@@ -1,5 +1,4 @@
using System;
using System.Globalization;
namespace CliFx.Demo.Models
{
@@ -24,21 +23,23 @@ namespace CliFx.Demo.Models
CheckDigit = checkDigit;
}
public override string ToString() => $"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}";
public override string ToString() =>
$"{EanPrefix:000}-{RegistrationGroup:00}-{Registrant:00000}-{Publication:00}-{CheckDigit:0}";
}
public partial class Isbn
{
public static Isbn Parse(string value)
public static Isbn Parse(string value, IFormatProvider formatProvider)
{
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));
int.Parse(components[0], formatProvider),
int.Parse(components[1], formatProvider),
int.Parse(components[2], formatProvider),
int.Parse(components[3], formatProvider),
int.Parse(components[4], formatProvider)
);
}
}
}

View File

@@ -8,7 +8,7 @@ namespace CliFx.Demo
{
public static class Program
{
private static IServiceProvider ConfigureServices()
private static IServiceProvider GetServiceProvider()
{
// We use Microsoft.Extensions.DependencyInjection for injecting dependencies in commands
var services = new ServiceCollection();
@@ -25,15 +25,11 @@ namespace CliFx.Demo
return services.BuildServiceProvider();
}
public static async Task<int> Main(string[] args)
{
var serviceProvider = ConfigureServices();
return await new CliApplicationBuilder()
public static async Task<int> Main() =>
await new CliApplicationBuilder()
.AddCommandsFromThisAssembly()
.UseCommandFactory(schema => (ICommand) serviceProvider.GetRequiredService(schema.Type))
.UseTypeActivator(GetServiceProvider().GetService)
.Build()
.RunAsync(args);
}
.RunAsync();
}
}

View File

@@ -2,6 +2,6 @@
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.
This demo project shows basic CliFx functionality such as command routing, argument parsing, autogenerated help text, and some other things.
You can get a list of available commands by running `CliFx.Demo --help`.

View File

@@ -25,7 +25,7 @@ namespace CliFx.Demo.Services
return JsonConvert.DeserializeObject<Library>(data);
}
public Book GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
public Book? GetBook(string title) => GetLibrary().Books.FirstOrDefault(b => b.Title == title);
public void AddBook(Book book)
{