Created
May 8, 2013 03:02
-
-
Save Konard/5537895 to your computer and use it in GitHub Desktop.
ConsoleHelpers is a class that contains utility methods for making development of console applications easier.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
namespace Konard.Helpers | |
{ | |
public static class ConsoleHelpers | |
{ | |
public static string ExtractOptionParameter(string[] args, string optionName, bool parameterIsMandatory) | |
{ | |
int optionIndex = Array.IndexOf(args, "-" + optionName); | |
if (optionIndex >= 0) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
int firstArgIndex = optionIndex + 1; | |
for (int i = firstArgIndex; i < args.Length; i++) | |
{ | |
if (args[i].StartsWith("-")) | |
break; | |
else | |
{ | |
if (!string.IsNullOrWhiteSpace(args[i])) | |
{ | |
if (i == firstArgIndex) | |
sb.Append(args[i]); | |
else | |
{ | |
sb.Append(' '); | |
sb.Append(args[i]); | |
} | |
} | |
} | |
} | |
string optionParameter = sb.ToString(); | |
if (parameterIsMandatory && string.IsNullOrWhiteSpace(optionParameter)) | |
{ | |
string error = string.Format("После опции '{0}' должен следовать параметр.", optionName); | |
throw new ArgumentException(error); | |
} | |
else | |
{ | |
return optionParameter; | |
} | |
} | |
else | |
{ | |
return String.Empty; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment