using System;
using System.Collections.Generic;
using System.Text;
namespace Wayne.Lib
{
///
/// Argument support for parameters passed to the application's Main method.
/// It handles either flags, such as -a /a or just a.
/// Or it handles arguments on the form key=value.
///
public class AppArgSupport
{
private readonly Dictionary argDict = new Dictionary();
private readonly List flagList = new List();
///
/// Constructor.
///
///
public AppArgSupport(string[] args)
{
foreach (string arg in args)
{
int equalSignIndex = arg.IndexOf('=');
if (equalSignIndex > -1)
argDict[arg.Substring(0, equalSignIndex).ToUpper()] = arg.Substring(equalSignIndex + 1);
else
{
string flagName = arg;
if (!flagList.Exists(s => s.Equals(flagName, StringComparison.CurrentCultureIgnoreCase)))
flagList.Add(arg);
}
}
}
///
/// Checks if the specified flag exists
///
///
///
public bool HasFlag(string flag)
{
return flagList.Exists(s => s.Equals(flag, StringComparison.CurrentCultureIgnoreCase));
}
///
/// Tries to get the specified parameter.
///
/// name of the parameter
/// value of the parameter
/// True if the parameter exists
public bool TryGetArgStringValue(string name, out string value)
{
return argDict.TryGetValue(name.ToUpper(), out value);
}
///
/// Gets the argument value as a string
///
///
///
public string GetArgStringValue(string name)
{
string value;
if (argDict.TryGetValue(name.ToUpper(), out value))
return value;
throw new ArgumentException(string.Concat("Missing argument \"", name, "\""));
}
///
/// Gets the argument value, providing a default value to use in case the parameter is not specified.
///
///
///
///
public string GetArgStringValue(string name, string defaultValue)
{
string value;
if (!argDict.TryGetValue(name.ToUpper(), out value))
value = defaultValue;
return value;
}
///
/// Tries to get an integer argument value.
///
///
///
/// True if success.
public bool TryGetArgIntValue(string name, out int value)
{
try
{
string stringValue;
if (TryGetArgStringValue(name, out stringValue))
{
value = int.Parse(stringValue);
return true;
}
}
catch (Exception) { }
value = 0;
return false;
}
///
/// Gets an integer argument value
///
///
///
/// If value does not exist
/// If value is not integer
public int GetArgIntValue(string name)
{
string stringValue = GetArgStringValue(name);
try
{
return int.Parse(stringValue);
}
catch (Exception)
{
throw new ArgumentException(string.Concat("Bad int-value in argument \"", name, "\": \"", stringValue, "\""));
}
}
///
/// Gets an integer argument value if exists, otherwise returns the default value.
///
///
///
///
public int GetArgIntValue(string name, int defaultValue)
{
try
{
string value = GetArgStringValue(name, defaultValue.ToString());
return int.Parse(value);
}
catch (Exception)
{
return defaultValue;
}
}
///
/// Gets an enumeration argument value.
///
///
///
///
/// True if success.
public bool TryGetArgEnumValue(string name, out TEnum value) where TEnum : struct
{
try
{
string stringValue;
if (TryGetArgStringValue(name, out stringValue))
{
value = (TEnum)Enum.Parse(typeof(TEnum), stringValue, true);
return true;
}
}
catch (Exception) { }
value = default(TEnum);
return false;
}
///
/// Gets an enumeration argument value.
///
///
///
///
/// If value does not exist
/// If value is not one of the enumerated values
public TEnum GetArgEnumValue(string name)
{
string stringValue = GetArgStringValue(name);
try
{
return (TEnum)Enum.Parse(typeof(TEnum), stringValue, true);
}
catch (Exception)
{
throw new ArgumentException(string.Concat("Bad enum-value in argument \"", name, "\": \"", stringValue, "\""));
}
}
///
/// Gets an enumeration argument value if exists, otherwise returns the default value.
///
///
///
///
public TEnum GetArgEnumValue(string name, TEnum defaultValue)
{
try
{
string stringValue = GetArgStringValue(name, defaultValue.ToString());
return EnumSupport.Parse(stringValue, true, defaultValue);
}
catch (Exception)
{
return defaultValue;
}
}
}
}