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?