Click here to Skip to main content
15,886,075 members
Articles / Desktop Programming / MFC
Article

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

Rate me:
Please Sign up or sign in to vote.
2.06/5 (9 votes)
3 Feb 20051 min read 58.1K   874   32   7
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.

Introduction

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
    

    or

    programname -n value
    

    or

    programname -n "value"
    

History

25.08.2004 - First version is available.

BioRainbow Group.

License

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


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionnice, but no way to retrieve parameter values? Pin
philgoetz20-Apr-06 8:54
philgoetz20-Apr-06 8:54 
Generalnice, but Pin
cooky_r21-Aug-05 23:16
cooky_r21-Aug-05 23:16 
GeneralBoost has a very similar library Pin
User 9727622-Sep-04 0:49
User 9727622-Sep-04 0:49 
GeneralA few suggestions Pin
User 9727615-Sep-04 9:22
User 9727615-Sep-04 9:22 
Thanks for posting this, I was just looking for something to deal with this chore! I have a few suggestions if you don't mind, I am using MS dotNet 2003 (beginner here so take this with a pinch of salt).

<br />
At the end of ParamContainer.h there is this line:<br />
		#endif __PARAMCONTAINER_H<br />
it needs to be changed to<br />
		#endif // __PARAMCONTAINER_H<br />

and a newline is needed too!


Secondly you are using a couple of functions (stricmp and getcwd) which are not found by the Microsoft linker, because MS saw fit to rename them to _stricmp and _getcwd. If the names are changed in the source then the code compiles fine. Better is to use this bit of macro hackery I got from the web:

<br />
/* _MSC_VER is defined by Microsoft compilers */<br />
#ifdef WIN32<br />
#  ifdef _MSC_VER<br />
#    define stricmp _stricmp<br />
#    define getcwd _getcwd<br />
#  endif<br />
#else<br />
#define stricmp strcasecmp<br />
#endif<br />



Actually, the use of stricmp can be eliminated altogether (it's not part of the C++ standard, it's a C routine) by writing a routine to lowercase a string:


<br />
std::string ToLower(const std::string& Str) {<br />
    std::string result(Str);<br />
    for (std::string::iterator i = result.begin(); i != result.end(); ++i)<br />
        *i = static_cast<char> (tolower(*i));<br />
    return result;<br />
}<br />
<br />
        if(j==k && ((div[0]=='\\' && (ToLower(spath.substr(i, j-i)) != ToLower(tpath.substr(i, k-i)))) || (div[0]=='/' && spath.substr(i, j-i) == tpath.substr(i, k-i))))<br />
		//if(j==k && ((div[0]=='\\' && !stricmp(spath.substr(i, j-i).c_str(), tpath.substr(i, k-i).c_str())) || (div[0]=='/' && spath.substr(i, j-i)==tpath.substr(i, k-i))))<br />



getcwd can't be removed in this fashion, but I would go further and notice that there is quite a lot of code just to support the loading and saving of parameter containers, and I don't think this will be used by 95% of the clients of this class, so why not move it into a derived class, say "PersistentParamContainer", or into global functions within a ParamContainer namespace? I didn't have time to pursue this myself, so I just deleted all that code. (In fact, this gets rid of the need for stricmp too Smile | :)

I would also like the facility to do a lookup of option names in a case sensitive manner. This is because it is my experience that most people on Windows expect case-insensitive operations. I think this should be done by getting rid of the initOptions routine, adding those and the new case sensitive option into the constructor, and then constructing a map which is case-sensitive or not, as appropriate. There is some code here to do it, but I haven't had chance to do that yet. And it's a bit tricky at the moment with all those Russian comments Smile | :)

If you email me I will send you my modified version.




--
alnitak
GeneralRe: A few suggestions Pin
Johann Gerell5-Feb-05 6:48
Johann Gerell5-Feb-05 6:48 
GeneralVery nice Pin
m4dc4p1-Sep-04 5:55
m4dc4p1-Sep-04 5:55 
GeneralRe: Very nice Pin
BioRainbow2-Sep-04 20:55
BioRainbow2-Sep-04 20: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.