C#: converting string[] args to Dictionary

Sample for converting string[] args to Dictionary<string, string>.

Notice both keys and values are converted to UPPER and separated by “:” in args.

        public void Main(string[] args)
        {
            Dictionary<string, string> argDict = ArgsToDictionary(args);
            bool batchMode = KeyToBool(argDict, "BATCHMODE");
        }

        private Dictionary<string, string> ArgsToDictionary(string[] args)
        {
            var argDict = new Dictionary<String, String>();
            if (args != null && args.Length > 0)
            {
                var argList = new List<string>(args);
                var argKvList = new List<KeyValuePair<String, String>>();
                argList.ForEach(itm => argKvList.Add(ArgToKeyValuePair(itm)));
                argDict = argKvList.ToDictionary(itm => itm.Key, itm => itm.Value);
            }
            return argDict;
        }

        private KeyValuePair<string, string> ArgToKeyValuePair(string arg)
        {
            string[] pair = arg.ToUpper().Split(':');
            string key = "", val = "";
            if (pair.Length > 0)
            {
                key = pair[0];
                if (key.StartsWith("-")) //e.g. "-BATCHMODE:$TRUE" like in Powershell
                {
                    key = key.Substring(1); //Remove starting "-"
                }
            }
            if (pair.Length > 1) val = pair[1];
            var retVal = new KeyValuePair<string, string>(key, val);
            return retVal;
        }

        public bool KeyToBool(Dictionary<string, string> argDict, string key)
        {
            bool retVal = false;
            if (argDict.ContainsKey(key.ToUpper()))
            {
                var boolString = argDict[key];
                if (boolString.StartsWith("$")) //e.g. $TRUE like in Powershell
                {
                    boolString = boolString.Substring(1); //Remove starting "$"
                }
                bool value;
                if (!string.IsNullOrEmpty(boolString))
                {
                    bool.TryParse(boolString, out value);
                }
                else
                {
                    value = false;
                }
                retVal = value;
            }
            return retVal;
        }

        public string KeyToString(Dictionary<string, string> argDict, string key)
        {
            string retVal = "";
            if (argDict.ContainsKey(key.ToUpper()))
            {
                retVal = argDict[key];
            }
            return retVal;
        }

The End.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: