|
using System;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
/// <summary>
/// Arguments class
/// </summary>
public class Arguments
{
// Variables
private StringDictionary Parameters;
// Constructor
public Arguments(string[] args)
{
Parameters = new StringDictionary();
Regex spliter = new Regex(@"^-{1,2}|^/|=|:",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
Regex remover = new Regex(@"^['""]?(.*?)['""]?$",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
string Parameter = null;
string[] Parts;
// Valid parameters forms:
// {-,/,--}param{ ,=,:}((",')value(",'))
// Examples:
// -param1 value1 --param2 /param3:"Test-:-work"
// /param4=happy -param5 '--=nice=--'
foreach (string Txt in args)
{
// Look for new parameters (-,/ or --) and a
// possible enclosed value (=,:)
Parts = spliter.Split(Txt, 3);
switch (Parts.Length)
{
// Found a value (for the last parameter
// found (space separator))
case 1:
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
{
Parts[0] = remover.Replace(Parts[0], "$1");
Parameters.Add(Parameter, Parts[0]);
}
Parameter = null;
}
// else Error: no parameter waiting for a value (skipped)
break;
// Found just a parameter
case 2:
// The last parameter is still waiting.
// With no value, set it to true.
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
Parameter = Parts[1];
break;
// Parameter with enclosed value
case 3:
// The last parameter is still waiting.
// With no value, set it to true.
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
Parameter = Parts[1];
// Remove possible enclosing characters (",')
if (!Parameters.ContainsKey(Parameter))
{
Parts[2] = remover.Replace(Parts[2], "$1");
Parameters.Add(Parameter, Parts[2]);
}
Parameter = null;
break;
}
}
// In case a parameter is still waiting
if (Parameter != null)
{
if (!Parameters.ContainsKey(Parameter))
Parameters.Add(Parameter, "true");
}
}
/// <summary>
/// Get paramater value by parameter key.
/// </summary>
/// <param name="param">The paramter whose value to get.</param>
/// <returns>Parameter value.</returns>
/// <remarks>
/// Overriding C# indexer property.
/// </remarks>
public string this[string key]
{
get { return (Parameters[key]); }
}
/// <summary>
/// Get number of paramters.
/// </summary>
public int Count
{
get { return Parameters.Count; }
}
/// <summary>
/// Gets a collection of parameter keys.
/// </summary>
public string[] Keys
{
get
{
string[] keys = new string[Parameters.Keys.Count];
Parameters.Keys.CopyTo(keys, 0);
return keys;
}
}
/// <summary>
/// Determines if arguments contains specific key.
/// </summary>
/// <param name="key">The parameter key to locate.</param>
/// <returns>True if parameter exists otherwise false.</returns>
public bool ContainsKey(string key)
{
return Parameters.ContainsKey(key);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.