Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

34 help command #38

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion BusyList/Commands/Commands.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace BusyList.Commands
using System;

namespace BusyList.Commands
{
public record Command;

Expand All @@ -7,4 +9,6 @@ public record NextCommand() : Command;
public record AddCommand(string Description) : Command;
public record DeleteCommand(int Id) : Command;
public record DoneCommand(int Id) : Command;
public record HelpCommand(string name = null) : Command;
}

4 changes: 3 additions & 1 deletion BusyList/Handlers/AddHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using BusyList.Commands;
using BusyList.HelpSystem;
using System;


namespace BusyList.Handlers
{
[HelpAttribute("add", "Add a new task with the passed description", "add [Description]")]
public class AddHandler : IHandler<AddCommand>
{
private readonly ITaskRepository _taskRepository;
Expand All @@ -12,6 +14,7 @@ public AddHandler(ITaskRepository taskRepository)
{
_taskRepository = taskRepository;
}

public void Run(AddCommand command)
{
var item = new AddTaskData(command.Description);
Expand All @@ -20,6 +23,5 @@ public void Run(AddCommand command)

Console.WriteLine($"Added {currentTask.Print()}");
}

}
}
6 changes: 4 additions & 2 deletions BusyList/Handlers/DeleteHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using BusyList.Commands;
using BusyList.HelpSystem;
using System;

namespace BusyList.Handlers
{
[HelpAttribute("delete", "Delete the task with the passed id", "[Id] delete")]
public class DeleteHandler : IHandler<DeleteCommand>
{
private readonly ITaskRepository _taskRepository;
Expand All @@ -11,12 +13,12 @@ public DeleteHandler(ITaskRepository taskRepository)
{
_taskRepository = taskRepository;
}

public void Run(DeleteCommand command)
{
var selectedTask = _taskRepository.GetTaskById(command.Id);
_taskRepository.DeleteTask(selectedTask);
Console.WriteLine($"Task with id {command.Id} has been deleted.");
}
}
}
}
2 changes: 2 additions & 0 deletions BusyList/Handlers/DoneHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using BusyList.Commands;
using BusyList.HelpSystem;
using System;

namespace BusyList.Handlers
{
[HelpAttribute("done", "Mark the task with the given id as done", "[Id] done")]
public class DoneHandler : IHandler<DoneCommand>
{
private readonly ITaskRepository _taskRepository;
Expand Down
51 changes: 51 additions & 0 deletions BusyList/Handlers/HelpHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using BusyList.Commands;
using BusyList.HelpSystem;
using System;

namespace BusyList.Handlers
{
public class HelpHandler : IHandler<HelpCommand>
{
private readonly HelpProvider _helpProvider;

public HelpHandler(HelpProvider helpProvider)
{
_helpProvider = helpProvider;
}

public void Help()
pobiega marked this conversation as resolved.
Show resolved Hide resolved
{
var helpTexts = _helpProvider.GetAllHelpText();

foreach (var helpText in helpTexts)
pobiega marked this conversation as resolved.
Show resolved Hide resolved
{
Console.WriteLine($"{helpText.Item1}: {helpText.Item2}");
}

Console.WriteLine("For further information type help [keyword] e.g help add");
}

public void Run(HelpCommand command)
{
if (command.name == null)
{
Help();

return;
}

var (description, syntax) = _helpProvider.GetHelpText(command.name);
unbekanntunity marked this conversation as resolved.
Show resolved Hide resolved

if (description != null && syntax != null)
{
Console.WriteLine($"Command: {command.name}");
Console.WriteLine($"Description: {description}");
Console.WriteLine($"Syntax: {syntax}");
}
else
{
Console.WriteLine($"Help for the command '{command.name}' does not exists");
}
}
}
}
4 changes: 2 additions & 2 deletions BusyList/Handlers/IHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace BusyList.Handlers
{
internal interface IHandler<in TCommand> where TCommand : Command
public interface IHandler<in TCommand> where TCommand : Command
{
void Run(TCommand command);
}
}
}
5 changes: 4 additions & 1 deletion BusyList/Handlers/NextHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using BusyList.Commands;
using BusyList.HelpSystem;
using System;

namespace BusyList.Handlers
{

[HelpAttribute("next", "Lists all tasks", "next")]
public class NextHandler : IHandler<NextCommand>
{
private readonly ITaskRepository _taskRepository;
Expand Down Expand Up @@ -30,4 +33,4 @@ public void Run(NextCommand command)
}
}
}
}
}
4 changes: 3 additions & 1 deletion BusyList/Handlers/ReadHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using BusyList.Commands;
using BusyList.HelpSystem;
using System;

namespace BusyList.Handlers
{
[HelpAttribute("read", "Print the id, description and status of the task with the passed id", "[Id]")]
public class ReadHandler : IHandler<ReadCommand>
{
private readonly ITaskRepository _taskRepository;
Expand All @@ -25,4 +27,4 @@ public void Run(ReadCommand command)
Console.WriteLine(task.Print());
}
}
}
}
19 changes: 19 additions & 0 deletions BusyList/HelpSystem/HelpAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;

namespace BusyList.HelpSystem
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class HelpAttribute : Attribute
{
public string Name { get; }
public string Description { get; }
public string Syntax { get; }

public HelpAttribute(string name, string description, string syntax)
{
Name = name;
Description = description;
Syntax = syntax;
}
}
}
31 changes: 31 additions & 0 deletions BusyList/HelpSystem/HelpProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace BusyList.HelpSystem
{
public class HelpProvider
{
private readonly Dictionary<string, (string, string)> _helpText = new();

public HelpProvider()
{
_helpText = Assembly.GetExecutingAssembly().GetExportedTypes()
.Where(x => x.IsDefined(typeof(HelpAttribute)))
.Select(x => x.GetCustomAttribute<HelpAttribute>())
.ToDictionary(x => x.Name.ToLowerInvariant(), x => (x.Description, x.Syntax));
}

public (string, string)[] GetAllHelpText() => _helpText.Select(x => (x.Key, x.Value.Item1)).ToArray();

public (string, string) GetHelpText(string key)
{
if(_helpText.TryGetValue(key, out var value))
{
return value;
}

return (null, null);
}
}
}
16 changes: 16 additions & 0 deletions BusyList/Parsing/CommandGrammar.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using BusyList.Commands;
using Sprache;
using System;
using System.Linq;

namespace BusyList.Parsing
Expand All @@ -9,6 +10,9 @@ public static class CommandGrammar
private static readonly Parser<string> _keywordAdd =
Parse.IgnoreCase("add").Text();

private static readonly Parser<string> _keywordHelp =
Parse.IgnoreCase("help").Text();

private static readonly Parser<string> _keywordDelete =
Parse.IgnoreCase("delete")
.Or(Parse.IgnoreCase("del"))
Expand Down Expand Up @@ -49,12 +53,24 @@ from _ in Parse.WhiteSpace
from keyword in _keywordDone
select new DoneCommand(id);

private static readonly Parser<Command> _helpCommand =
from keyword in _keywordHelp
select new HelpCommand();

private static readonly Parser<Command> _helpCommandSpecific =
from keyword in _keywordHelp
from _ in Parse.WhiteSpace
from name in Parse.AnyChar.AtLeastOnce().Text()
select new HelpCommand(name);

public static readonly Parser<Command> Source =
_deleteCommand
.Or(_nextCommand)
.Or(_doneCommand)
.Or(_readCommand)
.Or(_addCommand)
.Or(_helpCommandSpecific)
.Or(_helpCommand)
.End();
}
}
7 changes: 7 additions & 0 deletions BusyList/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using BusyList.Commands;
using BusyList.Handlers;
using BusyList.HelpSystem;
using BusyList.Parsing;
using Microsoft.Extensions.DependencyInjection;
using Sprache;
using System;
using System.Reflection;

namespace BusyList
{
Expand Down Expand Up @@ -65,6 +67,8 @@ private static IServiceCollection ConfigureServices()
services.AddTransient<IHandler<DoneCommand>, DoneHandler>();
services.AddTransient<IHandler<NextCommand>, NextHandler>();
services.AddTransient<IHandler<ReadCommand>, ReadHandler>();
services.AddTransient<IHandler<HelpCommand>, HelpHandler>();
services.AddSingleton<HelpProvider>();

return services;
}
Expand All @@ -88,6 +92,9 @@ private static void HandleCommand(ServiceProvider provider, Command command)
case ReadCommand read:
provider.GetRequiredService<IHandler<ReadCommand>>().Run(read);
break;
case HelpCommand help:
provider.GetRequiredService<IHandler<HelpCommand>>().Run(help);
break;
default:
throw new Exception($"Unknown command type {command.GetType().FullName} sent to HandleCommand!");
}
Expand Down