Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++
Article

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

Rate me:
Please Sign up or sign in to vote.
4.96/5 (13 votes)
14 Jan 20022 min read 144.6K   902   49   19
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.

Introduction

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 Arguments_src.zip.

The following code should be enough to make you curious:

After including...

#include "Arguments.h"

...you 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 ) )
    exit(-1);

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

if( cArg['h'] )
{
    cArg.Usage();
    exit(0);
}

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;
else
    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...

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

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

Arguments:
      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?

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
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
NewsOptional arguments don't work correctly! Pin
Member 796511916-Jun-11 23:47
Member 796511916-Jun-11 23:47 
General.NET version anyone? (C++/CLI port) Pin
semmel7122-Oct-07 10:46
semmel7122-Oct-07 10:46 
GeneralLovely class - perfectly with VC8 and UNICODE Pin
semmel7122-Mar-07 13:39
semmel7122-Mar-07 13:39 
Generaloperator error for tstring Pin
edspace23-Dec-05 18:03
edspace23-Dec-05 18:03 
QuestionArticle moved ? Pin
Jonathan de Halleux18-Aug-02 23:21
Jonathan de Halleux18-Aug-02 23:21 
GeneralWindows.h Pin
30-Apr-02 6:34
suss30-Apr-02 6:34 
GeneralTnx Pin
3-Apr-02 1:47
suss3-Apr-02 1:47 
GeneralLicense Pin
29-Jan-02 6:29
suss29-Jan-02 6:29 
GeneralRe: License Pin
Patrick Hoffmann29-Jan-02 6:45
Patrick Hoffmann29-Jan-02 6:45 
GeneralRe: License Pin
Dan WAlkes18-Aug-08 9:40
Dan WAlkes18-Aug-08 9:40 
GeneralWIN32 has _argc and _argv Pin
15-Jan-02 10:02
suss15-Jan-02 10:02 
GeneralRe: WIN32 has _argc and _argv Pin
Patrick Hoffmann15-Jan-02 23:02
Patrick Hoffmann15-Jan-02 23:02 
GeneralRe: WIN32 has _argc and _argv Pin
25-Jun-02 9:29
suss25-Jun-02 9:29 
GeneralRe: WIN32 has _argc and _argv Pin
mkrueger11-Feb-04 8:14
mkrueger11-Feb-04 8:14 
General-h switch is not working Pin
Tadeusz Dracz7-Nov-01 2:11
professionalTadeusz Dracz7-Nov-01 2:11 
GeneralRe: -h switch is not working Pin
7-Nov-01 2:27
suss7-Nov-01 2:27 
GeneralRe: -h switch is not working Pin
Master_U21-Jun-07 22:18
Master_U21-Jun-07 22:18 
GeneralCool, but problem with Unicode release Pin
CPPWorker5-Oct-01 9:19
CPPWorker5-Oct-01 9:19 
GeneralRe: Cool, but problem with Unicode release Pin
17-Jan-02 9:53
suss17-Jan-02 9: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.