Click here to Skip to main content
13,354,605 members (58,114 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 28 Feb 2006

CGetOpt - A commandline Parameter Parsing Class

, 6 Mar 2006
Rate this:
Please Sign up or sign in to vote.
This class enables you to easily parse a command line in any C++ application.
Sample Image - getopt_screen.png


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 -check true

Parse Results


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:

  • GetOpt.cpp
  • GetOpt.h

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:

// Declare variables
CGetOpt oParamParsing;

// Add some example parameters
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.


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 TypeDescription
WrongTypeExceptionWrong parameter type is used
ValueExpectedExceptionValue was expected, but none was given
UnknownParameterExceptionUnknown parameters are used
DoubleParametersExceptionParameters are used twice

A simple example of parsing with error checking (all exceptions are caught):

    // Parse parameters
catch (exception & ex)
    printf("%s\n", ex.what());

Reading Parameter Results

There are two ways of reading the parameter results:

  1. 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());
  2. Iterate through all the used items:
    // Show results
    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.


  • March 7th, 2006
    • Released version 1.1 which contains a small bug fix when checking for valid integer type
  • February 28th, 2006
    • Initial release


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


About the Author

Geert van Horrik
Software Developer CatenaLogic
Netherlands Netherlands
I am Geert van Horrik, and I have studied Computer Science in the Netherlands.

I love to write software using .NET (especially the combination of WPF and C#). I am also the lead developer of Catel, an open-source application development framework for WPF, Silverlight, WP7 and WinRT with the focus on MVVM.

I have my own company since January 1st 2007, called CatenaLogic. This company develops commercial and non-commercial software.

To download (or buy) applications I have written, visit my website:

You may also be interested in...

Comments and Discussions

GeneralThanks Pin
Fastfootskater11-Jul-07 1:02
memberFastfootskater11-Jul-07 1:02 
Thank you for sharing the code.
I have saved a lot time and I can go to lunch now Smile | :)

Greetings from the Lake of Constance, Germany


GeneralBoost.Program_Options Pin
Éric Malenfant7-Mar-06 4:19
memberÉric Malenfant7-Mar-06 4:19 
GeneralRe: Boost.Program_Options Pin
Geert van Horrik7-Mar-06 5:31
memberGeert van Horrik7-Mar-06 5:31 
GeneralRe: Boost.Program_Options Pin
Éric Malenfant7-Mar-06 7:09
memberÉric Malenfant7-Mar-06 7:09 
GeneralFine! Pin
krause6-Mar-06 20:24
memberkrause6-Mar-06 20:24 
GeneralRe: Fine! Pin
Geert van Horrik6-Mar-06 23:09
memberGeert van Horrik6-Mar-06 23:09 
GeneralThanks! Pin
franklinvv28-Feb-06 23:45
memberfranklinvv28-Feb-06 23:45 

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.180111.1 | Last Updated 7 Mar 2006
Article Copyright 2006 by Geert van Horrik
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid