tmp commit of cli

This commit is contained in:
Tim Van Baak 2024-08-14 08:44:22 -07:00
parent 0ee31a9629
commit 20b7269e4b
3 changed files with 92 additions and 0 deletions

View File

@ -1,5 +1,9 @@
using System.Text.Json;
using CommandLine;
using MultiversalDiplomacy.Model;
namespace MultiversalDiplomacy.CommandLine;
[Verb("adjudicate", HelpText = "Adjudicate a Multiversal Diplomacy game state.")]
@ -10,6 +14,13 @@ public class AdjudicateOptions
public static void Execute(AdjudicateOptions args)
{
Stream input = args.InputFile switch {
null => Console.OpenStandardInput(),
"-" => Console.OpenStandardInput(),
_ => new FileStream(args.InputFile!, FileMode.Open, FileAccess.Read),
};
var state = JsonSerializer.Deserialize<World>(input);
throw new NotImplementedException();
}
}

View File

@ -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);
}

View File

@ -0,0 +1,60 @@
using MultiversalDiplomacy.Model;
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)
{
var args = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (args.Length == 0)
{
return this;
}
var command = args[0];
switch (command)
{
case "help":
case "?":
Console.WriteLine("Commands:");
Console.WriteLine(" help, ?: print this message");
Console.WriteLine(" stab: stab");
Console.WriteLine(" new: start a new game");
break;
case "stab":
Console.WriteLine("stab");
break;
case "new" when args.Length == 1:
Console.WriteLine("Usage:");
Console.WriteLine(" new [map]");
Console.WriteLine("Available maps:");
Console.WriteLine(string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant())));
break;
case "new" when args.Length > 1:
if (!Enum.TryParse<MapType>(args[1], out MapType map)) {
Console.WriteLine($"Unknown map {args[1]}");
Console.WriteLine("Available maps:");
Console.WriteLine(string.Join(", ", Enum.GetNames<MapType>().Select(s => s.ToLowerInvariant())));
break;
}
World world = World.WithMap(Map.FromType(map));
Console.WriteLine($"Created a new {map} game");
return this;
default:
Console.WriteLine($"Unrecognized command: {command}");
break;
}
return this;
}
}