Last active
December 12, 2015 04:28
-
-
Save xcud/4714259 to your computer and use it in GitHub Desktop.
Simple string[] args => Dictionary<string, object> parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Collections; | |
namespace Xcud | |
{ | |
/// <summary> | |
/// Simple command line argument parser. | |
/// E.g. | |
/// ParsedArgs parsed = new ParsedArgs(args); | |
/// | |
/// if (parsed.ContainsKey("PerformanceCounters", true)) // case-insensitive | |
/// AddPerfCounter(); | |
/// | |
/// string target = parsed.GetValue("Target", true) as string; | |
/// | |
/// bool isVerbose = (bool)parsed.GetValue("Verbose", true); | |
/// | |
/// </summary> | |
public class ParsedArgs : Dictionary<string, object> | |
{ | |
public ParsedArgs(string[] args) | |
{ | |
args.ToList().ForEach(x => | |
{ | |
int dpos = x.IndexOf(":"); | |
if (dpos > -1) | |
base[x.Substring(1, dpos - 1)] = x.Substring(dpos + 1); | |
else | |
base[x.Substring(1)] = true; | |
}); | |
} | |
public object GetValue(string key, bool ignoreCase) | |
{ | |
return this.FirstOrDefault(a => string.Compare(a.Key, key, ignoreCase) == 0).Value; | |
} | |
public bool ContainsKey(string key, bool ignoreCase) | |
{ | |
return this.Keys.Contains(key, ignoreCase | |
? StringComparer.InvariantCultureIgnoreCase : StringComparer.InvariantCulture); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment