Click here to Skip to main content
15,892,059 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.Configuration;
using System.Collections;
using System.Xml;

namespace Params
{
	public class ParamConfigurationSectionHandler : IConfigurationSectionHandler
	{
		#region IConfigurationSectionHandler Members

		public object Create(object parent, object context, System.Xml.XmlNode section)
		{
			if(ParamCollection.ApplicationParameters != null)
				throw(new Exception("ParamCollection already initialized. Use ParamCollection.ApplicationParameters."));

			ParamCollection col = new ParamCollection();

			col.DefaultAllowMultipleValues = Helper.GetBool(section.Attributes, "defaultAllowMultiple", false);
			col.DefaultIsOptional = Helper.GetBool(section.Attributes, "defaultOptional", false);
			col.DefaultType = (ParamType)Helper.GetEnum(section.Attributes, "defaultType", ParamType.String, typeof(ParamType));

			col.ShortControlString = Helper.GetString(section.Attributes, "shortControlString", "-");
			col.LongControlString = Helper.GetString(section.Attributes, "longControlString", "-");
			col.SeparatorChar = Helper.GetString(section.Attributes, "separatorChar", " ")[0];
			col.AttribChars = Helper.GetString(section.Attributes, "attribChars", " ").ToCharArray();
			col.AutoGenerateParams = Helper.GetBool(section.Attributes, "autoGenerateParams", false);

			foreach(XmlNode param in section.ChildNodes)
			{
				if(param.NodeType == XmlNodeType.Element && param.Name.ToLower() == "param")
				{
					col.Add(new Param(param, col));
				}
			}

			return col;
		}

		#endregion
	}
}

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