Click here to Skip to main content
12,294,775 members (59,724 online)
Click here to Skip to main content

Stats

42.5K views
380 downloads
24 bookmarked
Posted

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.Collections;
using System.Text.RegularExpressions;
using System.Xml;

namespace Params
{
	public class Param
	{
		protected string longName;
		protected string shortName;
		protected string description;
		protected ParamType type;
		protected bool isOptional;
		protected bool isDefault;
		protected bool allowMultipleValues;
		protected object value;
		protected object defaultValue;
		protected ArrayList values;

		public string Name
		{
			get
			{
				if(longName != null)
					return longName;
				else
					return shortName;
			}
		}
		
		public string ShortName
		{
			get
			{
				return this.shortName;
			}
		}

		public string LongName
		{
			get
			{
				return this.longName;
			}
		}

		public string Description
		{
			get 
			{
				return this.description;
			}
		}
		
		public ParamType Type
		{
			get
			{
				return this.type;
			}
		}
		
		public bool IsOptional
		{
			get
			{
				return this.isOptional;
			}
		}

		public bool IsDefault
		{
			get
			{
				return this.isDefault;
			}
		}

		public bool AllowMultipleValues
		{
			get
			{
				return this.allowMultipleValues;
			}
		}
			
		public object DefaultValue
		{
			get
			{
				return this.defaultValue;
			}
		}

		public object Value
		{
			get
			{
				if(this.allowMultipleValues)
				{
					if(this.values.Count > 0)
					{
						return this.values[0];
					}
					else
					{
						return null;
					}
				}
				else
				{
					return this.value;
				}
			}
		}

		internal void SetValue(object value)
		{
			if(this.allowMultipleValues)
			{
				this.values.Clear();
				this.values.Add(Cast(value));
			}
			else
			{
				this.value = Cast(value);
			}
		}

		internal void AddValue(object value)
		{
			if(this.allowMultipleValues)
			{
				this.values.Add(Cast(value));
			}
			else
			{
				this.value = Cast(value);
			}
		}

		public ArrayList Values
		{
			get 
			{
				if(this.allowMultipleValues)
				{
					return this.values;
				}
				else
				{
					ArrayList a = new ArrayList(1);
					a.Add(this.value);
					return a;
				}
			}
		}

		protected object Cast(object o)
		{
			object result;

			if(o is string && ((string)o) == string.Empty)
				o = null;

			switch(this.Type)
			{
				case ParamType.String:
					result = (o == null) ? null : (o.ToString());
					break;
				case ParamType.Integer:
					result = (o == null) ? null : (object)Convert.ToInt32(o);
					break;
				case ParamType.TimeSpan:
					if(o is String)
					{
						result = TimeSpan.Parse((string)o);
					}
					else //assume it is TimeSpan, don't catch the exception to show the user that something went wrong
					{
						result = (TimeSpan)o;
					}
					break;
				case ParamType.Float:
					result = (o == null) ? null : (object)Convert.ToSingle(o);
					break;
				case ParamType.Bool:
					result = (o == null) ? null : (object)Convert.ToBoolean(o);
					break;
				default:
					throw new NotImplementedException();
			}

			return result;
		}

		internal Param()
		{
			this.isOptional = true;
			this.allowMultipleValues = false;
		}

		internal Param(string name, object value) : this()
		{
			this.longName = name;
			this.value = value;
		}
		
		internal Param(string name, object value, ParamCollection col)
		{
			this.longName = name;
			this.value = value;
			this.isOptional = col.DefaultIsOptional;
			this.allowMultipleValues = col.DefaultAllowMultipleValues;

			if(allowMultipleValues)
				this.values = new ArrayList();
		}				
		
		internal Param(string name, object value, bool allowMultipleValues, bool isDefault)
		{
			this.longName = name;
			this.value = value;
			this.allowMultipleValues = allowMultipleValues;
			this.isDefault = isDefault;

			if(allowMultipleValues)
				this.values = new ArrayList();
		}		
		
		internal Param(XmlNode node, ParamCollection col)
		{
			this.longName = Helper.GetString(node.Attributes, "longName", null);
			this.shortName = Helper.GetString(node.Attributes, "shortName", null);
			this.description = Helper.GetString(node.Attributes, "description", null);

			this.type = (ParamType)Helper.GetEnum(node.Attributes, "type", col.DefaultType, typeof(ParamType));

			this.defaultValue = Cast(Helper.GetValue(node.Attributes, "defaultValue", null));
     
			this.isOptional = Helper.GetBool(node.Attributes, "optional", col.DefaultIsOptional);
			this.allowMultipleValues = Helper.GetBool(node.Attributes, "allowMultiple", col.DefaultAllowMultipleValues);
            			
			if(allowMultipleValues)
				this.values = new ArrayList();

			SetValue(Cast(node.InnerText));
		}

		public bool IsNamed(string name)
		{
            if(name == this.ShortName || name == this.LongName)
				return true;
			else
				return false;
		}
	}
}

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.

You may also be interested in...

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