{"id":2334,"date":"2013-12-02T11:46:41","date_gmt":"2013-12-02T18:46:41","guid":{"rendered":"http:\/\/www.sheer.us\/weblogs\/?p=2334"},"modified":"2013-12-02T11:47:32","modified_gmt":"2013-12-02T18:47:32","slug":"c-arg-parser","status":"publish","type":"post","link":"http:\/\/www.sheer.us\/weblogs\/uncategorized\/c-arg-parser","title":{"rendered":"C# arg parser"},"content":{"rendered":"<p>Since I posted a perl one. Note there&#8217;s probably some neat way to do this using system libraries that I just don&#8217;t know about, but this is what I use<\/p>\n<p>As part of the class (you can have this as a class i.e. Config.cs, or as part of the program:<\/p>\n<p><PRE><br \/>\n  static readonly Dictionary<string, string> argsDict = new Dictionary<string, string>();<br \/>\n<\/PRE><\/p>\n<p>Parser:<\/p>\n<p><PRE><br \/>\n static public void ArgHandler(string[] args)<br \/>\n        {<br \/>\n            foreach (string arg in args)<br \/>\n            {<br \/>\n                Regex quoteRE = new Regex(&#8220;\\&#8221;(.*)\\&#8221;&#8221;);<br \/>\n                string[] quoteResults = quoteRE.Split(arg);<br \/>\n                string workarg;<br \/>\n                if (quoteResults.Length > 1)<br \/>\n                {<br \/>\n                    workarg = quoteResults[1];<br \/>\n                }<br \/>\n                else<br \/>\n                {<br \/>\n                    workarg = arg;<br \/>\n                }<\/p>\n<p>                Regex argRE = new Regex(&#8220;&#8211;(.*)=(.*)&#8221;);<br \/>\n                string[] regResults = argRE.Split(workarg);<br \/>\n                if (regResults.Length > 1)<br \/>\n                {<br \/>\n                    Console.WriteLine(string.Format(&#8221; &#8212; {0} -> {1}&#8221;, regResults[1], regResults[2]));<br \/>\n                    argsDict.Add(regResults[1], regResults[2]);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n<\/PRE><\/p>\n<p>Get a argument:<\/p>\n<p><PRE><br \/>\n     static public string GetArg(string arg)<br \/>\n        {<br \/>\n            string result = null;<br \/>\n            argsDict.TryGetValue(arg, out result);<br \/>\n            \/\/if (debug)<br \/>\n            \/\/    Console.WriteLine(&#8220;arg &#8221; + arg + &#8221; -> &#8221; + result);<br \/>\n            return result;<br \/>\n        }<br \/>\n<\/PRE><\/p>\n<p>At the top of main(), call:<\/p>\n<p><PRE><br \/>\nArgHandler(args);<br \/>\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since I posted a perl one. Note there&#8217;s probably some neat way to do this using system libraries that I just don&#8217;t know about, but this is what I use As part of the class (you can have this as a class i.e. Config.cs, or as part of the program: static readonly Dictionary argsDict = [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,6,7],"_links":{"self":[{"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2334"}],"collection":[{"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/comments?post=2334"}],"version-history":[{"count":2,"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2334\/revisions"}],"predecessor-version":[{"id":2336,"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/posts\/2334\/revisions\/2336"}],"wp:attachment":[{"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/media?parent=2334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/categories?post=2334"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sheer.us\/weblogs\/wp-json\/wp\/v2\/tags?post=2334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}