Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

CommandLine Parser

by the3factory 3/5/2008 11:02:00 PM

Parsing command line arguments is a common step in console applications. I needed a simple way of doing this without having to register a bunch of event handlers or complicated argument patterns. It also needed to support the most common argument flags ("-", "--", and "/"), as well as handle values with spaces in them (such as file paths.)

Using the code

The code is extremely easy to use. Simple call the Parse() method on CommandLine class with the string[] of arguments you wish to parse. Then extract the arguments and parameters from the returned CommandArgs.

For example:

static void Main(string[] args)
{
// parse the command line into arguments and parameters 
CommandArgs commandArgs = CommandLine.Parse(args);
// output all the argument pairs 
Console.WriteLine("Command Line Arguments:");
foreach ( KeyValuePair<string, string> pair in commandArgs.ArgPairs )
{
Console.WriteLine( string.Format( " {0} = {1}", pair.Key, pair.Value ));
}
// output all the parameters 
Console.WriteLine("\nCommand Line Parameters:");
foreach ( string param in commandArgs.Params )
{
Console.WriteLine( " " + param );
}
}

History

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0