improve util arg parsing

This commit is contained in:
Chris Forbes
2011-04-09 12:28:49 +12:00
parent ba72276b83
commit 19ad0bf0cc

View File

@@ -48,13 +48,10 @@ namespace OpenRA.Utility
if (args.Length == 0) { PrintUsage(); return; } if (args.Length == 0) { PrintUsage(); return; }
bool piping = false; var pipename = GetNamedArg(args, "--pipe");
var i = Array.IndexOf(args, "--pipe"); var piping = pipename != null;
if (args.Length > 1 && i >= 0) if (pipename != null)
{ {
piping = true;
var pipename = args[i + 1];
var pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, var pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1,
PipeTransmissionMode.Byte, PipeOptions.None, PipeBufferSize, PipeBufferSize, PipeTransmissionMode.Byte, PipeOptions.None, PipeBufferSize, PipeBufferSize,
MakePipeSecurity()); MakePipeSecurity());
@@ -62,12 +59,14 @@ namespace OpenRA.Utility
pipe.WaitForConnection(); pipe.WaitForConnection();
Console.SetOut(new StreamWriter(pipe) { AutoFlush = true }); Console.SetOut(new StreamWriter(pipe) { AutoFlush = true });
} }
i = Array.IndexOf(args, "--SupportDir");
if (args.Length > 1 && i >= 0) var supportDir = GetNamedArg(args, "--SupportDir");
if (supportDir != null)
{ {
Log.LogPath = args[i+1] + Path.DirectorySeparatorChar + "Logs" + Path.DirectorySeparatorChar; Log.LogPath = supportDir + Path.DirectorySeparatorChar + "Logs" + Path.DirectorySeparatorChar;
Console.WriteLine("LogPath: {0}", Log.LogPath); Console.WriteLine("LogPath: {0}", Log.LogPath);
} }
try try
{ {
var action = WithDefault( null, () => actions[args[0]]); var action = WithDefault( null, () => actions[args[0]]);
@@ -107,5 +106,17 @@ namespace OpenRA.Utility
try { return f(); } try { return f(); }
catch { return def; } catch { return def; }
} }
static string GetNamedArg(string[] args, string arg)
{
if (args.Length < 2)
return null;
var i = Array.IndexOf(args, arg);
if (i < 0 || i == args.Length - 1) // doesnt exist, or doesnt have a value.
return null;
return args[i + 1];
}
} }
} }