Skip to content

Commit

Permalink
More comments
Browse files Browse the repository at this point in the history
  • Loading branch information
EnoughTea committed Jul 4, 2020
1 parent 85b49ed commit 8a5a33a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
34 changes: 21 additions & 13 deletions ConsoleBackEnd.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,51 @@ internal class Program
{
private static void Main(string[] args)
{
// Create instance containing several commands which call ExchangeRates API synchronously.
// For this demo I've written ExchangeRatesApi class, which provides several methods
// representing requests to https://exchangeratesapi.io/. These return various forex currency rates.
// So let's allow user to call them from console via text input,
// passing function parameters as JSON objects:

// First, create instance containing several commands which call ExchangeRates API synchronously.
var exchangeRatesApi = new ExchangeRatesApi();

// Slightly customize 'command result to string' converter,
// since ExchangeRates returns ISO dates without time part.
var returnConverter = new CommandReturnedObjectJsonConverter(true, Formatting.Indented,
// since exchangeratesapi.io returns ISO dates without time part.
var commandReturnsConverter = new CommandReturnedObjectJsonConverter(true, Formatting.Indented,
new JsonSerializerSettings { DateFormatString = "yyyy'-'MM'-'dd" });

// Create command manager.
// Create command manager. It will contain a set of commands, parse given user input into a proper command,
// convert its parameters from a string to typed objects, execute the command with converted parameters
// and return a result.
var consoleCommands = new ConsoleCommands();

// Methods marked with [CommandExecutable] will be added from the given instance:
// Methods marked with [CommandExecutable] will be added from the given exchangeRatesApi instance:
consoleCommands.RegisterAllFromInstance(exchangeRatesApi);

// Example of manually created commands:
// Example of the manually created commands:
var help = new Func<string>(() => {
Console.WriteLine($"Available commands:{Environment.NewLine}");
return consoleCommands.ToString();
});
consoleCommands.Register(new ConsoleCommand(help.Target!, help.Method, "Help",
"Prints available commands.", new[] { "help", "commands" }));
consoleCommands.Register(new ConsoleCommand(help.Target!, help.Method, "help",
"Prints available commands.", new[] { "commands" }));

var clearScreen = new Action(Console.Clear);
consoleCommands.Register(new ConsoleCommand(clearScreen.Target!, clearScreen.Method, "ClearScreen",
consoleCommands.Register(new ConsoleCommand(clearScreen.Target!, clearScreen.Method, "clear",
"Clears console.", new[] { "cls" }));

var quit = new Action(() => Environment.Exit(0));
consoleCommands.Register(new ConsoleCommand(quit.Target!, quit.Method, "Quit",
consoleCommands.Register(new ConsoleCommand(quit.Target!, quit.Method, "quit",
"Quits the program.", new[] { "q" }));

Console.WriteLine("Input \"help\" without quotes to see available commands.");
Console.WriteLine("Input \"help\" without quotes to see available commands. Pass command parameters as " +
"JSON, for example: d(\"2019-12-31\", \"USD\", [\"EUR\", \"RUB\"])");

// Simple REPL:
// Continue to execute user imput until the 'quit' command:
while (true) {
Console.Write("Command prompt: ");
string commandRepr = Console.ReadLine();
string evaluated = consoleCommands.Execute(commandRepr).ConvertOrError(returnConverter);
string evaluated = consoleCommands.Execute(commandRepr).ConvertOrError(commandReturnsConverter);
Console.WriteLine(evaluated);
}
}
Expand Down
3 changes: 3 additions & 0 deletions ConsoleBackEnd/Command/IConsoleCommandParser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace ConsoleBackEnd
{
/// <summary>
/// Interface for classes able to parse user input into a registered command with parameters array.
/// </summary>
public interface IConsoleCommandParser
{
IConsoleCommandParseResult Parse(string commandRepr);
Expand Down

0 comments on commit 8a5a33a

Please sign in to comment.