Introduction
I needed a simple (but working) parameter parsing method. After searching for a long time, nothing came out. This doesn't mean there are no useful classes, but none of them fitted my needs. This class is able to parse all kinds of parameters.
For example, which classes are able to parse this:
Command Line Parameters
-booltest 1 -log -url www.url-check.com -check true
Parse Results
Name |
Value |
Type |
-booltest |
1 |
boolean |
-url |
www.url-check.com |
string |
-log |
- |
- |
-check |
true |
boolean |
The main features are as follows:
- No use of MFC, so the class can be used in Unix or Win32 applications
- Type checking
- Case sensitive or case insensitive, it’s adjustable
- Uses exceptions instead of integer return values
This is not an award-winning piece of code. But, this piece of code will ease the pain when using parameters. So, if you are experiencing some problems when using parameters in your solutions, just try this class to see if it fits your requirements.
How to Use
This part will briefly explain how to use the CGetOpt
class. If you still have any questions, just post your question in the forum below.
Start by adding these files to your project:
Adding Parameters
You have to add all the parameters the user can enter to the object. This allows the class to check for expected parameters, type checking and value checking. You can add a parameter to the class by calling the function:
void AddExpectedParameter(string sParameter, bool bValueExpected = true,
ParameterType oParamType = PARAMETERTYPE_UNKNOWN);
For example, three parameters are added:
Name |
Type |
-booltest |
boolean |
-url |
string |
-log |
- |
-check |
boolean |
CGetOpt oParamParsing;
oParamParsing.AddExpectedParameter("-booltest", true, PARAMETERTYPE_BOOL);
oParamParsing.AddExpectedParameter("-url", true, PARAMETERTYPE_STRING);
oParamParsing.AddExpectedParameter("-log", false);
oParamParsing.AddExpectedParameter("-check", true, PARAMETERTYPE_BOOL);
All the parameters added to the CGetOpt
object can now be used by the user.
Parsing
The parsing of the command line is very simple. The CGetOpt
class uses different type of exceptions which can be caught.
The different exceptions that can be thrown are:
Exception Type |
Description |
WrongTypeException |
Wrong parameter type is used |
ValueExpectedException |
Value was expected, but none was given |
UnknownParameterException |
Unknown parameters are used |
DoubleParametersException |
Parameters are used twice |
A simple example of parsing with error checking (all exceptions are caught):
try
{
oParamParsing.ParseCommandLine(szCommandLine);
}
catch (exception & ex)
{
printf("%s\n", ex.what());
}
Reading Parameter Results
There are two ways of reading the parameter results:
- Call all parameters by hand.
Is parameter used?
ParameterUsed(string sParameter);
Get the value of the parameter:
Param oInfo = GetParameterResultInfo(string sParameter);
printf("%s => %s\n", oInfo.sParameter.c_str(), oInfo.sValue.c_str());
- Iterate through all the used items:
for (int i = 0; i < oParamParsing.GetParameterResultCount(); i++)
{
Param oInfo = oParamParsing.GetParameterResultInfo(i);
printf("%s => %s\n", oInfo.sParameter.c_str(), oInfo.sValue.c_str());
}
CGetOpt Demo
There is also a small demo included. The demo contains the same code which is given as an example in this article.
The demo works quite simple. You start the application, four basic parameters are added to the allowed parameters, and you can try to enter a parameter list.
When you want to quit the demo application, simply hit enter with an empty line.
History
- March 7th, 2006
- Released version 1.1 which contains a small bug fix when checking for valid integer type
- February 28th, 2006