Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C#

C#/.NET Command Line Arguments Parser

Rate me:
Please Sign up or sign in to vote.
4.91/5 (104 votes)
5 Nov 2002MIT3 min read 864.9K   8.5K   269  
Class to parse command line arguments and store/retrieve them.
/*
* Test class: main testing class
*
* Authors:		R. LOPES
* Contributors:	R. LOPES
* Created:		28 October 2002
* Modified:		28 October 2002
*
* Version:		1.0
*/

using System;
using CommandLine.Utility;

namespace CommandLine{
	/// <summary>
	/// Testing class
	/// </summary>
	class Test{
		/// <summary>
		/// Main loop
		/// </summary>
		[STAThread]
		static void Main(string[] Args){
			// Command line parsing
			Arguments CommandLine=new Arguments(Args);

			// Look for specific arguments values and display them if they exist (return null if they don't)
			if(CommandLine["param1"]!=null) Console.WriteLine("Param1 value: "+CommandLine["param1"]);
			else Console.WriteLine("Param1 not defined !");
			
			if(CommandLine["height"]!=null) Console.WriteLine("Height value: "+CommandLine["height"]);
			else Console.WriteLine("Height not defined !");
			
			if(CommandLine["width"]!=null) Console.WriteLine("Width value: "+CommandLine["width"]);
			else Console.WriteLine("Width not defined !");
			
			if(CommandLine["size"]!=null) Console.WriteLine("Size value: "+CommandLine["size"]);
			else Console.WriteLine("Size not defined !");
			
			if(CommandLine["debug"]!=null) Console.WriteLine("Debug value: "+CommandLine["debug"]);
			else Console.WriteLine("Debug not defined !");

			// Wait for key
			Console.Out.WriteLine("Arguments parsed. Press a key...");
			Console.Read();
			}
		}
	}

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 MIT License


Written By
Software Developer (Senior) Siliconz Ltd
New Zealand New Zealand
Richard Lopes
Just Programmer

Comments and Discussions