Click here to Skip to main content
13,253,492 members (51,129 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 25 Aug 2004

ParamContainer - easy-to-use command-line parameter parser

, 3 Feb 2005
Rate this:
Please Sign up or sign in to vote.
ParamContainer is a simple and easy to use C++ class to parse command line parameters. Parameters format are derived from UNIX getopt_long() function syntax but may contain nested parameters as well.


ParamContainer is a simple and easy to use C++ class to parse command line parameters. Parameters format is derived from UNIX getopt_long() function syntax but may contain nested parameters as well. It was developed to fit the requirements of our projects, but we'll be glad if it will be useful for somebody else. Main features of ParamContainer are:

  • Easy to use.
  • Structure of command line conforms object hierarchy.
  • Adding/changing parameters is really easy. You don't need to modify class interfaces and anything outside of the class which new parameter corresponds to.
  • Parameters can be saved to the project file and loaded later.
  • When command line contains additional file names, their paths will be converted to relative in project file, so you can freely move project with all required files to a different location.
  • ParamContainer can be used as internal interface between presentation (GUI) and logic parts of the project. You can use the same logic part in graphics/command-line versions of your project.
  • Dynamically generated help screen.
  • Powerful error handling.
  • Portability between Win32 and Unix systems (on Win32 systems, there must be WIN32 preprocessor definition).

Using the code

Here is a simple way to use ParamContainer in your project:

  • Add ParamContainer.cpp and ParamContainer.h into your project.
  • Include ParamContainer.h in your main CPP file:
    #include "ParamContainer.h"
  • Create ParamContainer object in your main() function.
    ParamContainer p;
  • Add some parameters using addParam().
    p.addParam("long-name", 'n', ParamContainer::regular, 
               "parameter description", "default_value");  
    //"long-name" - long parameter name
    //'n' - short parameter name
    //ParamContainer::regular - parameter type 
    // (regular means that parameter is not required and has an argument)
    //"parameter description" - description
    //"default_value" - default value
  • Call parseCommandLine().
    p.parseCommandLine(argc, argv[]);
  • Obtain parameter values via [].
    cout << p["long-name"];
  • Compile and run your program, specifying your argument:
    programname --long-name=value


    programname -n value


    programname -n "value"


25.08.2004 - First version is available.

BioRainbow Group.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

Questionnice, but no way to retrieve parameter values? Pin
philgoetz20-Apr-06 9:54
memberphilgoetz20-Apr-06 9:54 
I want to pass the name of a filename on the command line, then open and read from that file. I want to put the filename into a char* variable. There doesn't appear to be any way to do this. None of these will work:

char filename[256];
strcpy(filename, p["paramName"]);


filename << p["paramName"];


char filename[256] = p["paramName"];

Also, I'd like to know what sort of license you're releasing this with. Can I use it for anything, free of charge?

Sadly, I spent some time writing a similar class about 15 years ago, and can't find the source code to it now.
Generalnice, but Pin
cooky_r22-Aug-05 0:16
membercooky_r22-Aug-05 0:16 
GeneralBoost has a very similar library Pin
alnitak22-Sep-04 1:49
memberalnitak22-Sep-04 1:49 
GeneralA few suggestions Pin
alnitak15-Sep-04 10:22
memberalnitak15-Sep-04 10:22 
GeneralRe: A few suggestions Pin
Johann Gerell5-Feb-05 7:48
memberJohann Gerell5-Feb-05 7:48 
GeneralVery nice Pin
m4dc4p1-Sep-04 6:55
memberm4dc4p1-Sep-04 6:55 
GeneralRe: Very nice Pin
BioRainbow2-Sep-04 21:55
memberBioRainbow2-Sep-04 21:55 

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
Web01 | 2.8.171114.1 | Last Updated 4 Feb 2005
Article Copyright 2004 by BioRainbow
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid