Added some commands

pull/2/head
Francesco Bertolaccini 2016-01-28 19:22:46 +01:00
parent aa6b3c9d9e
commit 8c52025470
1 changed files with 131 additions and 1 deletions

View File

@ -29,6 +29,100 @@ namespace Test
}
}
static IEnumerable<string> ParseInput(string input)
{
var sb = new StringBuilder();
bool openString = false;
foreach (var c in input)
{
if (char.IsWhiteSpace(c))
{
if (!openString)
{
if (sb.ToString() != "")
{
yield return sb.ToString();
}
sb.Clear();
continue;
}
else
{
sb.Append(c);
}
}
if (c == '"')
{
if (sb.ToString() != "")
{
yield return sb.ToString();
}
sb.Clear();
openString = !openString;
}
else
{
sb.Append(c);
}
}
if (sb.ToString() != "")
{
yield return sb.ToString();
}
}
static Dictionary<string, Action<string[]>> commands = new Dictionary<string, Action<string[]>>();
#region Commands
static void Help(string[] args)
{
Console.WriteLine("Commands:");
foreach (var kvp in commands)
{
Console.WriteLine(" - {0}", kvp.Key);
}
}
static void Mount(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: mount <archive> <mntpoint> <append>");
return;
}
bool append;
if (!bool.TryParse(args[2], out append))
{
Console.WriteLine("append can only be true or false");
}
PhysFS.PhysFS.Mount(args[0], args[1], append);
}
static void Enumerate(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: enumerate/ls <dir>");
return;
}
foreach (var f in PhysFS.PhysFS.EnumerateFiles(args[0]))
{
Console.WriteLine(" - {0}", f);
}
}
static void GetLastError(string[] args)
{
Console.WriteLine(PhysFS.PhysFS.GetLastError());
}
#endregion
static void Main(string[] args)
{
try
@ -50,10 +144,46 @@ namespace Test
PrintSupportedArchives();
Console.WriteLine("Type 'help' for a list of commands");
while(true)
commands.Add("help", Help);
commands.Add("mount", Mount);
commands.Add("enumerate", Enumerate);
commands.Add("ls", Enumerate);
while (true)
{
Console.Write("> ");
var input = Console.ReadLine();
var split = ParseInput(input);
if (split.Count() == 0)
{
continue;
}
if (split.First() == "quit")
{
break;
}
else
{
Action<string[]> cmd;
if (commands.TryGetValue(split.First(), out cmd))
{
try
{
cmd(split.Skip(1).ToArray());
Console.WriteLine("Done.");
}
catch (PhysFS.PhysFSException e)
{
Console.Error.WriteLine("ERROR: {0}", e.Message);
}
}
else
{
Console.Error.WriteLine("Invalid command");
}
}
}
PhysFS.PhysFS.Deinit();