Click here to Skip to main content
15,896,269 members
Articles / Programming Languages / C#

A C#/.NET Attributes Based Command Line Argument Parser

Rate me:
Please Sign up or sign in to vote.
4.96/5 (9 votes)
10 Jan 2012CPOL21 min read 43.3K   1.6K   37  
This article introduces an easy to use attribute/reflection based library for seamlessly parsing command line arguments for applications.
//
// NAttrArgs
//
// Copyright (c) 2012 Pete Barber
//
// Licensed under the The Code Project Open License (CPOL.html)
// http://www.codeproject.com/info/cpol10.aspx 
//
using System;
using System.Reflection;
using System.Linq;

namespace NAttrArgs
{
	public class MemberSetter
	{
		public static void SetOptionalFlagArg<T>(T t, MemberAttribute arg, bool value)
		{
			SetImpl(t, arg, value);
		}

		public static void SetRemainingArg<T>(T t, MemberAttribute arg, string[] value)
		{
			SetImpl(t, arg, value);
		}

		public static void SetArg<T>(T t, MemberAttribute arg, string value)
		{
			if (arg.AllowedValues != null)
				if (arg.AllowedValues.Contains(value) == false)
					throw new FormatException(string.Format("Not an allowed value:{0}", value));

			SetImpl(t, arg, value);
		}

		public static void SetImpl<T, U>(T t, MemberAttribute arg, U value)
		{
			if (arg.MemberInfo is FieldInfo)
				SetFieldWithArg((FieldInfo)arg.MemberInfo, t, value);
			else if (arg.MemberInfo is PropertyInfo)
				SetPropertyWithArg((PropertyInfo)arg.MemberInfo, t, value);
			else if (arg.MemberInfo is MethodInfo)
				SetMethodWithArg((MethodInfo)arg.MemberInfo, t, value);
			else
				throw new NotImplementedException("Unsupported member type");
		}

		public static void SetFieldWithArg<T, U>(FieldInfo foundMember, T t, U value)
        {
			foundMember.SetValue(t, CustomConvert.ChangeType(value, foundMember.FieldType));
        }

		public static void SetPropertyWithArg<T, U>(PropertyInfo foundMember, T t, U value)
        {
			foundMember.SetValue(t, CustomConvert.ChangeType(value, foundMember.PropertyType), null);
        }

		public static void SetMethodWithArg<T, U>(MethodInfo foundMember, T t, U value)
		{
			ParameterInfo[] paramInfo = foundMember.GetParameters();

			if (paramInfo.Length == 0)
				foundMember.Invoke(t, new object[] { });
			else
				foundMember.Invoke(t, new object[] { CustomConvert.ChangeType(value, paramInfo[0].ParameterType) });
		}
	}
}

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
Team Leader
United Kingdom United Kingdom
My day job is mostly working in C++ with a bit of C#. I write a fair amount of command line based tools and really wish they could have a GUI front-end to them hence why I spend my spare time working with WPF.

I started a blog few years back but didn't do a lot with it. I've started describing some of the interesting programming things I come across on it. Please take a look.

Comments and Discussions