Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Article

Param.NET - An Automated Command-line Parameter Parser

Rate me:
Please Sign up or sign in to vote.
2.48/5 (11 votes)
19 Oct 2005CPOL3 min read 54.2K   407   24   7
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

Introduction

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:

XML
<configSections>
<section name="parameters" type="Params.ParamConfigurationSectionHandler,Param.NET"
   allowLocation="false"/>
</configSections>

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.

XML
<parameters autoGenerateParams="false" defaultIsOptional="false" 
defaultType="String" shortControlString="-" longCOntrolString="--" attribChars=" =">
</parameters>

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:

XML
<param isOptional="false" longName="param-a" shortName="a" type="float"
   defaultValue="true"
   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.

C#
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.

History

  • 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 :(

License

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


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

Comments and Discussions

 
GeneralMy vote of 1 Pin
bobfox6-Mar-10 13:50
professionalbobfox6-Mar-10 13:50 
GeneralNConsoler - another command line parsing library Pin
Maxim Tihobrazov8-Sep-08 7:15
Maxim Tihobrazov8-Sep-08 7:15 
GeneralOh Pin
egoroff11-Oct-05 22:59
egoroff11-Oct-05 22:59 
GeneralRe: Oh Pin
eugen.anghel11-Oct-05 23:10
eugen.anghel11-Oct-05 23:10 
GeneralRe: Oh Pin
egoroff11-Oct-05 23:17
egoroff11-Oct-05 23:17 
GeneralRe: Oh - It doesn't work Pin
robvon19-Oct-05 14:24
robvon19-Oct-05 14:24 
GeneralRe: Oh - It doesn't work Pin
egoroff19-Oct-05 16:10
egoroff19-Oct-05 16: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.