5,699,997 members and growing! (12,719 online)
Email Password   helpLost your password?
Languages » C# » Applications     Intermediate License: The Code Project Open License (CPOL)

Yet Another Command Line Parser

By ChrisStoy

A simple to use C# Command Line parser.
C# (C# 1.0, C# 2.0, C# 3.0, C#), Windows, .NET, Dev

Posted: 5 Mar 2008
Updated: 5 Mar 2008
Views: 5,781
Bookmarked: 21 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
4 votes for this Article.
Popularity: 2.47 Rating: 4.10 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 75.0%
4
1 vote, 25.0%
5

Introduction

This is a basic command-line argument parser. Most parsers I have found try to do too much, such as default parameter support that requires a complicated setup. This utility simply parses the command line arguments into argument/value pairs and a list of single parameters. It is up to the user to figure out what to do after that.

Background

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. Simply call the Parse() method on the 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

  • Release 1.0.0.0 - 3/5/2008.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ChrisStoy


Professional game programmer for over 10 years. Doing my best to become a better programmer.
Occupation: Software Developer (Senior)
Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralNConsoler - another command line parsing librarymembertihobrazov8:04 8 Sep '08  
GeneralProblems parsing stand-alone paramaters.memberkenrentz7:58 3 Jun '08  
GeneralMore details in article pleasememberHotcut4:54 6 Mar '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Mar 2008
Editor: Smitha Vijayan
Copyright 2008 by ChrisStoy
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project