Skip to content

Instantly share code, notes, and snippets.

@Konard
Created May 8, 2013 03:02
Show Gist options
  • Save Konard/5537895 to your computer and use it in GitHub Desktop.
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.
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