From 19ad0bf0cc4d2f5de5943c3358bd21875693d5ed Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Sat, 9 Apr 2011 12:28:49 +1200 Subject: [PATCH] improve util arg parsing --- OpenRA.Utility/Program.cs | 47 ++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/OpenRA.Utility/Program.cs b/OpenRA.Utility/Program.cs index d20fb6eb30..74a160c9ad 100644 --- a/OpenRA.Utility/Program.cs +++ b/OpenRA.Utility/Program.cs @@ -48,26 +48,25 @@ namespace OpenRA.Utility if (args.Length == 0) { PrintUsage(); return; } - bool piping = false; - var i = Array.IndexOf(args, "--pipe"); - if (args.Length > 1 && i >= 0) - { - piping = true; - var pipename = args[i + 1]; + var pipename = GetNamedArg(args, "--pipe"); + var piping = pipename != null; + if (pipename != null) + { + var pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, + PipeTransmissionMode.Byte, PipeOptions.None, PipeBufferSize, PipeBufferSize, + MakePipeSecurity()); - var pipe = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, - PipeTransmissionMode.Byte, PipeOptions.None, PipeBufferSize, PipeBufferSize, - MakePipeSecurity()); + pipe.WaitForConnection(); + Console.SetOut(new StreamWriter(pipe) { AutoFlush = true }); + } + + var supportDir = GetNamedArg(args, "--SupportDir"); + if (supportDir != null) + { + Log.LogPath = supportDir + Path.DirectorySeparatorChar + "Logs" + Path.DirectorySeparatorChar; + Console.WriteLine("LogPath: {0}", Log.LogPath); + } - pipe.WaitForConnection(); - Console.SetOut(new StreamWriter(pipe) { AutoFlush = true }); - } - i = Array.IndexOf(args, "--SupportDir"); - if (args.Length > 1 && i >= 0) - { - Log.LogPath = args[i+1] + Path.DirectorySeparatorChar + "Logs" + Path.DirectorySeparatorChar; - Console.WriteLine("LogPath: {0}", Log.LogPath); - } try { var action = WithDefault( null, () => actions[args[0]]); @@ -107,5 +106,17 @@ namespace OpenRA.Utility try { return f(); } 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]; + } } }