Click here to Skip to main content
Click here to Skip to main content

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

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

       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

Share

About the Author

Patrick Hoffmann
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

 
NewsOptional arguments don't work correctly! PinmemberMember 796511917-Jun-11 0:47 
General.NET version anyone? (C++/CLI port) Pinmembersemmel7122-Oct-07 11:46 
GeneralLovely class - perfectly with VC8 and UNICODE Pinmembersemmel7122-Mar-07 14:39 
Generaloperator error for tstring Pinmemberedspace23-Dec-05 19:03 
QuestionArticle moved ? PinmemberJonathan de Halleux19-Aug-02 0:21 
GeneralWindows.h PinmemberJason Sandys30-Apr-02 7:34 
GeneralTnx Pinmembersen_763-Apr-02 2:47 
GeneralLicense PinmemberKarl Mutch29-Jan-02 7:29 
GeneralRe: License PinmemberPatrick Hoffmann29-Jan-02 7:45 
GeneralRe: License PinmemberDan WAlkes18-Aug-08 10:40 
GeneralWIN32 has _argc and _argv PinmemberDon Hatlestad15-Jan-02 11:02 
GeneralRe: WIN32 has _argc and _argv PinmemberPatrick Hoffmann16-Jan-02 0:02 
GeneralRe: WIN32 has _argc and _argv PinmemberUnplugged25-Jun-02 10:29 
GeneralRe: WIN32 has _argc and _argv Pinsussmkrueger11-Feb-04 9:14 
General-h switch is not working PinmemberTadeusz Dracz7-Nov-01 3:11 
GeneralRe: -h switch is not working PinmemberAnonymous7-Nov-01 3:27 
GeneralRe: -h switch is not working PinmemberMaster_U21-Jun-07 23:18 
GeneralCool, but problem with Unicode release PinmemberCPPWorker5-Oct-01 10:19 
GeneralRe: Cool, but problem with Unicode release PinmemberBen Berck17-Jan-02 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.141223.1 | Last Updated 15 Jan 2002
Article Copyright 2001 by Patrick Hoffmann
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid