Add a repl script handling framework
This commit is contained in:
parent
6bb6c0695f
commit
7ad6f3a3d3
|
@ -1,12 +1,84 @@
|
||||||
using CommandLine;
|
using CommandLine;
|
||||||
|
|
||||||
|
using MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
namespace MultiversalDiplomacy.CommandLine;
|
namespace MultiversalDiplomacy.CommandLine;
|
||||||
|
|
||||||
[Verb("repl", HelpText = "Begin an interactive 5dplomacy session.")]
|
[Verb("repl", HelpText = "Begin an interactive 5dplomacy session.")]
|
||||||
public class ReplOptions
|
public class ReplOptions
|
||||||
{
|
{
|
||||||
|
[Option('i', "input", HelpText = "Begin the repl session by executing the commands in this file.")]
|
||||||
|
public string? InputFile { get; set; }
|
||||||
|
|
||||||
|
[Option('o', "output", HelpText = "Echo the repl session to this file. Specify a directory to autogenerate a filename.")]
|
||||||
|
public string? OutputFile { get; set; }
|
||||||
|
|
||||||
public static void Execute(ReplOptions args)
|
public static void Execute(ReplOptions args)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
IEnumerable<string>? inputFileLines = null;
|
||||||
|
if (args.InputFile is not null) {
|
||||||
|
var fullPath = Path.GetFullPath(args.InputFile);
|
||||||
|
inputFileLines = File.ReadAllLines(fullPath);
|
||||||
|
Console.WriteLine($"Reading from {fullPath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a writer to the output file, if specified.
|
||||||
|
StreamWriter? outputWriter = null;
|
||||||
|
if (args.OutputFile is not null)
|
||||||
|
{
|
||||||
|
string fullPath = Path.GetFullPath(args.OutputFile);
|
||||||
|
string outputPath = Directory.Exists(fullPath)
|
||||||
|
? Path.Combine(fullPath, $"{DateTime.UtcNow:yyyyMMddHHmmss}.log")
|
||||||
|
: fullPath;
|
||||||
|
Console.WriteLine($"Echoing to {outputPath}");
|
||||||
|
outputWriter = File.CreateText(outputPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerable<string?> GetInputs()
|
||||||
|
{
|
||||||
|
foreach (string line in inputFileLines ?? [])
|
||||||
|
{
|
||||||
|
var trimmed = line.Trim();
|
||||||
|
// File inputs weren't echoed to the terminal so they need to be echoed here
|
||||||
|
Console.WriteLine($"{trimmed}");
|
||||||
|
yield return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? input;
|
||||||
|
do
|
||||||
|
{
|
||||||
|
input = Console.ReadLine();
|
||||||
|
yield return input;
|
||||||
|
}
|
||||||
|
while (input is not null);
|
||||||
|
// The last null is returned because an EOF means we should quit the repl.
|
||||||
|
}
|
||||||
|
|
||||||
|
IScriptHandler? handler = new ReplScriptHandler();
|
||||||
|
|
||||||
|
Console.Write(handler.Prompt);
|
||||||
|
foreach (string? nextInput in GetInputs())
|
||||||
|
{
|
||||||
|
// Handle quitting directly.
|
||||||
|
if (nextInput is null || nextInput == "quit" || nextInput == "exit")
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
string input = nextInput.Trim();
|
||||||
|
outputWriter?.WriteLine(input);
|
||||||
|
outputWriter?.Flush();
|
||||||
|
|
||||||
|
// Delegate all other command parsing to the handler.
|
||||||
|
handler = handler.HandleInput(input);
|
||||||
|
|
||||||
|
// Quit if the handler ends processing, otherwise prompt for the next command.
|
||||||
|
if (handler is null)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Console.Write(handler.Prompt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("exiting");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
namespace MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A handler that interprets and executes 5dp script commands. Script handlers may create additional script handlers
|
||||||
|
/// and delegate handling to them, allowing a sort of recursive parsing of script commands.
|
||||||
|
/// </summary>
|
||||||
|
public interface IScriptHandler
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// When used interactively, the prompt that should be displayed.
|
||||||
|
/// </summary>
|
||||||
|
public string Prompt { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Process a line of input.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>
|
||||||
|
/// The handler that should handle the next line of input, or null if script handling should end.
|
||||||
|
/// </returns>
|
||||||
|
public IScriptHandler? HandleInput(string input);
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
namespace MultiversalDiplomacy.Script;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// A script handler for the interactive repl.
|
||||||
|
/// </summary>
|
||||||
|
public class ReplScriptHandler : IScriptHandler
|
||||||
|
{
|
||||||
|
public string Prompt => "5dp> ";
|
||||||
|
|
||||||
|
public IScriptHandler? HandleInput(string input)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[{input}]");
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue