Click here to Skip to main content
13,247,704 members (17,092 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 5 Mar 2008

Yet Another Command Line Parser

, 5 Mar 2008
Rate this:
Please Sign up or sign in to vote.
A simple to use C# Command Line parser.


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.


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 ); 


  • Release - 3/5/2008.


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


About the Author

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

You may also be interested in...


Comments and Discussions

GeneralNConsoler - another command line parsing library Pin
tihobrazov8-Sep-08 8:04
membertihobrazov8-Sep-08 8:04 
NConsoler is an open source library that provides command line parser functionality based on attribute metadata attached to type.
Library is very easy to add and use in your application. NConsoler gives an ability to display help and validation messages without any line of code.

Example code:

using System;
using NConsoler;

public class Program {
public static void Main(params string[] args) {
Consolery.Run(typeof(Program), args);

public static void Method(
[Required] string name,
[Optional(true)] bool flag) {
Console.WriteLine("name: {0}, flag: {1}", name, flag);

and use it:

program.exe "Maxim" /-flag
GeneralRe: NConsoler - another command line parsing library Pin
Claire Streb20-Sep-11 14:49
memberClaire Streb20-Sep-11 14:49 
GeneralProblems parsing stand-alone paramaters. Pin
kenrentz3-Jun-08 7:58
memberkenrentz3-Jun-08 7:58 
AnswerRe: Problems parsing stand-alone paramaters. Pin
MarkusD24-Feb-09 2:46
memberMarkusD24-Feb-09 2:46 
GeneralMore details in article please Pin
Hotcut6-Mar-08 4:54
memberHotcut6-Mar-08 4:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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 | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 5 Mar 2008
Article Copyright 2008 by ChrisStoy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid