Parsing Command Line Arguments

If you want a full blown Command Line Parser then there are several good options available:

[I used the Command Line Parser Library recently in the SQL Diagnostic Runner I wrote.]

If you just want a very basic parser, supporting simple options in the format  /argname:argvalue  then you could use this:

/// 
/// Very basic Command Line Args extracter
/// Parse command line args for args in the following format:
/// /argname:argvalue /argname:argvalue ...
///

public class CommandLineArgs
{
private const string Pattern = @"\/(?\w+):(?.+)";
private readonly Regex _regex = new Regex(Pattern, RegexOptions.IgnoreCase|RegexOptions.Compiled);
private readonly Dictionary _args = new Dictionary();

public CommandLineArgs()
{
BuildArgDictionary();
}

public string this[string key]
{
get { return _args.ContainsKey(key) ? _args[key] : null; }
}

public bool ContainsKey(string key)
{
return _args.ContainsKey(key);
}

private void BuildArgDictionary()
{
var args = Environment.GetCommandLineArgs();
foreach (var match in args.Select(arg => _regex.Match(arg)).Where(m => m.Success))
{
try
{
_args.Add(match.Groups["argname"].Value, match.Groups["argvalue"].Value);
}
// Ignore any duplicate args
catch (Exception) {}
}
}
}