Click here to Skip to main content
11,414,619 members (68,619 online)
Click here to Skip to main content
Add your own
alternative version

Param.NET - An Automated Command-line Parameter Parser

, 19 Oct 2005 CPOL
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)

Share

About the Author

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 20 Oct 2005
Article Copyright 2005 by eugen.anghel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid