Click here to Skip to main content
12,691,789 members (27,554 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked

Managing argc and argv[] with STL in a simple way

, 14 Jan 2002
Rate this:
Please Sign up or sign in to vote.
This class makes it very easy and unified to process the argument list of your main(). It provides many features to support options with parameters, optional parameters and automatic output of error messages and a short manual. It uses STL and is unicode compliant.
<!-- Download Links --> <!-- Add the rest of your HTML here -->


This class unifies the process to read the argument list of your main() in a very easy way. It provides many features to support options with parameters, optional parameters and automatic output of error messages and a short manual. It uses STL and is unicode compliant.

A short piece of HTML documentation, the source, a .DSP for a static library and a a test application is given in

The following code should be enough to make you curious:

After including...

#include "Arguments.h" build an instance of Arguments in your main(); the name of the application, a copyright notice and the possible option-markers are given as parameters to the constructor...

void main( int argc, char *argv[] )
	Arguments cArg( "argtest", "(C) 2001 NOVACOM GmbH", "-/" );

Now you add the definition of your options and parameters. The first example is a simple option "-h" to get the manual. For that you can use the AddOption() with the character 'h' and a brief description that occurs automatically in the manual.

cArg.AddOption('h', "display usage help");

The second one is the parameter "2". It's definition is a little bit more complicated, because we now want to add a parameter to the option. Building the instance of cOpt is similar to the AddOption call. When you create the option in this way you are able to add parameters with AddArgument() to the !option!:

Arguments::Option   cOpt( '2', "define additional input file" );
cOpt.AddArgument( "outfile2", "additional input filename" );
cArg.AddOption( cOpt );

Adding arguments is quite simple too. You use AddArgument() to define an argument with a name and a brief description.

cArg.AddArgument( "infile", "filename to read" );

You are also able to give a default value for an argument. The result is an optional argument...

cArg.AddArgument( "outfile", "filename for output", "stdout" );

After defining the options and arguments you can instruct your Arguments instance to parse the command line with...

if( !cArg.Parse( argc, argv ) )

To request an option (e.g. the -h option) you only have to write the following code...

if( cArg['h'] )

It's the same with '-2', and you also get the argument of this option by using []...

if( cArg['2'] )
    cout << "Option -2 set, outfile2 = " << cArg['2']["outfile2"] << endl;
    cout << "Option -2 not set" << endl;

Requesting the arguments doesn't really differ...

cout << "infile = " << cArg["infile"] << endl;

So the manual output of our example will be...

Usage: argtest [-2 outfile2] [-h] infile [outfile]

      -2        define additional input file
       outfile2       = additional input filename
      -h        display usage help

      infile  = filename to read
      outfile = filename for output
             optional argument (default='stdout')

(C) 2001 NOVACOM GmbH

Isn't it easy to change the settings of your program arguments, now?


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

Patrick Hoffmann
Web Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

NewsOptional arguments don't work correctly! Pin
Member 796511917-Jun-11 0:47
memberMember 796511917-Jun-11 0:47 
General.NET version anyone? (C++/CLI port) Pin
semmel7122-Oct-07 11:46
membersemmel7122-Oct-07 11:46 
GeneralLovely class - perfectly with VC8 and UNICODE Pin
semmel7122-Mar-07 14:39
membersemmel7122-Mar-07 14:39 
Generaloperator error for tstring Pin
edspace23-Dec-05 19:03
memberedspace23-Dec-05 19:03 
QuestionArticle moved ? Pin
Jonathan de Halleux19-Aug-02 0:21
memberJonathan de Halleux19-Aug-02 0:21 
GeneralWindows.h Pin
Jason Sandys30-Apr-02 7:34
memberJason Sandys30-Apr-02 7:34 
GeneralTnx Pin
sen_763-Apr-02 2:47
membersen_763-Apr-02 2:47 
GeneralLicense Pin
Karl Mutch29-Jan-02 7:29
memberKarl Mutch29-Jan-02 7:29 
GeneralRe: License Pin
Patrick Hoffmann29-Jan-02 7:45
memberPatrick Hoffmann29-Jan-02 7:45 
GeneralRe: License Pin
Dan WAlkes18-Aug-08 10:40
memberDan WAlkes18-Aug-08 10:40 
GeneralWIN32 has _argc and _argv Pin
Don Hatlestad15-Jan-02 11:02
memberDon Hatlestad15-Jan-02 11:02 
GeneralRe: WIN32 has _argc and _argv Pin
Patrick Hoffmann16-Jan-02 0:02
memberPatrick Hoffmann16-Jan-02 0:02 
GeneralRe: WIN32 has _argc and _argv Pin
Unplugged25-Jun-02 10:29
memberUnplugged25-Jun-02 10:29 
GeneralRe: WIN32 has _argc and _argv Pin
mkrueger11-Feb-04 9:14
sussmkrueger11-Feb-04 9:14 
General-h switch is not working Pin
Tadeusz Dracz7-Nov-01 3:11
memberTadeusz Dracz7-Nov-01 3:11 
GeneralRe: -h switch is not working Pin
Anonymous7-Nov-01 3:27
memberAnonymous7-Nov-01 3:27 
GeneralRe: -h switch is not working Pin
Master_U21-Jun-07 23:18
memberMaster_U21-Jun-07 23:18 
GeneralCool, but problem with Unicode release Pin
CPPWorker5-Oct-01 10:19
memberCPPWorker5-Oct-01 10:19 
GeneralRe: Cool, but problem with Unicode release Pin
Ben Berck17-Jan-02 10:53
memberBen Berck17-Jan-02 10:53 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170117.1 | Last Updated 15 Jan 2002
Article Copyright 2001 by Patrick Hoffmann
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid