Sunday, February 24, 2013

 

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:

/// <summary>
/// Very basic Command Line Args extracter
/// <para>Parse command line args for args in the following format:</para>
/// <para> /argname:argvalue /argname:argvalue ...</para>
/// </summary>
public class CommandLineArgs
{
private const string Pattern = @"\/(?<argname>\w+):(?<argvalue>.+)";
private readonly Regex _regex = new Regex(Pattern, RegexOptions.IgnoreCase|RegexOptions.Compiled);
private readonly Dictionary<String, String> _args = new Dictionary<String, String>();

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) {}
}
}
}


    

Powered by Blogger