Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

Param.NET - An Automated Command-line Parameter Parser

Rate me:
Please Sign up or sign in to vote.
2.48/5 (11 votes)
19 Oct 2005CPOL3 min read 54.4K   407   24  
A library that allows developers to specify the parameters that they expect to receive from the command line and provides an easier way to access them
using System;
using System.Xml;
using System.Collections;
using System.Collections.Specialized;

namespace Params
{
	public class Helper
	{
		public static object GetValue(object o, string key, object defaultValue)
		{
			if(o is IDictionary)
			{
				IDictionary dict = o as IDictionary;

				if(dict.Contains(key))
					return dict[key];
				else
					return defaultValue;
			}
			if(o is NameValueCollection)
			{
				NameValueCollection nvc = o as NameValueCollection;

				if(nvc[key] != null)
					return nvc[key];
				else
					return defaultValue;
			}
			if(o is XmlAttributeCollection)
			{
				XmlAttributeCollection xac = o as XmlAttributeCollection;
				
				if(xac[key] != null)
					return xac[key].Value;
				else
					return defaultValue;
			}

			throw new InvalidOperationException();
		}

		public static bool GetBool(object o, string key, bool defaultValue)
		{
			object ret = GetValue(o, key, null);

			if(ret == null)
				return defaultValue;
			else
				return Convert.ToBoolean(ret);
		}

		public static int GetInt(object o, string key, int defaultValue)
		{
			object ret = GetValue(o, key, null);

			if(ret == null)
				return defaultValue;
			else
				return Convert.ToInt32(ret);
		}
		public static float GetFloat(object o, string key, float defaultValue)
		{
			object ret = GetValue(o, key, null);

			if(ret == null)
				return defaultValue;
			else
				return Convert.ToSingle(ret);
		}
		public static Enum GetEnum(object o, string key, Enum defaultValue, Type enumType)
		{
			object ret = GetValue(o, key, defaultValue);
			if(ret is string)
				return (Enum)Enum.Parse(enumType, (string)ret, true);
			else
				return (Enum)ret;
		}
		public static string GetString(object o, string key, string defaultValue)
		{
			return GetValue(o, key, defaultValue) as string;
		}

		public static int BeginsWith(string str, int offset, params string[] patterns)
		{
			for(int i = 0; i < patterns.Length; i++)
			{
				string pattern = patterns[i];
				if(	pattern != null && 
					pattern != string.Empty && 
					str.Length >= (pattern.Length + offset) &&
					str.Substring(offset, pattern.Length).Equals(pattern))
				{
					return i;
				}
			}
			return -1;
		}
	
		public static int GetNextParamIndex(string cmdLine, int offset, string shortControlString, string longControlString)
		{
			int valueEndIndexShort = cmdLine.IndexOf(shortControlString, offset);
			int valueEndIndexLong = cmdLine.IndexOf(longControlString, offset);

			if(valueEndIndexShort == -1 && valueEndIndexLong == -1)
				return cmdLine.Length-1;
			else if(valueEndIndexLong == -1)
				return valueEndIndexShort;
			else if(valueEndIndexShort == -1)
				return valueEndIndexLong;
			else
			{
				return Math.Min(valueEndIndexLong, valueEndIndexShort);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Romania Romania
Just you average programmer geek from Romania. Student at Faculty of Computer Science, Al. I. Cuza University Iasi, Romania.

Comments and Discussions