Click here to Skip to main content
13,351,050 members (54,488 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 10 Oct 2005

Param.NET - An Automated Command-line Parameter Parser

, 19 Oct 2005
Rate this:
Please Sign up or sign in to vote.
A library that allows developers to specify the parameters that they expect to receive from the command line and provides an easier way to access them


This is a parameter parsing library that I created for a school project. Its main feature is that it automatically reads the parameters you specify in the app.config file into a Dictionary and allows you to access their values very easily.

Using the Code

To start using this library, you must first create a custom section handler in your application's configuration file to specify the type that will handle the <parameters> section. You can do this by placing the following lines in the app.config inside the <configuration> element:

<section name="parameters" type="Params.ParamConfigurationSectionHandler,Param.NET"


You must then create the <parameters> section that will contain the list of parameters that you expect to receive. Because there is no other documentation, I will specify all possible attributes with their default value, but all of them are optional so you will not have to do this every time.

<parameters autoGenerateParams="false" defaultIsOptional="false" 

defaultType="String" shortControlString="-" longCOntrolString="--" attribChars=" =">

The autoGenerateParams attribute indicates whether parameters that have been specified in the command-line but do not exist here should be considered valid and added to the dictionary like normal parameters.

The defaultIsOptional attribute specifies whether the parameters should be optional by default or mandatory. This gets overwritten by each parameter's own optional attribute.

shortControlString and longControlString signal the beginning of a new parameter and attribChars is a list of characters that separate the parameter name from its value.

defaultType is the type that all parameters will have unless they specify their own types. This includes autoGenerated parameters, so if you specify float here and pass an unknown parameter with a value that cannot be converted to float it will be considered invalid. The possible types that a parameter can have are String, Integer, Float, and Bool.

You can now (finally) start specifying the parameters you would like to receive. Each parameter will be a new <param> element:

<param isOptional="false" longName="param-a" shortName="a" type="float"


   description="Some float parameter named a">false</param>

The defaultValue is the value that the parameter will have if no value is specified at the command-line. The text inside the element is the value that the parameter will have if it is not passed at all.

longName and shortName are the two names that the parameter can take. With controlChar "-", shortName can be for example "i" and longName "-install", so you would be able to pass -i or --install with the same effect. You don't need to specify both of these, only one will do.

With all the configuration over, it's now time to start reading the values.

The whole parsing is done in the static constructor of the ParamCollection class so you don't have to worry about initialization. To read the values, simply access ParamCollection.ApplicationParameters["<insert-param-name-here>"].Value from anywhere in your app. The value returned has the type that was specified in the configuration file so you can directly cast it to the desired type. For example, if you specified float, a boxed float will be returned.

float a = (float)ParamCollection.ApplicationParameters["a"];

In the above example, specifying either ParamCollection.ApplicationParameters["a"].Value or ParamCollection.ApplicationParameters["param-a"].Value returns the same value, so it does not matter which one of the names the passed parameter had.

I know there is a lot of room for improvement to the library and I hope I will have the time needed to continue working on it.


  • 10th October, 2005
    • Initial post
  • 20th October, 2005
    • I updated the files to a version with a lot fewer bugs and a few new features.
    • You can now specify more that one AttribChars and there are two control strings: shortControlString and longControlSting, so you can set one to "-" and the other to "--".
    • Still very ugly code though :(


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


About the Author

Web Developer
Romania Romania
Just you average programmer geek from Romania. Student at Faculty of Computer Science, Al. I. Cuza University Iasi, Romania.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
bobfox6-Mar-10 14:50
memberbobfox6-Mar-10 14:50 
GeneralNConsoler - another command line parsing library Pin
tihobrazov8-Sep-08 8:15
membertihobrazov8-Sep-08 8:15 
GeneralOh Pin
egoroff11-Oct-05 23:59
memberegoroff11-Oct-05 23:59 
GeneralRe: Oh Pin
CISCBrain12-Oct-05 0:10
memberCISCBrain12-Oct-05 0:10 
GeneralRe: Oh Pin
egoroff12-Oct-05 0:17
memberegoroff12-Oct-05 0:17 
GeneralRe: Oh - It doesn't work Pin
robvon19-Oct-05 15:24
memberrobvon19-Oct-05 15:24 
GeneralRe: Oh - It doesn't work Pin
egoroff19-Oct-05 17:10
memberegoroff19-Oct-05 17:10 

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
Web04 | 2.8.180111.1 | Last Updated 20 Oct 2005
Article Copyright 2005 by eugen.anghel
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid