Click here to Skip to main content
Licence CPOL
First Posted 5 Mar 2008
Views 17,780
Downloads 433
Bookmarked 34 times

Yet Another Command Line Parser

By | 5 Mar 2008 | Article
A simple to use C# Command Line parser.

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

Software Developer (Senior)

United States United States

Member

Professional game programmer for over 10 years. Doing my best to become a better programmer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralNConsoler - another command line parsing library Pinmembertihobrazov7:04 8 Sep '08  
GeneralRe: NConsoler - another command line parsing library PinmemberClaire Streb13:49 20 Sep '11  
GeneralProblems parsing stand-alone paramaters. Pinmemberkenrentz6:58 3 Jun '08  
AnswerRe: Problems parsing stand-alone paramaters. PinmemberMarkusD1:46 24 Feb '09  
GeneralMore details in article please PinmemberHotcut3:54 6 Mar '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 5 Mar 2008
Article Copyright 2008 by ChrisStoy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid