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:
<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.
<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:
<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.
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
- 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 string
s: shortControlString
and longControlSting
, so you can set one to "-" and the other to "--". - Still very ugly code though :(